关于一些初级ACM竞赛题目的分析和题解(五)。
拓展了一些字母变换的题目,看题:
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.
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.
Print the result of the given word's processing.
cAPS
Caps
Lock
Lock
1.字母全为大写;2.除了第一个字母外其余全为大写。
时变换大小写,不满足时照常输出,下面是代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
char a[600]; //定义字符串,并确定范围大于100
scanf("%s",a); //输入字符串
int b=32; // 定义变量
for(int i=1;a[i];i++) // 确定条件
if(a[i]>='a') b=0; //执行
for(int i=0;a[i];i++)
putchar(b^a[i]); //改变字符串
return 0;
}
题目涉及到了位与,位或,位异或,的数据处理,举个例子&= 是按位与之后赋值,^=是按位异或之后赋值,|=是按位或之后赋值。与,或以及异或的操作很简单:
1
2
3
4
|
101010 101010 101010
& 011100 | 011100 ^ 011100
--------- ---------- ----------
001000 111110 110110
|
若没有三个字母之一则输出NO,下面是代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int a,b,l;
a=0;
b=0;
char s[500]; // 定义字符串,并确定范围。
scanf("%s",s); // 输入字符串
l=strlen(s); // 确定字符串长度
for (int i=0;i<=l-1;i++)
{if (s[i]=='H'||s[i]=='Q'||s[i]=='9') // 判断
return 0*printf("YES"); // 执行并输出
else b++;
}
if (b==l)
printf("NO"); // 输出
}
有一些细节 例如l=strlen(s) 代表的是s字符串的长度 strlen() 代表字符串长度的函数,
return 0*printf(“a”) 意为输出a并结束,相当于输出后面加break,