蓝桥杯练习

蓝桥杯

1.ASCII码排序


Problem Description

输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。

Input

输入数据有多组,每组占一行,有三个字符组成,之间无空格。

Output

对于每组输入数据,输出一行,字符中间用一个空格分开。

Sample Input

qwe

asd

zxc

Sample Output

e q w

a d s

c x z

代码:

#include <stdio.h>
int main(){
	char x,y,z,temp;
	while(scanf("%c%c%c",&x,&y,&z)!=EOF){
		getchar();
		if(x>y){
			temp=x;
			x=y;
			y=temp;
		}
		if(x>z){
			temp=x;
			x=z;
			z=temp;
		}
		if(y>z){
			temp=y;
			y=z;
			z=temp;
		}
		printf("%c %c %c",x,y,z);
	}
	return 0;
} 


注:

1.x>y x>z y>z 按顺序来

2.getchar()用来接收空格符

2.计算两点间的距离

Problem Description

输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。

Input

输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。

Output

对于每组输入数据,输出一行,结果保留两位小数。

Sample Input

 

0 0 0 1 0 1 1 0

Sample Output

 

1.00 1.41

代码:

#include <stdio.h>
#include <math.h>
int main(){
	double x1,y1,x2,y2;
	double d;
	while(scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2)!=EOF){
		d=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
		printf("%.2lf",d);
	}
	return 0;
}

注:

1》x1,x2,y1,y2最好也要用double定义

2》两点距离公式:

-        根号:sqrt()

-        平方:pow(x,a)

-        绝对值:abs()

3》注意头文件导math

3.计算球体积

Problem Description

根据输入的半径值,计算球的体积。

Input

输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。

Output

输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。

Sample Input

1

1.5

Sample Output

4.189

14.137

代码:

#include <stdio.h>
#include <math.h>
#define PI 3.141526
int main(){
	double r,v;
	while(scanf("%lf",&r)!=EOF){
		v=4.0/3.0 * PI * pow(r,3);
		printf("%.3lf",v);
	}
	return 0;
} 

注:

1》球体积公式:

2》宏定义:#define PI 3.141526    //后面不加分号

4.求绝对值

Problem Description

求实数的绝对值。

Input

输入数据有多组,每组占一行,每行包含一个实数。

Output

对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。

Sample Input

123

-234.00

Sample Output

123.00

234.00

代码:

方法一:用fabs函数

#include <stdio.h>
#include <math.h>
int main(){
	double x;
	while(scanf("%lf",&x)!=EOF){
		x=fabs(x);
		printf("%.2lf",x);
	}
	return 0;
}

方法二:

#include <stdio.h>
#include <math.h>
double fabs(double a){
	double x;
	if(a>=0)  x=a;
	else x=-a;
	return x;
}
int main(){
	double b,f;
	while(scanf("%lf",&b)!=EOF){
		printf("%.2lf",fabs(b));
	}
	return 0;
}

注:绝对值函数用fabs而不是abs,因为此时的数据类型为double

5.成绩转换

Problem Description

输入一个百分制的成绩t,将其转换成对应的等级,具体转换规则如下:
90~100为A;
80~89为B;
70~79为C;
60~69为D;
0~59为E;

Input

输入数据有多组,每组占一行,由一个整数组成。

Output

对于每组输入数据,输出一行。如果输入数据不在0~100范围内,请输出一行:“Score is error!”。

Sample Input

56

67

100

123

Sample Output

E

D

A

Score is error!

代码 :

#include <stdio.h>
int main(){
	int score;
	while(scanf("%d",&score)!=EOF){
		if(score>0 && score<=100){
			if(score>=90 && score<=100) printf("A");
			if(score>=80 && score<=89) printf("B");
			if(score>=70 && score<=79) printf("C");
			if(score>=60 && score<=69) printf("D");
			if(score>=0 && score<=59) printf("E");
		}
		else printf("Score is error!");
	}
	return 0;
}

第几天?

Problem Description

给定一个日期,输出这个日期是该年的第几天。

Input

输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。

Output

对于每组输入数据,输出一行,表示该日期是该年的第几天。

Sample Input

1985/1/20

2006/3/12

Sample Output

20

71

代码:

#include <stdio.h>
int main(){
	int year,month,day,sum;
	int a[31]={31,28,31,30,31,30,31,31,30,31,30,31};
	while(scanf("%d/%d/%d",&year,&month,&day)!=EOF){
		sum=0; 
		if(year%4!=0 && year%100==0 || year%400==0){
			a[1]=29;
		}
		else a[1]=28;
		for(int i=0;i<month-1;i++){
			sum+=a[i];
		}
		printf("%d",sum+day);	
	}
}

注意:

1》//sum要在循环内初始为0,保证每次循环sum都为0

2》判断平闰年: 年份对4取余不为0且对100取余为0,或者对400取余为0

3》判断二月份是否为28/29天时,不要写成a[i]+=1;因为这样每次闰年a[i]都会加1,直接写为=29/28天即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值