Sapphire
2022/2/23
解题思路:
建议直接看题解。
你可能有的疑惑:
1.为什么要 char d[3] 而不直接 char d?
答:因为当你输入99到d时,如果是char d,只能读取到9这个字符,所以我们只好放弃用d-'0'这样的思路。
2.atoi函数的作用是?
答:atoi函数藏在头文件stdlib.h里,作用是将字符串转换成整型数字。
参考代码:
#include<stdio.h> #include<stdlib.h> int main() { int a,b; char c; char d[3]; int k; int count=0; while(~(k=scanf("%d%c%d=%s",&a,&c,&b,d))) { if((a>=0&&a<=100)&&(b>=0&&b<=100)) { if((c=='+')&&(a+b==atoi(d)))count++; if((c=='-')&&(a-b==atoi(d)))count++; } } printf("%d",count); return 0; }
该代码的优越性在于,不用考虑当d为'?'时的这一特殊情况,只要满足两个if,count++。
0.0分
2 人评分
C语言训练-素数问题 (C语言代码)浏览:1689 |
淘淘的名单 (C语言代码)答案错误???浏览:614 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:584 |
WU-复数求和 (C++代码)浏览:2111 |
WU-printf基础练习2 (C++代码)浏览:2047 |
2004年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:536 |
C语言程序设计教程(第三版)课后习题6.5 (C++代码)浏览:481 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:385 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:583 |
字符串输入输出函数 (C语言代码)浏览:2565 |