Sleyar 发表于 2022-2-28 22:28:34

#include <stdio.h>
#include <math.h>
int main()
{
                int a;
                float b , c ;
                for (a = 0; b >= c; a++)
                {
                                b = 10000 + 0.1 * 10000 * a;
                                c = 10000 * pow((1 + 0.05), a);
                }
                printf("%d年后,黑夜的投资额超过小甲鱼!\n", a - 1);
                printf("小甲鱼的投资额是:%.2f\n", b);
                printf("黑夜的投资额是:%.2f\n", c);
                return 0;
}

#满楼红袖招# 发表于 2022-3-1 09:23:19

1

乙烯依旧 发表于 2022-3-1 10:48:43

0. 请问下边代码将打印多少个 'A'?

```c
#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;
}
```

```
10次
```



\1. 请问下边代码会打印多少个 'B'?

```c
#include <stdio.h>

int main()
{
      int i = 0;

      while (i++)
      {
                if (i > 10)
                {
                        goto Label;
                }
                putchar('B');
      }

Label:putchar('\n');

      return 0;
}
```

```
11次
```



\2. 请写出表达式 a = b = c = 5 中的"l-value"?

```
c=5
b=c
a=b
c、b、a依次为i-value
```

\3. 请问表达式 a = (b = 3, 4, c = b++ + 5, ++c + ++b); 执行后,整型变量 a、b、c 的值是?

```
c=10,b=4,a=14
```

\4. 请使用条件运算符求出变量 x 的绝对值,并存放到变量 z 中。

```
x = -a > a? -a; a
```

\5. C 语言其实在大部分情况下不使用 goto 语句也能做得很好,请尝试将下列代码段写成不带 goto 语句的版本。

A. 版权属于:(http://bbs.fishc.com)

```
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. 版权属于:(http://bbs.fishc.com)

```
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.版权属于:(http://bbs.fishc.com)

```
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);
```

**动动手:**

假设小甲鱼和黑夜手上均有 10000 元,小甲鱼以 10% 的单利息投资,黑夜则以每年 5% 的复合利息投资。请编写一个程序,计算需要多少年黑夜手头的 Money 才会超过小甲鱼?

提示:单利息即每年的利息均为投资额 * 利率;复合利息则是每年的本息所得作为第二年的投资额。

程序实现如图所示:

!(https://xxx.ilovefishc.com/forum/201603/20/032234sqz71zhcptyf1qvc.png)

```c
#include <stdio.h>

int main()
{
      int i;
      double xiaojiayu,heiye;
      xiaojiayu = 10000;
      heiye = 10000;

      for (i = 0; xiaojiayu >= heiye; i++ )
      {
                xiaojiayu += 10000 *0.1;
                heiye *= 1.05;
      }

      printf("%d年后,黑夜的投资额超过小甲鱼!\n",i);
      printf("小甲鱼的投资额是:%.2f\n",xiaojiayu);
      printf("黑压的投资额是:%.2f\n",heiye);


      return 0;
}

```



\1. 都说天降横财不是什么好事儿,这不,小甲鱼中了双色球一等奖,扣除税收后还剩下 400 万人民币。假设小甲鱼把这些钱做固定投资,每年可以获得 8% 的红利,但在每年的第一天,小甲鱼都会取出 50 万作为本年度的开销……

请编写一个程序,计算需要多久小甲鱼才会败光所有家产,再次回到一贫如洗?(本故事纯属虚构,小甲鱼还是需要大家的支持才能养活自己滴,点击购买 VIP 支持小甲鱼 -> [传送门](http://fishc.taobao.com))

!(https://xxx.ilovefishc.com/forum/201603/20/032309t0niicdikl0yxb3v.png)

```c
#include <stdio.h>

int main()
{
      float xiaojiayu = 4000000;
      int i;

      for (i = 0; xiaojiayu > 0; i++)
      {
                xiaojiayu *= 1.08;
                xiaojiayu -= 500000;
      }

      printf("%d年后,小甲鱼败光了所有的家产,再次回到一贫如洗....\n");

      return 0;
}

```



\2. 根据以下已知公式求 Pi 的近似值,要求正确计算出小数点后前 7 位(即3.1415926)。

!(https://xxx.ilovefishc.com/forum/201603/20/033142z95n4rht2sr4s35m.png)

提示1:分子永远都是1;
提示2:后一项的分母是前一项的分母加2;
提示3:可以通过i = -i实现符号取反的效果;
提示4:要正确计算出小数点后前 7 位数,最后一项的绝对值需要小于 10^(-8) 才行(该项不累加);
提示5:求某数的绝对值可以使用 fabs 函数(函数快查:(http://bbs.fishc.com/thread-70132-1-1.html)

```c
#include <stdio.h>
#include <math.h>

int main()
{

      int fenzi, num, fenmu;
      double temp,result = 0;

      for (fenzi = 1, num = 1, fenmu = 1 ; fabs(fenzi / fenmu) < 0.00000001; num++ , fenmu+=2)
      {
                if (num % 2 != 0)
                {
                        result += (fenzi / fenmu);
                }
                else
                {
                        result -= (fenzi/fenmu);
                }
      }

      printf("结果:%.7f\n",result);


      return 0;
}

```

\3. 这是一个有趣的古典数学问题:如果说兔子在出生两个月后,就有繁殖能力,在拥有繁殖能力之后,这对兔子每个月能生出一对小兔子来。假设所有兔子都不会死去,能够一直干下去,那么两年之后可以繁殖多少对兔子呢?

!(https://xxx.ilovefishc.com/forum/201603/20/032407qyz1y3dqquq6qqof.png)

吾城北徐公 发表于 2022-3-1 11:11:26

加油

3two 发表于 2022-3-1 19:48:12

{:5_91:}

q1242598090 发表于 2022-3-1 19:51:28

22222

琈笙物语 发表于 2022-3-2 00:02:55

1

Anthony7 发表于 2022-3-2 01:01:54

111

琈笙物语 发表于 2022-3-2 01:36:02

onetree666 发表于 2022-3-2 13:40:37

1

448432341 发表于 2022-3-2 15:09:54

0.10次
1.9次
2.5?
3.b=3 c=10,a=13
4.?
5.

雨停了天晴了 发表于 2022-3-2 17:06:06

牛蛙

guocheng 发表于 2022-3-2 18:13:36

11111111111111111

a83711140 发表于 2022-3-3 00:19:05

看看

LZ学C 发表于 2022-3-3 09:59:32

答案

计算机学徒 发表于 2022-3-3 12:13:00

加油

榴莲一定要学好 发表于 2022-3-3 15:20:07

不知道

534492 发表于 2022-3-3 15:36:45

1

太郎 发表于 2022-3-3 19:12:38

1

取名小废材 发表于 2022-3-3 20:35:31

0、100个
1、11个
2、a、b、c
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;
}
sheds = 2;
help = 2 * sheds;

C:
while((scanf("%d", &score))==1)
if (score < 0)
{
      printf("count = %d\n", count);
}
count++;
}

0、
#include<stdio.h>
#include<math.h>
int main()
{
        float total1=10000.00;
        float total2=10000.00;
        int year;
        float r1=0.1,r2=0.05;
        float r=total1*r1;
        for(year=0;total1>=total2;year++)
        {
                total1=total1+r;
                total2=total2+total2*r2;
        }
        printf("%d年后,黑鱼的投资额超过小甲鱼!\n",year);
        printf("小甲鱼的投资额是:%.2f\n",total1);
        printf("黑鱼的投资额是:%.2f\n",total2);
       
        return 0;
}

1、#include<stdio.h>
int main()
{
        long long sum=4000000,i=500000;
        int year;
        for(year=0;sum>0;year++)
        {
                sum=(sum-i)*(1+0.08);
        }
        printf("%d年后,小甲鱼败光了所有的家产,再次回到一贫如洗……",year);
        return 0;
}

2、
#include<stdio.h>
#include<math.h>
int main()
{
        float Pi,p=1,i=1,j=1,sum=0;
        while(p>=0.00000001)
        {
                p=1/j;
                sum=sum+i*p;
                i=-i;
                j+=2;
        }
        Pi=sum*4;
        printf("Pi=%.7f",Pi);
        return 0;
}

3、#include<stdio.h>
int main()
{
        int month;
        long long count=1,temp;
        for(month=2;month<=24;month++)
        {
                temp=count;
                count=count+temp;
        }
        printf("两年之后可以繁殖%lld对兔子。\n",count);
        return 0;
}
页: 207 208 209 210 211 212 213 214 215 216 [217] 218 219 220 221 222 223 224 225 226
查看完整版本: S1E16:拾遗 | 课后测试题及答案