(一)用ctrl+z终止输入, ctrl+z之前的数都被乘在一起(因为scanf)
测试了输入:
89aa9 && 3 **17
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
//
freopen("input.txt", "r", stdin);
int num, res;
char ch;
int mul = 1;
bool not_done = true;
//int cnt = 0;
do{
//printf("hi\n");
res = scanf("%d", &num);
///一开始写成了scanf("%d\n", &num); 错了
//printf("#%d\n", ++cnt);
if(res == EOF)
{
not_done = false;
break;
}
else if(res == 0)
{
ch = getchar();
/*
输入8974enter, 8974被读入num中而'\n'不会被scanf读取,留在输入流中;
下一个循环开始时,scanf读到'\n'但忽略这个空白符,等待其他输入
所以下面的写法不可行,下面的代码永远读不到'\n'
if(ch == '\n')
{
not_done = false;
break;
}
*/
//printf("%c\n", ch);
}
else
{
mul = (mul*num)%1000;
//printf("%d, %d\n", num, mul);
}
} while(not_done);
printf("the last three digits: %d\n", mul);
}
(二)每行为一组数,乘在一起求后三位
测试:
89aa9 && 3 **17 **9
kk97 iey 35 **3 *7
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
//
freopen("input.txt", "r", stdin);
const int MAXN = 1000;
char input[MAXN];
while(fgets(input, MAXN, stdin))
{
bool is_num = false;
int num, mul;
mul = 1;
for(int i = 0; i < strlen(input); ++i)
{
if(isdigit(input[i]))
{
if(!is_num)
{
is_num = true;
num = 0;
}
if(is_num)
{
num = num*10 + (input[i]-'0');
}
}
else
{
if(is_num)
{
is_num = false;
mul = (mul*num) % 1000;
}
}
}
//if the last number is terminated by EOF
if(is_num)
{
is_num = false;
mul = (mul*num)%1000;
}
printf("the last 3 digits: %d\n", mul);
}
}
了解一下atoi ?