C语言成长学习题(五)

十七、求一元二次方程ax2+bx+c=0的实根(要求a、b、c的值从键盘输入,a!=0)。

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 void main(void)
 5 {
 6     int a, b, c;
 7     float delta, x1, x2;
 8     
 9     printf("Input a, b, c:
");
10     scanf("%d%d%d", &a, &b, &c);
11     delta = b * b - 4 * a * c;
12     if(delta < 0)
13         printf("No real root.
");
14     else
15     {
16         x1 = (-b + sqrt(delta)) / (2*a);
17         x2 = (-b - sqrt(delta)) / (2*a);
18         printf("x1 = %f, x2 = %f
", x1, x2);
19     }
20 }

 结果:

1.Input a, b, c:

 4 8 1

 x1 = 2.118034, x2 = -0.118034

2.Input a, b, c:

 2 4 3

 No real root.

3.Input a, b, c:

 4 12 9

 x1 = 1.500000, x2 = 1.500000

Mark:

  如果程序使用数学库函数,则必须在程序的开头加命令行#include <math.h>

十八、编写程序实现以下功能:输入某年的年份,判断是不是闰年。

 1 #include <stdio.h>
 2 
 3 void main(void)
 4 {
 5     int year, flag;
 6 
 7     scanf("%d", &year);
 8     if (year % 400 == 0)
 9         flag = 1;
10     else
11     {
12         if (year % 4 != 0)
13             flag = 0;
14         else
15         {
16             if (year % 100 != 0)
17                 flag = 1;
18             else
19                 flag = 0
20         }
21     }
22     if (flag == 1)
23         printf("%d is a leap year.
", year);
24     else
25         printf("%d is not a leap year.
", year);
26 }

十九、编写求下面分段函数值的程序,其中x的值从键盘输入。

 1 #include <stdio.h>
 2 {
 3     float x, y;
 4 
 5     printf("Input data:
");
 6     scanf("%f", &x);
 7     if (x < 0)
 8         y = 0;
 9     else if (x < 10)
10         y = x * x * x + 5;
11     else if (x < 20)
12         y = 2 * x * x - x - 6;
13     else if (x < 30)
14         y = x * x + 1;
15     else
16         y = x + 3;
17     printf("x = %f, y = %f
", x, y);
18 }

二十、分析下面程序,观察其输出结果。

 1 #include <stdio.h>
 2 
 3 void main(void)
 4 {
 5     int a;
 6 
 7     scanf("%", &a);
 8     switch(a)
 9     {
10         case 1: printf("A");
11         case 2: printf("B");
12         case 3: printf("C"); break;
13         default: printf("D");
14     }
15 }

结果:

(1)1

  ABC

(2)2

  BC

(3)3

  C

(4)4

  D

原文地址:https://www.cnblogs.com/zero-jh/p/5023319.html