第二期训练白银组第二题

Sum Problem

Time limit 500 ms
Memory limit 262144 kB

Problem Description

wHAT DO WE NEED cAPS LOCK FOR?
Caps lock is a computer keyboard key. Pressing it sets an input mode in which typed letters are capital by default. If it is pressed by accident, it leads to accidents like the one we had in the first passage.
Let’s consider that a word has been typed with the Caps lock key accidentally switched on, if:
either it only contains uppercase letters;
or all letters except for the first one are uppercase.
In this case we should automatically change the case of all letters. For example, the case of the letters that form words “hELLO”, “HTTP”, “z” should be changed.
Write a program that applies the rule mentioned above. If the rule cannot be applied, the program should leave the word unchanged.

Input

The first line of the input data contains a word consisting of uppercase and lowercase Latin letters. The word’s length is from 1 to 100 characters, inclusive.

Output

Print the result of the given word’s processing.

Examples

Input

cAPS

Output

Caps

Input

Lock

Output

Lock

问题链接:CodeForces - 131A

问题简述:

输入字母,如果全是大写或者首字母是小写而其他为大写,则大写换小写,小写换大写,如果不是以上两种情况则不作处理。

问题分析:
定义字符串数组将字母当做字符串处理,通过分单个字母情况、全是大写情况、开头是小写其余是大写三种情况考虑,其他归为else直接输出不作处理
  
程序说明:

用strlen计算字符串的长度,通过长度分出单个字母和多个字母,处理完单个字母用goto语句直接截断调试避免后续else发生错误,多个字母再讨论全是大写情况、开头是小写其余是大写,其余直接else输出不作处理
  
AC通过的C语言程序如下:

#include <iostream>
using namespace std;

int main()
{
	char a[2000];
	cin >> a;
	int length;
	length = strlen(a);
	int sum = 0;
	if (length == 1)                          //单个字母情况
	{
		if ((a[0] >= 'a')&(a[0] <= 'z'))
		{
			a[0]=a[0]- 32;
			cout << a[0];goto L;
		}
		else 
		{
			a[0] = a[0] + 32;;
			cout << a[0]; goto L;
		}
	}
	
	 
		for (int i = 0;i < length;i++)
		{
			if ((a[i] >= 'A')&(a[i] <= 'Z'))
			{
				sum++;
			}
			if(sum==length)                     //全是大写情况
			{
				for (int j = 0;j < length;j++)
				{
					a[j] = a[j] + 32;
					cout << a[j];
				}
				goto L;
			}
		
		}
		if ((sum == (length - 1))&(a[0] >= 'a'&a[0] <= 'z'))         //开头字母是小写其余全是大写情况
		{
			a[0] = a[0] - 32;
			cout << a[0];
			for (int k = 1;k < length;k++)
			{
				a[k] = a[k] + 32;
				cout << a[k];
			}
		}
		else {
			for (int M = 0;M < length;M++)
			{
				cout << a[M];
			}
		}
	    

	
L:;
}

猜你喜欢

转载自blog.csdn.net/weixin_44003969/article/details/84996853