c语言编程题y= ax,C语言几道编程题

本文展示了多个C语言编程实例,包括求解一元二次方程的根、判断三角形的存在及其面积、识别闰年、数组排序、打印九九乘法表、实现递归算法、电文加密解密以及计算素数。这些实例涵盖了基础数学计算、条件判断、循环、递归等编程概念,体现了C语言在算法和实用编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.求一元二次方程ax2+bx+x=0 的根,系数a,b,c从终端输入。应考虑实根、复根和无解的情况。

#include

#include

void main(void)

{

double a,b,c,s1,s2,m,n;

scanf("%lf%lf%lf",&a,&b,&c);

if(a==0)

printf("error,please input again!");

else if

((b*b-4*a*c)>=0)

s1=(-b+sqrt(b*b-4*a*c))/(2*a),s2=(-b-sqrt(b*b-4*a*c))/(2*a),

printf("s1=%lf,s2=%lf\n",s1,s2);

else

m=(-b)/(2*a),n=sqrt(4*a*c-b*b)/(2*a),

printf("s1=%lf+%lfi,s2=%lf-%lfi\n",m,n,m,n);

}

2.输入三角形三条边,判断是否满足三角形条件,若满足求其面积,否则输出出错信息。

#include

#include

void main()

{

float

a,b,c,p;

printf("请输入三角形三边:");

scanf("%f%f%f",&a,&b,&c);

p=(a+b+c)/2;

if (a+b>c &&

a+c>b &&

b+c>a)

printf("%f\t%f\t%f能构成三角形\n,三角形的面积=%.2f\n",a,b,c,sqrt(p*(p-a)*(p-b)*(p-c))); else

printf("不能构成三角形");

}

3.判断某一年是否是闰年

#include"stdio.h"

main()

{

int year,leap;

scanf("%d",&year);

if(year%4==0)

{

if(year%100==0)

{

if(year%400==0)

leap=1;

else

leap=0;

}

else

leap=1;

}

else

leap=0;

if(leap)

printf("%d is",year);

else

printf("%d is not",year);

printf(" a leap year.\n");

}

4.3个数由小到大排序

#include

main()

{

int a, b, c,

t; scanf("%d%d%d", &a, &b,

&c); if (a

>

b) {

t = a;

a = b;

b = t;

}

if (a

>

c) {

t = a;

a = c;

c = t;

}

if (b

>

c) {

t = b;

b = c;

c = t;

}

printf("the

order of the number is:\n");

printf("%d,%d,%d", a, b,

c); }

5.九九乘法表

#include"stdio.h"

void main()

{

int i,j;

for(i=1;i<=9;i++)

{

for(j=1;j<=i;j++)

printf("%d*%d=%-3d",i,j,i*j);

printf("\n");

}

}

我在TC下执行的结果是:

1*1=1

2*1=2 2*2=4

3*1=3 3*2=6 3*3=9

4*1=4 4*2=8 4*3=12 4*4=16

5*1=5 5*2=10 5*3=15 5*4=20 5*5=25

6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36

7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42

7*7=49

8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56

8*8=64

9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63

9*8=72 9*9=81

6.输入两个正整数m和n,求它们的最大公约数和最小公倍数

#include "stdio.h"

int main()//该程序可以扩展为求多个数的最大公约数和最小公倍数

{long m,n,big,small,r,i;

printf("Input 2 number:\n");//输入任意两个数

loop:scanf("%ld%ld",&m,&n);

if(m<0||n<0)//要求输入的两个数必须大于0,否则重新输入

{printf("Error! Enter m and n again.\n");

goto loop;

}

else {if(m>n){big=m;small=n;}

else{big=n;small=m;}//确保small存放较小的数

for(i=1;i<=small;i++)//循环求解最小公约数r,最多循环small次

{if(big%i||small%i)//两个数都能被i整除,把i的值赋予r

continue;

r=i;}//r的值可以有多个,最大的不能超过small

printf("Greatest common divisor:%ld\n",r);//输出最大公约数

printf("Lease common multiple:%ld\n",m*n/r);//输出最小公倍数

}

return 0;

}

7.译电文。为了使电文保密,往往按一定规律将其转换成密码,收报人再按规律译回原文。比如按照以下规律:将字母A变成E,即变成其后的第四个字母,相应的最后四个字母W变成A,X变成B,Y变成C,Z变成D,小写字母也是同样。

#include

void main()

{

char

c;

while((c =

getchar()) != '\n') { if((c >= 'a' && c

<= 'z') || (c >= 'A'

&& c <= 'Z'))

{

c = c + 4;

if((c > 'Z' &&

c<= 'Z'+4 || c > 'z'))

c = c - 26; } printf("%c",c); }

printf("\n");

}

8.兔子繁殖问题

#include

long f(long n)

{

if(n<3) return 1;

else return f(n-2)+f(n-1);

}

void main()

{

printf("一年后,有兔子%ld对.\n", f(12));

getch();

}

9.输出200~300之间所有的素数~!

#include

void main()

{

int i,k;

for (i=200;i<300;i++)

for(k=2;k

{

if(i%k==0)

break;

if(i==k+1)

printf("%d ",i);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值