学习C(六)

练习1 求解一元二次方程

#include<stdio.h>
#include<math.h>
/*
已知方程ax2+bx+c=0的系数值(设b2-4ac>0),求方程的根。
提示:方程ax2+bx+c=0(a≠0)称为一元二次方程.
一元二次方程的基本解法有开平方法、配方法、公式法和国式分解法.  
对于方程ax2+bx+c=0(a≠0),△=b2-4ac称为该方程的根的判别式.
当△>0时,方程有两个不相等的实数根
当△=0时,方程有两个相等的实数根
当△<0时,方程无实数根.
*/
int main(){
	double a = 0;
	double b = 0;
	double c = 0;
	double x1 = 0;
	double x2 = 0;
	printf("ax^2+bx+c = 0,请输入a,b,c的值:");
	scanf("%lf %lf %lf",&a,&b,&c);
	while(getchar()!='\n');
	double delta = b*b - 4*a*c;
	if(delta>0){
		x1 = (-b+sqrt(delta))/(2*a);
		x2 = (-b-sqrt(delta))/(2*a);
		printf("该方程的两个解分别为:%g,%g\n",x1,x2);
	}else if(delta==0){
		x1 = -b/(2*a);
		printf("该方程的两个解分别为:%g,%g\n",x1,x1);
	}else{
		printf("该方程没有实数跟\n");
	}
	return 0;
}

练习2 累加

#include<stdio.h>
#include<math.h>
/*
2:求1+1/3+1/5+...之和,直到某一项的值小于pow(10,-6)时停止累加
*/
int main(){
	int i = 0;
	double res = 0;
	for(i=1;1;i+=2){
		if(1.0/i < pow(10,-6)){break;}
		res = res +1.0/i;
	}
	printf("%lf\n",res);
	return 0;
}

练习3 大小写转换

#include<stdio.h>
#include<math.h>
/*
从键盘输入一行字符,若为小写字母,则转化为大写字母;若为大写字母,则转化为小写字母;否则转化为ASCII码表中的下一个字符
*/
int main(){
	char ch = 0;
	while(1){
		scanf("%c",&ch);
		if(ch=='\n'){
			printf("\n");
			break;
		}else if(ch>='A' && ch<='Z'){
			printf("%c",ch+32);
		}else if(ch>='a' && ch<='z'){
			printf("%c",ch-32);
		}else{
			printf("%c",ch+1);
		}
	}
	return 0;
}

练习4 求前n项之和

#include<stdio.h>
#include<math.h>
/*
	已知序列1/2,2/3,3/5,5/8,...,求其前20项之和
*/
int main(){
	double fm = 2;//分母
	double fz = 1;//分子
	double temp = 0;//用来储存数据的中间变量
	double res = 0;
	int i = 0;
	for(i=0;i<20;i++){
		res = res + fz/fm;
		temp = fz;
		fz = fm;
		fm = fm + temp;
	}
	printf("%g\n",res);
	return 0;
}

练习5 求数据类型的取值范围

#include<stdio.h>
#include<math.h>
/*
	使用循环,求出char和short数据类型的取值范围
*/
int main(){
	short a = 0;
	short b = a+1;
	while(a<b){
		a++;
		b++;
	}
	printf("short的取值范围为:%d~%d\n",b,a);
	return 0;
}

练习6 求球的体积和表面积

#include<stdio.h>
#include<math.h>
/*
	从键盘输入球体的半径,求其体积和表面积。
	S(球面)=4πR^2 (^2表示平方)
 	V 球=4/3 π r^3
*/
#define PI 3.141592654
int main(){
	double r = 0;
	printf("请输入球体的半径R:");
	scanf("%lf",&r);
	double S = 4*PI*r*r;
	double V = 4/3.0*PI*r*r*r;
	printf("表面积为:%g,体积为:%g\n",S,V);
	return 0;
}

练习7 键盘输入一个小于60的整数,输出属于几刻

#include<stdio.h>
#include<math.h>
/*
	从键盘输入一个小于60的整数,输出现在他属于几刻(每15分为1刻),使用判断以及运算符两种方式
*/
int main(){
	int time = 0;
	int res = 0;
	printf("请输入时间,注意不大于60:");
	while(1){
		scanf("%d",&time);
		if(time<=60){break;}
	}
	/*if(time>0 && time<=15){
		res = 1;
	}else if(time>15 && time<=30){
		res = 2;
	}else if(time>30 && time<=45){
		res = 3;
	}else{
		res = 4;
	}*/
	res = (time>0) + (time>15) + (time>30 ) + (time>45);
	printf("当前属于%d刻\n",res);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值