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++代码)(利用两个库函数——abs()求绝对值 和 sort()快排)浏览:1510 |
C语言训练-舍罕王的失算 (C语言代码)浏览:1051 |
买不到的数目 (C++代码)浏览:901 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:785 |
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:623 |
【矩阵】 (C++代码)浏览:983 |
C语言程序设计教程(第三版)课后习题9.4 (C语言代码)浏览:696 |
1642题解浏览:778 |
矩形面积交 (C++代码)浏览:1199 |
【偶数求和】 (C语言代码)浏览:455 |