训练一: 零起点学算法70——Yes,I can!
Description
Boys and girls:
Good evening.
Welcome to take the select contest. I wish you can be an ACMer, and ACM can bring the happiness of success to you. ACM is a little difficult,but if you try,you can!Nothing is impossible.
Input
One line, the ACMer’s name. The name is at most 8 characters.
Output
You should output one line like the output sample.
Output “I am” then the acmer’s name, then “,yes,I can!”
Sample Input
Mary
Sample Output
I am Mary,yes,I can!
测试代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
Description
Boys and girls:
Good evening.
Welcome to take the select contest. I wish you can be an ACMer, and ACM can bring the happiness of success to you.
ACM is a little difficult,but if you try,you can!Nothing is impossible.
Input
One line, the ACMer's name. The name is at most 8 characters.
Output
You should output one line like the output sample.
Output "I am" then the acmer's name, then ",yes,I can!"
Sample Input
Mary
Sample Output
I am Mary,yes,I can!
*/
int main()
{
char *str = calloc(8, sizeof(char));
while (gets(str) != NULL)
{
int cnt = strlen(str);
int i;
printf("I am ");
for (i = 0; i < cnt; i++)
printf("%c", *(str + i));
printf(",yes,I can!\n");
}
return 0;
}
运行结果:
测试结果: 通过!
简单的题目
练习二: 零起点学算法71——C语言合法标识符
Description
输入一个字符串,判断其是否是C的合法标识符。
Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出”yes”,否则,输出“no”。
Sample Input
3
12ajf
fi8x_a
ff ai_2
Sample Output
no
yes
no
练习代码一:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
Description
输入一个字符串,判断其是否是C的合法标识符。
Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。
Sample Input
3
12ajf
fi8x_a
ff ai_2
Sample Output
no
yes
no
*/
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
char *str = calloc(50, sizeof(char));
scanf("%s", str);
int cnt = strlen(str);
int i;
int is = 1;
if ((int)*str >= 48 && (int)*str <= 57) is = 0;
for (i = 1; i < cnt; i++)
{
if ((int)*(str + i) >= 48 && (int)*(str + i) <= 57 || (int)*(str + i) >= 65 && (int)*(str + i) <= 90 || (int)*(str + i) >= 97 && (int)*(str + i) <= 122 || *(str + i) == '_');
else is = 0;
}
if (is == 1) printf("yes\n");
else printf("no\n");
}
return 0;
}
运行结果:
第三个例子错误!
分析原因: scanf(“%s”,str); 只将前面的ff存入了字符串中了
试着用gets函数,但是scanf和gets在一起使用出了问题,百度了一下,解决方案如下:
感谢@lil259
用getchar过滤掉回车
测试代码二:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*
Description
输入一个字符串,判断其是否是C的合法标识符。
Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。
Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。
Sample Input
3
12ajf
fi8x_a
ff ai_2
Sample Output
no
yes
no
*/
int main()
{
int n;
scanf("%d", &n);
getchar();
while (n--)
{
char *str = calloc(50, sizeof(char));
gets(str);
int cnt = strlen(str);
int i;
int is = 1;
if ((int)*str >= 48 && (int)*str <= 57) is = 0;
for (i = 1; i < cnt; i++)
{
if ((int)*(str + i) >= 48 && (int)*(str + i) <= 57 || (int)*(str + i) >= 65 && (int)*(str + i) <= 90 || (int)*(str + i) >= 97 && (int)*(str + i) <= 122 || *(str + i) == '_');
else is = 0;
}
if (is == 1) printf("yes\n");
else printf("no\n");
}
return 0;
}
运行结果:
测试结果: 通过!!