#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int a,b,n,str,sum;
char x[102];
int main()
{
while(scanf("%d ",&n)==1) /*為什麼%d後面加個空格就AC 沒加就WA?*/
{
for(a=0;a<n;a++)
{
gets(x);
str=strlen(x);
printf("Case #%d: ",a+1);
sum=0;
for(b=0;b<str;b++)
{
if(x[b]=='a'||x[b]=='d'||x[b]=='g'||x[b]=='j'||x[b]=='m'||x[b]=='p'||x[b]=='t'||x[b]=='w'||x[b]==' ')
sum+=1;
else if(x[b]=='b'||x[b]=='e'||x[b]=='h'||x[b]=='k'||x[b]=='n'||x[b]=='q'||x[b]=='u'||x[b]=='x')
sum+=2;
else if(x[b]=='c'||x[b]=='f'||x[b]=='i'||x[b]=='l'||x[b]=='o'||x[b]=='r'||x[b]=='v'||x[b]=='y')
sum+=3;
else if(x[b]=='s'||x[b]=='z')
sum+=4;
}
printf("%d\n",sum);
}
}
return 0;
}
問題如程式碼的紅色字部份
你不能直接把scanf和gets混在一起用,這樣就會出現無法預知的狀況。(每個編譯器都這樣)
你有得到AC就很幸運了,恭喜。希望你能試著修改成只有scanf或gets。
#include
#include
#include
int a,b,n,str,sum;
char x[102];
int main()
{
while(scanf("%d ",&n)==1) /*為什麼%d後面加個空格就AC 沒加就WA?*/
{
for(a=0;a
{
gets(x);
str=strlen(x);
printf("Case #%d: ",a+1);
sum=0;
for(b=0;b
{
if(x[b]=='a'||x[b]=='d'||x[b]=='g'||x[b]=='j'||x[b]=='m'||x[b]=='p'||x[b]=='t'||x[b]=='w'||x[b]==' ')
sum+=1;
else if(x[b]=='b'||x[b]=='e'||x[b]=='h'||x[b]=='k'||x[b]=='n'||x[b]=='q'||x[b]=='u'||x[b]=='x')
sum+=2;
else if(x[b]=='c'||x[b]=='f'||x[b]=='i'||x[b]=='l'||x[b]=='o'||x[b]=='r'||x[b]=='v'||x[b]=='y')
sum+=3;
else if(x[b]=='s'||x[b]=='z')
sum+=4;
}
printf("%d\n",sum);
}
}
return 0;
}
問題如程式碼的紅色字部份
抱歉,剛剛第二個回覆是不小心按到的
By the way, 你可以用
int T;
char s[201];
cin.getline(s,200);
sscanf(s,"%d",&T);
這樣就可以了
抱歉,剛剛第二個回覆是不小心按到的
By the way, 你可以用
int T;
char s[201];
cin.getline(s,200);
sscanf(s,"%d",&T);
這樣就可以了
眞的耶!加一個空格就AC