测试题
0. 10 个
1. 不打印 B
2. a、b、c 都是
3. a = 14,b = 5,c = 9
4. #include <stdio.h>
int main(void)
{
int x, z;
printf("请输入一个整数:");
scanf("%d", &x);
if (x >= 0) {
z = x;
} else {
z = -x;
}
printf("%d 的绝对值是:%d\n", x, z);
return 0;
}
5
A.#include <stdio.h>
int main(void)
{
int size, flag = 1;
float cost = 1.0, bill = 0.0;
printf("请输入size:");
scanf("%d", &size);
// 带 goto 版本
if (size > 12) {
goto a;
}
goto b;
a: cost = cost * 1.05;
flag = 2;
b: bill = cost * flag;
printf("带 goto 版本:cost = %f, flag = %d, bill = %f\n", cost, flag, bill);
cost = 1, flag = 1, bill = 0;
// 不带 goto 版本
if (size > 12) {
cost = cost * 1.05;
flag = 2;
}
bill = cost * flag;
printf("不带 goto 版本:cost = %f, flag = %d, bill = %f\n", cost, flag, bill);
return 0;
}
B.#include <stdio.h>
int main(void)
{
int ibex, sheds, help;
printf("请输入ibex:");
scanf("%d", &ibex);
// 带 goto 版本
if (ibex > 14) {
goto a;
}
sheds = 2;
goto b;
a: sheds = 3;
b: help = 2 * sheds;
printf("help = %d\n", help);
// 不带 goto 版本
if (ibex > 14) {
sheds = 3;
} else {
sheds = 2;
}
help = 2 * sheds;
printf("help = %d\n", help);
return 0;
}
C#include <stdio.h>
int main(void)
{
int score, count = 0;
// 带goto版本
readin: scanf("%d", &score);
if (score < 0) {
goto stage2;
}
count ++;
goto readin;
stage2: printf("count = %d\n", count);
// 不带goto版本
count = 0;
while (scanf("%d", &score), !(score < 0)) {
count++;
}
printf("count = %d\n", count);
return 0;
}
动动手
0.#include <stdio.h>
int main(void)
{
float x_money = 10000.0, h_money = 10000.0;
int years = 0;
while (x_money >= h_money)
{
years++;
x_money += 10000.0 * 0.1;
h_money *= 1.05;
}
printf("%d年后,黑夜的投资额超过小甲鱼!\n", years);
printf("小甲鱼的投资额是:%.2f\n", x_money);
printf("黑夜的投资额是:%.2f\n", h_money);
return 0;
}
1.#include <stdio.h>
int main(void)
{
float money = 4000000.0;
int years = 0;
while (money > 0) {
years++;
money -= 500000.0;
money *= 1.08;
}
printf("%d年后,笑甲鱼败光了所有的家产,再次回到一贫如洗……\n", years);
return 0;
}
2.#include <stdio.h>
int main(void)
{
double item, quarter_pi;
int num, sign;
for (num = 1, sign = 1; (item = 1.0 / num) >= 1e-8; num += 2, sign = -sign) {
quarter_pi += sign * item;
}
printf("Pi小数点后前7位为:%.7f\n", quarter_pi * 4);
return 0;
}
3.#include <stdio.h>
int main(void)
{
int f_1 = 1, f_2 = 1, cur, month;
for (month = 22; month > 0; month -= 1) {
cur = f_1 + f_2;
f_1 = f_2;
f_2 = cur;
}
printf("两年后的兔子总计%d对\n", cur);
return 0;
}
|