《C程序设计语言现代方法》第5章 编程题

1 编写一个程序,确定一个数的位数。

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int a, cnt = 0;
 6 
 7     while (scanf("%1d", &a) == 1)
 8     {
 9         cnt++;
10     }
11     printf("%d
", cnt);
12 
13     return 0;
14 }

注:在Windows下,输入完毕后先按Enter键,再按CTRL+Z键,最后再按Enter键,即可结束输入。在Linux下,输入完毕后按CTRL+D键即可结束输入。

2 编写一个程序,要求用户输入24小时制的时间,然后显示12小时制的时间格式:

Enter a 24-hour time: 21:11

Equivalent 12-hour time: 9:11 PM

注意不要把12:00显示成0:00

 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int h, m;
 6 
 7     printf("Enter a 24-hour time: ");
 8     scanf("%d:%d", &h, &m);
 9     if (h > 23 || h < 0 || m > 59 || m < 0)
10         printf("Wrong time.
");
11     else
12     {
13         printf("Equivalent 12-hour time: ");
14         if (h > 12)
15             printf("%d:%d PM
", h%12, m);
16         else
17             printf("%d:%d AM
", h, m);
18     }
19 
20     return 0;
21 }

运行结果如下:

7 编写一个程序,从用户输入的4个整数中找出最大值和最小值。

Enter four integers: 21 43 10 45

Largest: 43

Smallest: 10

要求尽可能少用if语句。

9 编写一个程序,提示用户输入两个日期,然后显示哪一个日期更早:

Enter first date (mm/dd/yy) : 3/6/08

Enter second date (mm/dd/yy) : 5/17/07

5/17/07 is earlier than 3/6/08

11 编写一个程序,要求用户输入一个两位数,然后显示该数的英文单词:

Enter a two-digit number : 45

You entered the number forty-five

原文地址:https://www.cnblogs.com/rezone/p/3207542.html