# include <stdio.h>
# include <string.h>
void fun1(char *s1);
int main()
{
int n;
scanf(“%d”, &n);
char s1[100000];
while (n–) 假如去掉这个while就没事 一加上程序就停止工作 不知道为什么
{
gets(s1);
fun1(s1);
}
return 0;
}
void fun1(char *s1)
{
char s2[50000];
int i, j, len, temp;
len = strlen(s1);
if (1 == len)
{
temp = s1[0] – “0”;
temp %= 9;
s2[0] = temp + “0”;
s2[1] = “\0”;
}
else
{
for (i=0, j=0; i<len-1; i+=2)
{
temp = (s1[i]-“0”)*10 + s1[i+1]-“0”;
temp %= 9;
s2[j++] = temp + “0”;
}
if (len % 2 != 0)
{
s2[j++] = s1[len-1];
}
s2[j] = “\0”;
}
len = strlen(s2);
if (1 == len)
printf(“%s\n”, s2);
else
fun1(s2);
}
# include <string.h>
void fun1(char *s1);
int main()
{
int n;
scanf(“%d”, &n);
char s1[100000];
while (n–) 假如去掉这个while就没事 一加上程序就停止工作 不知道为什么
{
gets(s1);
fun1(s1);
}
return 0;
}
void fun1(char *s1)
{
char s2[50000];
int i, j, len, temp;
len = strlen(s1);
if (1 == len)
{
temp = s1[0] – “0”;
temp %= 9;
s2[0] = temp + “0”;
s2[1] = “\0”;
}
else
{
for (i=0, j=0; i<len-1; i+=2)
{
temp = (s1[i]-“0”)*10 + s1[i+1]-“0”;
temp %= 9;
s2[j++] = temp + “0”;
}
if (len % 2 != 0)
{
s2[j++] = s1[len-1];
}
s2[j] = “\0”;
}
len = strlen(s2);
if (1 == len)
printf(“%s\n”, s2);
else
fun1(s2);
}
解决方案
40
你scanf(“%d”, &n);以后,输入缓冲区会有遗留的回车符,会被gets读走
所以scanf后面加一句fflush(stdin);才行
所以scanf后面加一句fflush(stdin);才行
# include <stdio.h> # include <string.h> void fun1(char *s1); int main() { int n; scanf("%d", &n); fflush(stdin); char s1[100000]; while (n--) //假如去掉这个while就没事 一加上程序就停止工作 不知道为什么 { gets(s1); fun1(s1); } return 0; } void fun1(char *s1) { char s2[50000]; int i, j, len, temp; len = strlen(s1); if (1 == len) { temp = s1[0] - "0"; temp %= 9; s2[0] = temp + "0"; s2[1] = "\0"; } else { for (i=0, j=0; i<len-1; i+=2) { temp = (s1[i]-"0")*10 + s1[i+1]-"0"; temp %= 9; s2[j++] = temp + "0"; } if (len % 2 != 0) { s2[j++] = s1[len-1]; } s2[j] = "\0"; } len = strlen(s2); if (1 == len) printf("%s\n", s2); else fun1(s2); }