杭电oj刷题第一阶段答案

输入输出练习

这里贴上了我自己当时练习杭电oj时的代码和解题思路,必要的题目我会详细的写明需要注意的问题和每一步我的想法

1000 A+B

这道题只要注意EOF的用法就可以了,因为程序中并没有说明有多少数据,EOF可以帮助我们判断是否已经到了末尾。

#include <stdio.h>

int main(){
   
	int a = 0,b = 0;
	while(scanf("%d%d",&a,&b)!=EOF)//只要注意这种写法就可以了,在oj里面很常用
		printf("%d\n",a+b);
	return 0;
}

1089 A+B for Input-Output Practice (I)

#include <stdio.h>

int main(){
   
	int a = 0,b = 0;
	while(scanf("%d%d",&a,&b)!=EOF){
   
		printf("%d\n",a+b);
	}
	return 0;
}

1090 A+B for Input-Output Practice (II)

#include <stdio.h>

int main(){
   
	int N;
	scanf("%d",&N);
	int a,b;
	while(N--){
   
		scanf("%d%d",&a,&b);
		printf("%d\n",a+b);
	}
	return 0;
}

1091 A+B for Input-Output Practice (III)

#include <stdio.h>

int main(){
   
	int a = 0,b = 0;
	while(scanf("%d%d",&a,&b)!=EOF && ((a != 0)||(b != 0))){
   
		printf("%d\n",a+b);
	}
	return 0;
}

1092 A+B for Input-Output Practice (IV)

#include <stdio.h>

int main(){
   
	int n = 0,a = 0,sum = 0;
	while(scanf("%d",&n) != EOF&&(n!=0)){
   
		sum = 0;
		while(n--){
   
			scanf("%d",&a);
			sum += a;
		}
		printf("%d\n",sum);
	}
	return 0;
}

1093 A+B for Input-Output Practice (V)

#include <stdio.h>

int main(){
   
	int N = 0;
	int n = 0;
	int a = 0;
	int sum = 0;
	scanf("%d",&N);
	while(N--){
   
		scanf("%d",&n);
		sum = 0;
		while(n--){
   
			scanf("%d",&a);
			sum+=a;
		}
		printf("%d\n",sum);
	}
	return 0;
}

1094 A+B for Input-Output Practice (VI)

#include <stdio.h>

int main(){
   
	int n = 0;
	int a = 0;
	int sum = 0;
	while(scanf("%d",&n)!=EOF){
   
		sum = 0;
		while(n--){
   
			scanf("%d",&a);
			sum+=a;
		}
		printf("%d\n",sum);
	}
	return 0;
}

1095 A+B for Input-Output Practice (VII)

#include<stdio.h>

int main(){
   
	int a = 0,b = 0;
	while(scanf("%d%d",&a,&b)!=EOF){
   
		printf("%d\n\n",a+b);
	}
	return 0;
}

1096 A+B for Input-Output Practice (VIII)

#include <stdio.h>

int main(){
   
	int N = 0,M = 0;
	int a = 0,sum = 0;
	scanf("%d",&N);
	while(N--){
   
		scanf("%d",&M);
		sum = 0;
		while(M--){
   
			scanf("%d",&a);
			sum += a;
		}
		if(N!=0){
   //如果不是最后一组数据就输出带一个空行的结果
			printf("%d\n\n",sum);
		}else{
   //是最后一组数据单输出结果即可
			printf("%d\n",sum);
		}
	}
	return 0;
}

1001 Sum Problem

这个题稍微需要动一下脑子,题目本来的意思是每输入一个数,输出当前已经输入的所有数据的和,我说的动一下脑子指的是如果你有追求效率的心的话,没有那就别管了,我当时做法是避免二重循环效率低,所以打算采用 sum = n * (n + 1) / 2来做,但是很不幸,溢出了,题目只保证了结果会是int类型范围内,但是中间数据可不保证所以你可以采用下面这种做法,也可以换一个思路 sum = n / 2 * (n + 1) 这样就可以了

# include<stdio.h>

int main()
{
   	
	int n;
	while(scanf("%d",&n)!=EOF)
	{
   
		int sum=0;
		for(int i=0;i<=n;i++)
		{
   
			sum+=i;
		}
	printf("%d\n\n",sum);
	}
	return 0;
}

简单操作

2000 ASCII码排序

#include<stdio.h>
#include<string.h>
int main(){
   
	char a,b,c;
	while(scanf("%c %c %c",&a,&b,&c)!=EOF){
   
		getchar();//接收换行,不然下一次的数据可能会有问题
		char t;
		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("%c %c %c\n",a,b,c);
	}
}

2001 计算两点间的距离

#include <stdio.h>
#include <math.h>//为了使用开根函数

int main(){
   
	double x1,y1,x2,y2;//首先要注意题目中说明了输入数据会是实数
	double distance = 0;
	while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2)!=EOF){
   
		distance = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
		printf("%.2lf\n",distance);//注意保留两位小数的写法
	}
	return 0;
}

2002 计算球体积

这里只需要注意一下球的体积计算公式就可以了: 4 π r 3 3 \frac{4πr^3}{3} 34πr3

#include <stdio.h>
#define PI 3.1415927
int main(){
   
	double r;
	double tiji;//英文学的不好,体积是啥自己查
	while(scanf("%lf",&r)!=EOF){
   
		tiji = 4 * PI * r * r * r / 3.0;//这里为了以防万一我用的3.0来求精度
		printf("%.3lf\n",tiji);
	}
	return 0;
}

2003 求绝对值

#include <stdio.h>

int main(){
   
	double x;
	while ((scanf("%lf", &x))!= EOF)
		printf("%.2f\n", (x > 0) ? x : -x);
	return 0;
}

2004 成绩转换

#include <stdio.h>

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

2005 第几天?

这里首先要知道闰年的判断条件:能被4整除并且不能被100整除或者能被400整除

#include <stdio.h>

int main(){
   
	int day[12] = {
   31,28,31,30,31,30,31,31,30,31,30,31};
	int year,month,date;
	int num = 0;
	while(scanf("%d/%d/%d",&year,&month,&date)!=EOF){
   //输入要随机应变
		num = 0;
		if((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))){
   
			day[1] = 29;
		}else{
   
			day[1] = 28;
		}
		for(int i = 0;i < month - 1;i++){
   //要计算到前一个月
			num += day[i];
		}
		num += date;//加上本月的天数
		printf("%d\n",num);
	}
	return 0;
}

2006 求奇数的乘积

#include <stdio.h>

int main(){
   
	int n,a,result;
	while(scanf("%d",&n)!=EOF){
   
		result = 1;//因为是成绩所以要用1进行初始化
		while(n--){
   
			scanf("%d",&a);
			if(a % 2 != 0)
				result *= a;
		}
		printf("%d\n",result);
	}
	return 0;
}

2007 平方和与立方和

#include<stdio.h>

int main(){
   
	int m,n;
	int x,y;
	while(scanf("%d%d",&m,&n)!=EOF)<
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值