明解C語言 入門篇 第三章答案

 

練習3-1spa

 1 #include <stdio.h>
 2 
 3 int main() {
 4     int x;
 5     int y;
 6     puts("請輸入兩個整數。");
 7     printf("整數A:");
 8     scanf("%d", &x);
 9     printf("整數B:");
10     scanf("%d", &y);
11     if (x % y)
12         puts("B不是A的約數。");
13     else
14         puts("B是A的約數。");
15     return 0;
16 }

 

練習3-2code

依然能夠運行blog

練習3-3it

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int x;
 6 
 7     printf("請輸入一個整數:");
 8     scanf("%d", &x);
 9     if (x < 0)
10         printf("絕對值是%d", -x);
11     else
12         printf("絕對值是%d", x);
13 
14     
15 
16     return 0;
17 }

練習3-4io

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int x;
 6     int y;
 7 
 8     puts("請輸入兩個整數:");
 9     printf("整數A:");
10     scanf("%d", &x);
11     printf("整數B:");
12     scanf("%d", &y);
13 
14     if (x < y)
15         printf("A大於B");
16     else if (x = y)
17         printf("A等於B");
18     else
19         printf("B大於A");
20 
21     
22 
23     return 0;
24 }

練習3-5class

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int x;
 6     x = 3;
 7     int y;
 8     y = 4;
 9 
10      
11     printf("%d\n", (x < y));
12     printf("%d\n", (x > y));
13     printf("%d\n", (x == y));
14     printf("%d\n", (x != y));
15 
16 
17 
18 
19     return 0;
20 }

練習3-6di

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int x;
 6     int y;
 7     int z;
 8     printf("整數1:");
 9     scanf("%d", &x);
10     printf("整數2:");
11     scanf("%d", &y);
12     printf("整數3:");
13     scanf("%d", &z);
14     if (x < y&&x<z)
15         printf("%d",x);
16     if (y<z && y<x)
17         printf("%d",y);
18     if (z<x && z<y);
19     printf("%d",z);
20 
21 
22 }

練習3-7co

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int x;
 6     int y;
 7     int z;
 8     int m;
 9     printf("整數1:");
10     scanf("%d", &x);
11     printf("整數2:");
12     scanf("%d", &y);
13     printf("整數3:");
14     scanf("%d", &z);
15     printf("整數4:");
16     scanf("%d", &m);
17 
18     int max1;
19     int max2;
20     max1 = (x > y) ? x : y;
21     max2 = (z > m) ? z : m;
22     if (max1 > max2)
23         printf("%d", max1);
24     else
25         printf("%d", max2);
26 
27 
28 }

練習3-8cas

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int x, y;
 6     
 7     puts("請輸入兩個整數。");
 8     printf("整數1:");
 9     scanf("%d", &x);
10     printf("整數2:");
11     scanf("%d", &y);
12     
13     if (x > y)
14         printf("它們的差是%d", x - y);
15     if (y > x)
16         printf("它們的差是%d", y - x);
17 
18 }

練習3-9return

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int x;
 6     int y;
 7     int z;
 8     printf("整數1:");
 9     scanf("%d", &x);
10     printf("整數2:");
11     scanf("%d", &y);
12     printf("整數3:");
13     scanf("%d", &z);
14     
15     int min1, min2;
16     min1 = (x < y) ? x : y;
17     min2 = (z < y) ? z: y;
18     printf("最小值是:%d", (min1 < min2) ? min1 : min2);
19 
20 
21 }

 

練習3-10

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int x;
 6     int y;
 7     int z;
 8     printf("整數A:");
 9     scanf("%d", &x);
10     printf("整數B:");
11     scanf("%d", &y);
12     printf("整數C:");
13     scanf("%d", &z);
14     
15     if (x == y && y == z)
16         puts("有三個值相等");
17     else if (x == y || x == z || z == y)
18         puts("有兩個值相等");
19     else
20         puts("沒有值相等");
21 
22 
23 
24 
25 }

練習3-11

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int x;
 6     int y;
 7     printf("整數A:");
 8     scanf("%d", &x);
 9     printf("整數B:");
10     scanf("%d", &y);
11 
12     
13     if (x - y >= 10 || y - x >= 10)
14         puts("它們的差大於等於10");
15     else
16         puts("它們的差小於等於10");
17 
18 
19 
20 }

 

練習3-12

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int x;
 6     
 7     printf("請輸入一個整數:");
 8     scanf("%d", &x);
 9     switch (x % 2) {
10     case 1:puts("該整數是奇數"); break;
11     case 0:puts("該整數是偶數"); break;
12 
13 
14      }
15 
16 }

 練習3-13

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int mouth;
 6     
 7     printf("請輸入月份:");
 8     scanf("%d", &mouth);
 9     switch (mouth)
10         {
11     case 3:
12     case 4:
13     case 5: puts("");  break;
14 
15 
16     case 6:; 
17     case 7:
18     case 8:puts(""); break;
19 
20     case 9:
21     case 10:
22     case 11: puts(""); break;
23 
24     default:puts("");
25 
26 
27      }
28 
29 }
相關文章
相關標籤/搜索