|
|
发表于 2026-4-15 17:17:40
|
显示全部楼层
测试题:
0:10个,在内循环中的循环条件是j<10时循环,而最后j=10时内层循环退出,而外层循环的条件是需要j!=10,而此时j=10,外层循环也退出了.所以将打印10个'A'
1:0个,因为i++是先取出变量i的值,然后在自增1,那么第一次循环是while内的值是0,循环直接不执行.
2: a, b, c,都是lvalue.
3:a=14, b=5, c=9
4:z = x < 0 ? -x : x
5:
A:
- if (size > 12)
- {
- cost = cost * 1.05;
- flag = 2;
- }
- bill = cost * flag;
复制代码
B:
- if (ibex > 14)
- {
- sheds = 3;
- }else
- {
- sheds = 2;
- }
- help = 2 * sheds;
复制代码
c:
- scanf("%d", &score);
- while(score < 0)
- {
- count ++
- scanf("%d", &score);
- }
- printf("count = %d\n", count);
复制代码
动动手:
0:
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char *argv[]) {
- float xjysmoey = 10000, hysmoney = 10000;
- int years = 1;
- while (xjysmoey >= hysmoney) {
- xjysmoey = 10000 + 10000 * years * 0.1;
- hysmoney = hysmoney + hysmoney * 0.05;
- years++;
- }
- printf("%d年后,黑夜的投资额超过小甲鱼!\n小甲鱼的投资额是:%."
- "2f\n黑夜的投资额是:%.2f\n",
- years - 1, xjysmoey, hysmoney);
- return EXIT_SUCCESS;
- }
复制代码
1:
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char *argv[]) {
- float money = 4000000;
- int years = 0;
- while (money >= 0) {
- money -= 500000;
- money += money * 0.08;
- years++;
- }
- printf("%d年之后,小甲鱼败光了所有的家产,再次回到一贫如洗......\n", years);
- return EXIT_SUCCESS;
- }
复制代码
2:- #include <math.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char *argv[]) {
- double PI = 0.0, n = 1, term = 1.0; // PI 是圆周率 n是分母,term是该项的值;
- int sign = 1; // sign代表正负值;
- while (fabs(term) >= 1e-8) { // 1e-8 就是10 ^ (-8) 在程序中的表达式.
- PI = PI + term;
- sign = -sign;
- n = n + 2;
- term = sign / n;
- }
- PI = PI * 4;
- printf("PI的值为:%15.7f\n", PI);
- return EXIT_SUCCESS;
- }
复制代码
3:
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char *argv[]) {
- long a;
- int month = 3;
- int firstmonth, sencondmonth;
- firstmonth = 1;
- sencondmonth = 1;
- while (month <= 24) {
- a = firstmonth + sencondmonth;
- sencondmonth = firstmonth;
- firstmonth = a;
- month++;
- }
- printf("%ld\n", a);
- return EXIT_SUCCESS;
- }
复制代码 |
|