|
发表于 2020-1-26 19:12:14
|
显示全部楼层
0. 请问下边代码将打印多少个 'A'?
#include <stdio.h>
int main()
{
int i, j;
for (i = 0; i != 10, j != 10; i++)
{
for (j = 0; j < 10; j++)
{
putchar('A');
}
}
putchar('\n');
return 0;
}
答:逗号语句代表和,表示i与j都不等于10时,停止循环。十次循环共打印10个A,在第十一次时,j==10,不符合内层循环条件,内层循环停止,保留j==10,外层循环中i++到10,外层循环结束,程序停止。
1. 请问下边代码会打印多少个 'B'?
#include <stdio.h>
int main()
{
int i = 0;
while (i++)
{
if (i > 10)
{
goto Label;
}
putchar('B');
}
Label: putchar('\n');
return 0;
}
答:while语句的循环条件是i++,但代码中缺少i++,所以无法启动循环,程序直接结束,什么都不会打印。
2. 请写出表达式 a = b = c = 5 中的"l-value"?
答:c==5, b==c, a==b;
3. 请问表达式 a = (b = 3, 4, c = b++ + 5, ++c + ++b); 执行后,整型变量 a、b、c 的值是?
答:a == 14; b == 3; c == 9;
4. 请使用条件运算符求出变量 x 的绝对值,并存放到变量 z 中。
答:x < 0 ? z = x * (-1): z = x;
5. C 语言其实在大部分情况下不使用 goto 语句也能做得很好,请尝试将下列代码段写成不带 goto 语句的版本。
A.
if (size > 12)
{
goto a;
}
goto b;
a: cost = cost * 1.05;
flag = 2;
b: bill = cost * flag;
答:
if (size > 12)
{
cost = cost * 1.05;
flag = 2;
}
bill = cost * flag;
B.
if (ibex > 14)
{
goto a;
}
sheds = 2;
goto b;
a: sheds = 3;
b: help = 2 * sheds;
答:
if (ibex > 14)
{
sheds = 3;
}
sheds = 2;
help = 2 * sheds;
C.
readin: scanf("%d", &score);
if (score < 0)
{
goto stage2;
}
count++;
goto readin;
stage2: printf("count = %d\n", count);
答:
if (score < 0)
{
printf("count = %d\n", count);
}
count++;
scanf("%d", &score);
printf("count = %d\n", count);
动动手:
0.
#include <stdio.h>
int main()
{
int year;
float lilv = 0.05;
float fishc = 10000;
double night = 10000;
for (year = 0; fishc >= night; year++)
{
fishc = fishc + 1000;
night = night * lilv + night;
}
printf("%d年后,黑夜的投资额超过小甲鱼!\n", year);
printf("小甲鱼的投资额时:%.2f\n", fishc);
printf("黑夜的投资额是:%.2lf\n", night);
return 0;
}
1.
#include <stdio.h>
int main()
{
int year = 0;
int i = 4000000;// 本金
int j = 500000;//开销
float lilv = 0.08;
while (i > 0)
{
i = i - j;
i = i * lilv + i;
year = year + 1;
}
printf("%d年后,小甲鱼败光了所有的家产,再次回到一贫如洗……", year);
return 0;
}
2.
#include <stdio.h>
int main()
{
int j;
int i = 1;
int b = 1;
float c;
float ending = 0;
for(ending = 0; 1000000 > ending * 10000000; i = -i, b = b + 2)
{
c = 1.0 / (float)b * (float)i;
ending = ending + c;
}
printf("%f", ending);
return 0;
}
3.
#include <stdio.h>
int main()
{
int month = 1;
int num1 = 2;//成熟的兔子
int num2 = 0;//第一批兔子
int num3 = 0;//第二批兔子
// int num3 = 0;//第三批兔子
while (month <= 24)
{
if(month%2!=0)
{
num1 = num1 + num2;
num2 = num1 * 2;
}
if(month%2==0)
{
num1 = num1 + num3;
num3 = num1 * 2;
}
month = month + 1;
}
printf("两年后,可以繁衍%d只兔子。", num1 + num2 + num3);
return 0;
} |
|