HDOJ 十天刷题及答案 c++

HDOJ10天刷题顺序
每天四五道(1.1代表第一天第一道),从易到难
分享一下我的结果,都AC了,欢迎提意见哦~

1.1.HDOJ 1000

Problem Description

Calculate A + B.

Input

Each line will contain two integers A and B. Process to end of file.

Output

For each case, output A + B in one line.

Sample Input

1 1

Sample Output

2
#include <iostream>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
   
	int a;
	int b;
	while(cin>>a>>b){
   	//注意题目的each line
	cout<<a+b<<endl;	//endl丢失出现PE
	}
	return 0;
}

1.2.HDOJ 1089

Input

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

1 5
10 20

Sample Output

6
30
#include <iostream>
using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
   
   int a,b;
   while(cin>>a>>b){
   
   	cout<<a+b<<endl; 
   }
   return 0;
}

1.3.HDOJ1096

Input

Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.

Output

For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.

Sample Input

3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3

Sample Output

10

15

6
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
   
	int N,M,num;
	cin>>N;
	int sum=0;
	while(N--){
   
		cin>>M;
		sum=0;
		while (M--){
   
			cin>>num;
			sum+=num;
		}
		if(N!=0){
   
		cout<<sum<<endl;    //注意每行输出之间要带空行
		cout<<endl;	
		}
		else 
		{
   
		cout<<sum<<endl;	//最后一行的输出不用再次回车
		}
	}
	return 0;
}

1.4.HDOJ1001

Input

The input will consist of a series of integers n, one integer per line.

Output

For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.

Sample Input

1
100

Sample Output

1

5050
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
   
	int num,sum;
	while(cin>>num){
   
		sum=num;
		
		while(num--){
   
			sum+=num;
		}
		
		cout<<sum<<endl<<endl;
	}
	return 0;
}

1.5.HDOJ 2000

Problem Description

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

Input

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

Output

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

Sample Input

qwe
asd
zxc

Sample Output

e q w
a d s
c x z

答案一(正确、且必须这么做)

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv){
	char a,b,c,temp;
	while(cin>>a>>b>>c){		//这里就实现了下面二维数组没有实现的多组数据
		if(a>b){
			temp=a;
			a=b;
			b=temp;
		}
		if(a>c){
			temp=a;
			a=c;
			c=temp;
		}
		if(b>c){
			temp=b;
			b=c;
			c=temp;
		}
		cout<<a<<" "<<b<<" "<<c<<endl;
	}
	return 0;
}


答案二(wrong,因为人家没有说是三乘三的二维数组)

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv){
	char c[3][3];
	char in,temp;
	int i=0,j=0;
	
	while(i <3){
		j=0;
		while(j<3){
			cin>>in ;
			c[i][j++]=in;	
		}
		++i;	
	}  
	
	//bubble sort
	int k=0;
	while(k<3){
		for(int m = 3;m>=1;m--) 
		{
			for(int n = 1;n<m;n++){
				if(c[k][n]<c[k][n-1]){
					temp=c[k][n];
					c[k][n]=c[k][n-1];
					c[k][n-1]=temp;
				}
			}
		}	
			++k;
	}
	//put out
	for(int a =0;a<3;a++){
		for(int b=0;b<3;b++){
			cout<<c[a][b]<<" ";
		}	
		cout<<endl;
	}
	
	return 0;
}


2.1 HDOJ 2001

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 <iostream>
#include <iomanip>//保留小树头文件
#include <math.h>//开根号
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
   
	float x1,x2,y1,y2,s,d;
	while(cin>>x1>>y1>>x2>>y2){
   
		s=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
		s=sqrt(s);
		cout<<setiosflags(ios::fixed)<<setprecision(2)<<s<<endl;//保两位小数
        //	cout<<fixed<<setprecision(2)<<s<<endl; //fixed也可,已提交过
	}	
	return 0;
}

2.2 HDOJ 2002

Problem Description

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

Input

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

Output

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

Sample Input

1
1.5

Sample Output

4.189
14.137



Hint
#define PI 3.1415927
 
#include <iostream>
#include <math.h>
#include <iomanip>
#define PI 3.1415927
using namespace std;


int main(int argc, char** argv) {
   
	double r,v;     //第一次float居然wrong answer,开发类型不够哦
	while(cin>>r){
   
	v=4*PI*r*r*r/3;
	cout<<fixed<<setprecision(3)<<v<<endl; 	
	}
	return 0;
}

2.3 HDOJ2003绝对值

Problem Description

求实数的绝对值。

Input

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

Output

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

Sample Input

123
-234.00

Sample Output

123.00
234.00
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
   
	double a;
	while(cin>>a){
   
		if(a<0){
   
			a=0-a;
		}
		cout<<fixed<<setprecision(2)<<a<<endl;
	}
	return 0;
}

2.4 HDOJ 2004 成绩转换

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 <iostream>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
   
	int t;
	while(cin>>t){
   
		if(t>=90&&t<=100){
   
			cout<<"A"<<endl;
		}
		else if(t>=80&&t<=89){
   
			cout<<"B"<<endl;
		}
		else if(t>=70&&t<=79){
   
			cout<<"C"<<endl;
		}
		else if(t>=60&&t<=69){
   
			cout<<"D"<<endl;
		}
		else if(t>=0&&t<=59){
   
			cout<<"E"<<endl;
		}
		else {
   
			cout<<"Score is error!"<<endl;
		}
	}
	return 0;
}

2.5HDOJ2005

Problem Description

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

Input

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

Output

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

Sample Input

1985/1/20
2006/3/12

Sample Output

20
71
#include <iostream>
using namespace std;
//judge leap year
int leap(int y){
   						//判断闰年误把或写成且导致wrong answer
	if((y%4==0&&y%100!=0)||y%400==0){
   
		return 1;
	}
	return 0;
}

int main(int argc, char** argv) {
   
	int y,m,d,flag,sum;
	char month[12]={
   31,28,31,30,31,30,31,31,30,31,30,31};
	while(scanf("%d/%d/%d",&y,&m,&d)!=EOF){
   
		sum=0;
		flag=leap(y);
		if(m>2){
   
		sum+=flag;	
		}
		for(int i=0;i<m-1;i++){
   
			sum+=month[i];
		}
		sum+=d;
		cout<<sum<<endl; 
	}
	return 0;
}

3.1 HDOJ 2010

Problem Description

春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+3^3。
现在要求输出所有在m和n范围内的水仙花数。

Input

输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。

Output

对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。

Sample Input

100 120
300 380

Sample Output

no
370 371
#include <iostream>

using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//判断水仙花 
int flower(int m){
   
	int x,y,z;
	x=m%10;
	y=m/10%10;
	z=m/100%10;
	if(m==x*x*x+y*y*y+z*z*z){
   
		return 1;
	}
	 return 0;
}

int main(int argc, char** argv) {
   
	int m,n,num,i,flag,sum=0;
	int f[9000];
	while(cin>>m>>n){
   
		num = m;
		sum = 0; 
		while(num<=n){
   
			if(flower(num)){
    //是水仙花数 
			f[sum++]=num;
			num++;	
			}
			else{
   
			num++;
			}	
		}
		i=0;
		flag=0;
		if(sum>0){
   
		   	while(i<sum){
   		//用空格隔开需要flag帮助
		   		if(flag==1){
    	//用空格隔开就是最后一个数后面没有空格,坑:cout<<f[i++]<<" ";
		   			cout<<" ";
				   }
					cout<<f[i++];
					flag=1;	
			}
			cout<<endl;	
		}
		else{
   
			cout<<"no"<<endl;
		}
		
	}
	
	
	return 0;
}

3.2 HDOJ 2039

Problem Description

给定三条边,请你判断一下能不能组成一个三角形。

Input

输入数据第一行包含一个数M,接下有M行,每行一个实例,包含三个正数A,B,C。其中A,B,C <1000;

Output

对于每个测试实例,如果三条边长A,B,C能组成三角形的话,输出YES,否则NO。

Sample Input

2
1 2 3
2 2 2

Sample Output

NO
YES
#include <iostream>
#include <math.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
   
	double m,a,b,c;					//说是正数(double),不是正整数(int)
	cin>>m;				//Positive number 正数	positive integer正整数
	while(m--){
   
		cin>>a>>b>>c;
		if(a+b>c&&a+c>b&&b+c>a){
   
			cout<<"YES"<<endl;
		}
		else if(a>999||b>999||c>999||a<0||b<0||c<0){
     //可以省略,已提交验证
			return 0;
		}
		else{
   
			cout<<"NO"<<endl;
		} 
	}
	return 0;
}

3.3 HDOJ1720 转换进制

Problem Description

Many classmates said to me that A+B is must needs.
If you can’t AC this problem, you would invite me for night meal. _

Input

Input may contain multiple test cases. Each case contains A and B in one line.
A, B are hexadecimal number.
Input terminates by EOF.

Output

Output A+B in decimal number in one line.

Sample Input

1 9
A B
a b

Sample Output

10
21
21
#include <iostream>
using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int a,b;
	cin>>hex;
	while (cin>>a>>b){
		a+=b;
		cout<<dec;
		cout<<a<<endl;
	}
	return 0;
}

3.4HDOJ 1062 翻转字符

Problem Description

Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.

Output

For each test case, you should output the text which is processed.

Sample Input

3
olleh !dlrow
m'I morf .udh
I ekil .mca

Sample Output

hello world!
I'm from hdu.
I like acm.



Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.
#include <iostream>
#include <string.h>
using namespace  std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
   
	int t,j,flag=-1;
	char str[1000];
	char s1[1000];
	cin>>t;
	getchar(); 						//接收换行,你输完t必然会回车换行 
	while(t--){
   
		gets(str);
	 	int len=strlen(str);		//注意求数组的函数,.length()不识别,头文件记得.h,注意strlen是对数组用的 
		flag = -1;
		for(int i=0;i<=len;i++){
   
			if(str[i]==' ' || i==len) {
    //不用再建立新的数组存单词 
				 for(j=i-1;j>=0&&j!=flag;j--){
   //遇空格时候用flag标记,下次输出到此处停止 
				 	cout<<str[j];
				 } 
				 flag=i;
				if(i!=len){
   
				 	cout<<" ";
				 } 
			}
		} 
		cout<<endl;
	}
	
	return 0;
}

3.5 HDOJ 2104(互质)

Problem Description

The Children’s Day has passed for some days .Has you remembered something happened at your childhood? I remembered I often played a game called hide handkerchief with my friends.
Now I introduce the game to you. Suppose there are N people played the game ,who sit on the ground forming a circle ,everyone owns a box behind them .Also there is a beautiful handkerchief hid in a box which is one of the boxes .
Then Haha(a friend of mine) is called to find the handkerchief. But he has a strange habit. Each time he will search the next box which is separated by M-1 boxes from the current box. For example, there are three boxes named A,B,C, and now Haha is at place of A. now he decide the M if equal to 2, so he will search A first, then he will search the C box, for C is separated by 2-1 = 1 box B from the current box A . Then he will search the box B ,then he will search the box A.
So after three times he establishes that he can find the beautiful handkerchief. Now I will give you N and M, can you tell me that Haha is able to find the handkerchief or not. If he can, you should tell me “YES”, else tell me “POOR Haha”.

Input

There will be several test cases; each case input contains two i

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值