2542: 弟弟的作业
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 174 Solved: 83
[Submit][Status][Web Board]
Description
你的弟弟刚做完了“100以内数的加减法”这部分的作业,请你帮他检查一下。每道题目(包括弟弟的答案)的格式为a+b=c或者a-b=c,其中a和b是作业中给出的,均为不超过100的非负整数;c是弟弟算出的答案,可能是不超过200的非负整数,也可能是单个字符"?",表示他不会算。
Input
输入文件包含不超过100行,以文件结束符结尾。每行包含一道题目,格式保证符合上述规定,且不包含任何空白字符。输入的所有整数均不含前导0。
Output
输出仅一行,包含一个非负整数,即弟弟答对的题目数量。
Sample Input
1+2=3 3-1=5 6+7=? 99-0=99
Sample Output
2
#include<stdio.h> int main() { int a,b,c,n=0; char ch; char s[5]; while(scanf("%d%c%d=%s",&a,&ch,&b,&s)!=EOF) { if(s[0]!='?') { c=atoi(s);//把字符串转化成整数型数// if(ch=='+'&&a+b==c) n++; if(ch=='-'&&a-b==c) n++; } } printf("%d ",n); return 0; }
#include<stdio.h> int main() { char s[99]; int a,b,c,n=0; while(scanf("%s",&s)!=EOF) { if(sscanf(s,"%d+%d=%d",&a,&b,&c)==3&&a+b==c) n++; if(sscanf(s,"%d-%d=%d",&a,&b,&c)==3&&a-b==c) n++; } printf("%d ",n); return 0; } 头文件:#include <stdio.h> sscanf()函数用于从字符串中读取指定格式的数据,其原型如下: int sscanf (char *str, char * format [, argument, ...]); 【参数】参数str为要读取数据的字符串;format为用户指定的格式;argument为变量,用来保存读取到的数据。 【返回值】成功则返回参数数目,失败则返回-1,错误原因存于errno 中。 sscanf()会将参数str 的字符串根据参数format(格式化字符串)来转换并格式化数据(格式化字符串请参考scanf()), 转换后的结果存于对应的变量中。