你弟弟刚刚学会写英语的一(one)、二(two)和三(three)。他在纸上写了好些一二三,可惜有些字母写错了。已知每个单词最多有一个字母写错了(单词长度肯定不会错),你能认出他写的啥吗?
Input
第一行为单词的个数(不超过10)。以下每行为一个单词,单词长度正确,且最多有一个字母写错。所有字母都是小写的。
Output
对于每组测试数据,输出一行,即该单词的阿拉伯数字。输入保证只有一种理解方式。
Sample Input
3
owe
too
theee
Sample Output
1
2
Input
第一行为单词的个数(不超过10)。以下每行为一个单词,单词长度正确,且最多有一个字母写错。所有字母都是小写的。
Output
对于每组测试数据,输出一行,即该单词的阿拉伯数字。输入保证只有一种理解方式。
Sample Input
3
owe
too
theee
Sample Output
1
2
3
卡了十几次,就是过不去。
#include<stdio.h>
#include<cstring>
#include<cstdlib>
char a[8];
int main(){
int t,n;
scanf("%d",&t);
getchar();
while(t--){
memset(a,0,sizeof(a));
bool g=false;
gets(a); //怎么也想不到,会错在这里
n=strlen(a);
if(n==5){
{if(a[1]=='h'&&a[2]=='r'&&a[3]=='e'&&a[4]=='e')
g=true;
if(a[0]=='t'&&a[2]=='r'&&a[3]=='e'&&a[4]=='e')
g=true;
if(a[0]=='t'&&a[1]=='h'&&a[3]=='e'&&a[4]=='e')
g=true;
if(a[0]=='t'&&a[1]=='h'&&a[2]=='r'&&a[4]=='e')
g=true;
if(a[0]=='t'&&a[1]=='h'&&a[2]=='r'&&a[3]=='e')
g=true;}
if(g)
printf("3\n");
}
else if(n==3){
if(a[0]=='o'&&a[1]=='n'||a[0]=='o'&&a[2]=='e'||a[1]=='n'&&a[2]=='e')
printf("1\n");
else if(a[0]=='t'&&a[1]=='w'||a[0]=='t'&&a[2]=='o'||a[1]=='w'&&a[2]=='o')
printf("2\n");
}
}
return 0;
}
把gets()换成scanf("%s",a),或者cin>>a,就AC,,,QAQ~~~~~
#include<stdio.h>
#include<cstring>
#include<cstdlib>
char a[8];
int main(){
int t,n;
scanf("%d",&t);
getchar();
while(t--){
memset(a,0,sizeof(a));
bool g=false;
scanf("%s",&a);
n=strlen(a);
if(n==5){
{if(a[1]=='h'&&a[2]=='r'&&a[3]=='e'&&a[4]=='e')
g=true;
if(a[0]=='t'&&a[2]=='r'&&a[3]=='e'&&a[4]=='e')
g=true;
if(a[0]=='t'&&a[1]=='h'&&a[3]=='e'&&a[4]=='e')
g=true;
if(a[0]=='t'&&a[1]=='h'&&a[2]=='r'&&a[4]=='e')
g=true;
if(a[0]=='t'&&a[1]=='h'&&a[2]=='r'&&a[3]=='e')
g=true;}
if(g)
printf("3\n");
}
else if(n==3){
if(a[0]=='o'&&a[1]=='n'||a[0]=='o'&&a[2]=='e'||a[1]=='n'&&a[2]=='e')
printf("1\n");
else if(a[0]=='t'&&a[1]=='w'||a[0]=='t'&&a[2]=='o'||a[1]=='w'&&a[2]=='o')
printf("2\n");
}
}
return 0;
}
当然,这也是WA太多次,无奈全部情况都罗列出来的代码;
其实不用那么麻烦:
#include<iostream>
#include<cstring>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
char a[6];
cin>>a;
if(strlen(a)==5)
puts("3");
else if(a[0]=='o'){
if(a[1]=='w'&&a[2]=='o')
puts("2");
else
puts("1");
}
else{
if(a[1]=='n'&&a[2]=='e')
puts("1");
else
puts("2");
}
}
return 0;
}
所以说,慎用gets,
参见大神的解释:点击打开链接