gets() 讀測資是以換行字元為結束
而scanf() 第一個數字後,這個數字後面的換行還在
所以必須要吸收掉這個換行,使用 %*c 把換行讀入但不存取
之後就沒問題了
#include <stdio.h>
int main(void)
{
char str[101];
char push[26]={1,2,3,1,2,3,1,2,3,1,2,3,1,
2,3,1,2,3,4,1,2,3,1,2,3,4};
int T, i, casenum=0, button=0;
scanf("%d%*c", &T);
while(++casenum <= T)
{
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
{
button++;
continue;
}
button+= push[str[i]-'a'];
}
printf("Case #%d: %d\n", casenum, button);
button=0;
}
return 0;
}