HDOJ10天刷题顺序
每天四五道(1.1代表第一天第一道),从易到难
分享一下我的结果,都AC了,欢迎提意见哦~
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
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;
}
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
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;
}
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
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;
}
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
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;
}
Problem Description
输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
Sample Input
qwe
asd
zxc
Sample Output
e q w
a d s
c x z
答案一(正确、且必须这么做)
#include
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<
答案二(wrong,因为人家没有说是三乘三的二维数组)
#include
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
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
#include //保留小树头文件
#include //开根号
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<
}
return 0;
}
Problem Description
根据输入的半径值,计算球的体积。
Input
输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。
Output
输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。
Sample Input
1
1.5
Sample Output
4.189
14.137
Hint
#define PI 3.1415927
#include
#include
#include
#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;
}
Problem Description
求实数的绝对值。
Input
输入数据有多组,每组占一行,每行包含一个实数。
Output
对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
Sample Input
123
-234.00
Sample Output
123.00
234.00
#include
#include
#include
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;
}
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
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;
}
Problem Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20
71
#include
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;
}
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
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<
cout<<" ";
}
cout<<f[i++];
flag=1;
}
cout<<endl;
}
else{
cout<<"no"<<endl;
}
}
return 0;
}
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
#include
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;
}
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
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<
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
#include
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-