《算法竞赛入门经典》第三章习题3-3

来源:互联网 发布:和珅如何发家 知乎 编辑:程序博客网 时间:2024/05/16 09:27

输入若干个整数(可以是正数、负数或者零),输出它们的乘积的末3位。这些整数中会混入一些由大写字母组成的字符串,你的程序应当忽略它们。提示:试试看,在执行scanf("%d")时输入一个字符串会怎样?

#include <cstdio>using namespace std;char str[100];int main(){printf("Please input:\n");int ch,num,product=1;while(1){num=scanf("%d", &ch);if( num==EOF )break;if( num==1 )product=product*ch;elsescanf("%s",str);}int a;a=(product>=0? product:-product);a=a%1000;    printf( "%3d\n",a);return 0;}

参考出处http://www.haogongju.net/art/2146315

原创粉丝点击