A - 雷同检测
考试的时候老师最讨厌有人抄袭了。自从有了电子评卷,老师要查找雷同卷,就容易多了,只要将两个人的答案输入计算机,进行逐个字符的比对,把相同的位置都找出来,就一目了然了。
输入格式
22 行,每行包含一串字符(长度不超过 200200)。
输出格式
11 行,包含若干个以空格分隔的数字,表示出现相同字符的位置。
Sample Input
I am suantoujun. I am huayemei.
Sample Outpu
1 2 3 4 5 6 8 9
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
int i = 0;
char a[200], b[200];
gets(a);
gets(b);
while (a[i] != '\0' && b[i] != '\0')
{
if (a[i] == b[i])
printf("%d ", i+1);
i++;
}
return 0;
}
B - 首字母大写
对一个字符串中的所有单词,如果单词的首字母不是大写字母,则把单词的首字母变成大写字母。在字符串中,单词之间通过空白符分隔,空白符包括:空格(' ')、制表符('\t')、回车符('\r')、换行符('\n')。
Input
输入一行:待处理的字符串(长度小于80)。
Output
输出一行:转换后的字符串。
Sample Input
if so, you already have a google account. you can sign in on the right.
Sample Output
If So, You Already Have A Google Account. You Can Sign In On The Right.
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
char a[80];
int i=1;
gets(a);
if (a[0] >= 'a' && a[0] <= 'z')
a[0] = a[0] - 32;
while (a[i + 1] != '\0')
{
if ((a[i] == ' ' || a[i] == '\t' || a[i] == '\r' || a[i] == '\n') && (a[i + 1] != ' ' && a[i + 1] != '\t' && a[i + 1] != '\n' && a[i + 1] != '\r'))
{
if (a[i + 1] >= 'a' && a[i + 1] <= 'z')
a[i + 1] = a[i + 1] - 32;
}
i++;
}
puts(a);
return 0;
}
C - 大小写转换
读入一些字符串,将其中的小写字母转成大写字母(其他字符不变)。
输入
输入为多行,每行为一个字符串,字符串只由字母和数字组成,长度不超过80。输入以“End of file”结束。
输出
对于每行输入,输出转换后的字符串。
输入示例
Hello ICPC2004 12345abcde
输出示例
HELLO ICPC2004 12345ABCDE
提示
用“scanf("%s", str) == 1”这个条件可以判断输入是否结束。如果此条件为假,则输入结束(对于本题)。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
char str[80];
int i = 0;
while(scanf("%s", str) ==1)
{
for (i = 0; str[i] != '\0'; i++)
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
printf("%s\n", str);
}
return 0;
}
D - 数字反转
给定一个整数,请将该数各个位上数字反转得到一个新数。新数也应满足整数的常见形式,即除非给定的原数为零,否则反转后得到的新数的最高位数字不应为零(参见样例 22)。
输入格式
输入共 11 行,一个整数 NN。
输出格式
输出共 1 行,一个整数,表示反转后的新数。
数据范围
-1,000,000,000 \le N \le 1,000,000,000−1,000,000,000≤N≤1,000,000,000。
Sample Input
123
Sample Output
321
Sample Input 2
-380
Sample Output 2
-83
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int num1=123, num2=0;
int g = 1,n=10,wei=0,i=0;
scanf("%d", &num1);
if (num1 < 0)
{
g = -1;
num1 = -num1;
}
while (num1!= 0)
{
wei = num1 % 10;
num2 = num2 * n + wei;
num1 /= 10;
}
num2 = num2 * g;
printf("%d", num2);
return 0;
}
E - 删除单词后缀
给定一个单词,如果该单词以er
、ly
或者ing
后缀结尾, 则删除该后缀(题目保证删除后缀后的单词长度不为 00),否则不进行任何操作。
输入格式
输入一行,包含一个单词(单词中间没有空格,每个单词最大长度为 3232)。
输出格式
输出按照题目要求处理后的单词。
Sample Input
referer
Sample Output
refer
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
int n;
char a[32];
scanf("%s", a);
n = strlen(a);
if (n > 2 && a[n - 1] == 'r' && a[n - 2] == 'e') a[n - 2] = '\0';
if (n > 2 && a[n - 1] == 'y' && a[n - 2] == 'l') a[n - 2] = '\0';
if (n > 3 && a[n - 1] == 'g' && a[n - 2] == 'n' && a[n - 3] == 'i')a[n - 3] = '\0';
puts(a);
}
F - 判断字符串是否为回文
输入一个字符串,输出该字符串是否回文。回文是指顺读和倒读都一样的字符串。
输入格式
输入为一行字符串(字符串中没有空白字符,字符串长度不超过 100100)。
输出格式
如果字符串是回文,输出"yes"
;否则,输出"no"
。
Sample Input
abcdedcba
Sample Output
yes
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
int n=0,i=0;
char a[100];
gets(a);
n = strlen(a);
for (i = 0; i < n / 2; i++)
{
if (a[i] == a[n - i - 1])
i++;
else
break;
}
if (i >= n / 2)
printf("yes\n");
else
printf("no\n");
}
H - 字典序
给你两个不同的字符串,如果第一个字符串的字典序小于第二个字符串,则输出YES,如果第一个字符串的字典序大于第二个字符串,则输出NO。
Input
两行。第一行一个字符串,第二行一个字符串。保证字符串的长度不超过10000。保证两个字符串不完全相等。
Output
如果第一个字符串的字典序小于第二个字符串,则输出YES,如果第一个字符串的字典序大于第二个字符串,则输出NO。
Sample Input
abc abe
Sample Output
YES
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
int i;
char a[10000]={0}, b[10000]={0};
gets(a);
gets(b);
i = strcmp(a, b);
if (i < 0)
printf("YES");
else
printf("NO");
return 0;
}
I - 验证子串
输入两个字符串,验证其中一个串是否为另一个串的子串。
输入格式
输入两个字符串, 每个字符串占一行,长度不超过 200200 且不含空格。
输出格式
若第一个串 s_1s1 是第二个串 s_2s2 的子串,则输出"(s1) is substring of (s2)"
;
否则,若第二个串 s2是第一个串s1的子串,输出"(s2) is substring of (s1)"
;
否则,输出"No substring"
。
Sample Input
abc dddncabca
Sample Output
abc is substring of dddncabca
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
int n1 = 0, n2 = 0, i = 0,j=0,g=0;
char a[200], b[200];
scanf("%s", &a);
scanf("%s", &b);
n1 = strlen(a);
n2 = strlen(b);
if (n1 > n2)
{
for (i = 0; i <= n1; i++)
{
for (j = 0; j < n2; j++,i++)
{
if (a[i] == b[j]);
else break;
}
if (j == n2)
{
printf("%s is substring of %s",&b,&a);
g = 1;
}
}
}
if (n1 < n2)
{
for (i = 0; i <= n2; i++)
{
for (j = 0; j < n1; j++,i++)
{
if (b[i] == a[j]);
else break;
}
if (j ==n1)
{
printf("%s is substring of %s",&a,&b);
g = 1;
}
}
}
if (g == 0)
printf("No substring");
}
J - 子串查找(暴力检索结果超时)
这是一道模板题。
给定一个字符串 AA 和一个字符串 BB,求 BB 在 AA 中的出现次数。AA 和 BB 中的字符均为英语大写字母或小写字母。
AA 中不同位置出现的 BB 可重叠。
输入格式
输入共两行,分别是字符串 AA 和字符串 BB。
输出格式
输出一个整数,表示 BB 在 AA 中的出现次数。
样例
Input | Output |
---|---|
zyzyzyz zyz | 3 |
数据范围与提示
1 \leq A, B1≤A,B 的长度 \leq 10 ^ 6≤106,AA、BB 仅包含大小写字母。
#include<stdio.h>
#include<string.h>
int main()
{
char a[1000005]={0}, b[1000005]={0};
int i, j,count=0,n1=0,n2=0,g;
scanf("%s", a);
scanf("%s", b);
n1 = strlen(a);
n2 = strlen(b);
for (i = 0; i <= n1-n2; i++)
{
g=i;
for (j = 0; j < n2; j++,g++)
{
if (a[g] == b[j]);
else break;
}
if (j == n2)
count++;
}
printf("%d", count);
}