关于一些初级ACM竞赛题目的分析和题解(五)。

  关于一些初级ACM竞赛题目的分析和题解(五)。

拓展了一些字母变换的题目,看题:


  131A. cAPS lOCK
time limit per test
0.5 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
题目理解起来略有难度,n个字母属于【1,100】,满足以下条件之一 

1.字母全为大写;2.除了第一个字母外其余全为大写。

时变换大小写,不满足时照常输出,下面是代码

#include 
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
先把二进制形式写出来, &表示上下相同的输出该值,|表示上下不同的输出1,^表示上下不同的变为1,相同的统统变为0,这便是原理,

A. HQ9+
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

HQ9+ is a joke programming language which has only four one-character instructions:

  • "H" prints "Hello, World!",
  • "Q" prints the source code of the program itself,
  • "9" prints the lyrics of "99 Bottles of Beer" song,
  • "+" increments the value stored in the internal accumulator.

Instructions "H" and "Q" are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored.

You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output.

Input

The input will consist of a single line p which will give a program in HQ9+. String p will contain between 1 and 100 characters, inclusive. ASCII-code of each character of p will be between 33 (exclamation mark) and 126 (tilde), inclusive.

Output

Output "YES", if executing the program will produce any output, and "NO" otherwise.

Examples
input
Hi!
output
YES
input
Codeforces
output
NO
Note

In the first case the program contains only one instruction — "H", which prints "Hello, World!".

In the second case none of the program characters are language instructions.



本题需要注意细节HQ9+只有HQ9三个字符出现时才会打印 如果出现其中的字母则输出YES

若没有三个字母之一则输出NO,下面是代码

#include
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,





你可能感兴趣的:(竞赛)