您的位置:首页 > 编程语言 > C语言/C++

C语言成长学习题(五)

2015-12-06 12:15 288 查看
十七、求一元二次方程ax2+bx+c=0的实根(要求a、b、c的值从键盘输入,a!=0)。

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

void main(void)
{
int a, b, c;
float delta, x1, x2;

printf("Input a, b, c:\n");
scanf("%d%d%d", &a, &b, &c);
delta = b * b - 4 * a * c;
if(delta < 0)
printf("No real root.\n");
else
{
x1 = (-b + sqrt(delta)) / (2*a);
x2 = (-b - sqrt(delta)) / (2*a);
printf("x1 = %f, x2 = %f\n", x1, x2);
}
}


结果:

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>

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

#include <stdio.h>

void main(void)
{
int year, flag;

scanf("%d", &year);
if (year % 400 == 0)
flag = 1;
else
{
if (year % 4 != 0)
flag = 0;
else
{
if (year % 100 != 0)
flag = 1;
else
flag = 0
}
}
if (flag == 1)
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
}


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



#include <stdio.h>
{
float x, y;

printf("Input data:\n");
scanf("%f", &x);
if (x < 0)
y = 0;
else if (x < 10)
y = x * x * x + 5;
else if (x < 20)
y = 2 * x * x - x - 6;
else if (x < 30)
y = x * x + 1;
else
y = x + 3;
printf("x = %f, y = %f\n", x, y);
}


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

#include <stdio.h>

void main(void)
{
int a;

scanf("%", &a);
switch(a)
{
case 1: printf("A");
case 2: printf("B");
case 3: printf("C"); break;
default: printf("D");
}
}


结果:

(1)1

  ABC

(2)2

  BC

(3)3

  C

(4)4

  D
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: