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

本文解析了两个初级ACM竞赛题目,包括字母大小写转换问题和HQ9+编程语言指令识别问题,介绍了相关算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  关于一些初级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 <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
先把二进制形式写出来, &表示上下相同的输出该值,|表示上下不同的输出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<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,





ACM/ICPC(ACM International Collegiate Programming Contest, 国际大学生程序设计竞赛)是由国际计算机界历史悠久、颇具权威性的组织ACM(Association for Computing Machinery,国际计算机协会)主办的,世界上公认的规模最大、水平最高的国际大学生程序设计竞赛,其目的旨在使大学生运用计算机来充分展示自己分析问题解决问题的能力。该项竞赛从1970年举办至今已历29届,一直受到国际各知名大学的重视,并受到全世界各著名计算机公司的高度关注,在过去十几年中,APPLE、AT&T、MICROSOFTIBM等世界著名信息企业分别担任了竞赛的赞助商。可以说,ACM国际大学生程序设计竞赛已成为世界各国大学生最具影响力的国际级计算机类的赛事,是广大爱好计算机编程的大学生展示才华的舞台,是著名大学计算机教育成果的直接体现,是信息企业与世界顶尖计算机人才对话的最好机会。   该项竞赛分区域预赛国际决赛两个阶段进行,各预赛区第一名自动获得参加世界决赛的资格,世界决赛安排在每年的3~4月举行,而区域预赛安排在上一年的9~12月在各大洲举行。   ACM/ICPC的区域预赛是规模很大、范围很广的赛事。仅在2003年参加区域预赛的队伍就有来自75个国家(地区),1411所大学的3150支代表队,他们分别在127个赛场中进行比赛,以争夺全球总决赛的73个名额,其激烈程度可想而知。 2005年第30届ACM/ICPC亚洲赛区预赛共设了北京、成都、汉城、东京等11个赛站,来自亚洲各国知名高校的各个代表队进行了激烈的角逐。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值