NYOJ663 弟弟的作业

来源:互联网 发布:二合一沐浴露 知乎 编辑:程序博客网 时间:2024/05/11 10:16

原题链接


#include <stdio.h>//#include <stdlib.h>int main(){int a, b, c, count = 0;char ch, sign, s[5];while(scanf("%d%c%d=%s", &a, &sign, &b, s) != EOF){if(s[0] == '?') continue;if(sign == '+') c = a + b;else c = a - b;a = ch = 0;for(b = 0; s[b] != '\0'; ++b){if(s[b] == '-'){ch = 1;continue;}a = a * 10 + s[b] - '0';}if(ch) a = -a;if(a == c) ++count;}printf("%d\n", count);//system("pause");return 0;}


另一种思路:

#include <stdio.h>//#include <stdlib.h>int main(){int a, b, c, j, k, count = 0;char s[15];while(~scanf("%s", s)){j = sscanf(s, "%d+%d=%d", &a, &b, &c);k = sscanf(s, "%d-%d=%d", &a, &b, &c);if(j == 3 && a + b == c) ++count;else if(k == 3 && a - b == c) ++count;}printf("%d\n", count);//system("pause");return 0;}



0 0
原创粉丝点击