|
发表于 2024-6-14 18:58:19
|
显示全部楼层
测试题:
0.答:10个'A'
1.答:0个‘B’
2.答:a,b,c都是"l-value"
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;
5.B 答:
if (ibex > 14)
{
     sheds = 3;
}
sheds = 2;
help = 2 * sheds;
5.C 答:
if (score < 0)
{
       printf("count = %d\n", count);
}
count++;
scanf("%d", &score);
动动手:
0.答:
#include <stdio.h>
int main()
{
double fish=10000.0,black=10000.0,fish_interest=0.0,black_interest=0.0;
int years=0;
do
{
fish_interest+=fish*0.1;
black_interest=black*0.05;
black+=black_interest+10000.0;
years++;
}while(black_interest<fish_interest);
printf("%d年后,黑夜的投资额超过小甲鱼!\n",years);
printf("小甲鱼的投资额是:%.2f\n",fish_interest+10000.0);
printf("黑夜的投资额是:%.2f\n",black_interest+10000.0);
return 0;
}
1.答:
#include<stdio.h>
int main()
{
double fish_money=4000000.0;
int years=0;
while(fish_money>0)
{
fish_money-=500000.0;
fish_money+=fish_money*0.08;
years++;
}
printf("%d年之后,小甲鱼败光了所有的家产,再次回到一贫如洗......\n",years);
return 0;
}
2.答:
#include<stdio.h>
#include<math.h>
#define THRESHOLD 0.0000001 // 设置一个阈值来判断是否接近π
int main()
{
double x = 1.0, y = 0.0, z = 1.0, num_result = 0.0;
int sign = 1; // 用来表示每一项的符号
while (fabs(num_result - M_PI) > THRESHOLD)
{
// 根据sign来决定x的符号
x = sign * (1.0 / z);
y += x;
// 切换sign的值,以实现交替加减
sign = -sign;
z += 2;
num_result = fabs(y * 4);
}
printf("%.7f\n", num_result);
return 0;
}
3.答:
#include <stdio.h>
int main() {
int previous = 1; // 上一个月的兔子对数
int current = 1; // 当前月的兔子对数
int next; // 下一个月的兔子对数
int months = 24; // 假设两年为24个月
// 打印第1个月和第2个月的兔子对数
printf("Month 1: %d pairs\n", previous);
printf("Month 2: %d pairs\n", current);
// 从第3个月开始迭代计算
for (int i = 3; i <= months; i++) {
next = previous + current; // 计算下一个月的兔子对数
printf("Month %d: %d pairs\n", i, next);
// 更新上一个月和当前月的兔子对数
previous = current;
current = next;
}
// 如果只需要两年后的兔子对数总数,则不需要上面的打印语句,只需记录最后的值
printf("After 2 years (24 months): %d pairs\n", current);
return 0;
} |
|