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 integers N and M, which satisfy the relationship: 1<=M<=100000000 and 3<=N<=100000000. When N=-1 and M=-1 means the end of input case, and you should not process the data.

Output

For each input case, you should only the result that Haha can find the handkerchief or not.

Sample Input

3 2
-1 -1

Sample Output

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 */
//互为质数:辗转相除法
//以除数和余数反复做除法运算,当余数为 0 时,取当前算式除数为最大公约数,所以就得出了 1997 和 615 的最大公约数 1。 
//如果n和m不互质的话就会出现某些人是永远不会找,互质最大公因数是1,就是说他门之间以1为间隔的数都会遍历 
int check(int n,int m){
	while(m){
		int r=n%m;
		n=m;
		m=r;
	}
	return n;
}
int main(int argc, char** argv){
	int n,m;
	while(cin>>n>>m){
			if(n==-1&&m==-1){
			return 0;
		}
		int r=check(n,m);
		if(r==1){
			cout<<"YES"<<endl;
		}
		else{
			cout<<"POOR Haha"<<endl;
		}
	}
	return 0; 
}

** 答案二(wrong,无法克服数组定义及3数组越界)**

#include <iostream>
#include <math.h>
using namespace std; 

int main(int argc, char** argv) {
	int m,n=100,r=1,count;			//r代表要此时寻得box 
	while(cin>>n>>m){
	long int flag[n]={0};//编译不通过,克服不了如何定数组长度 ,直接定义一亿会内存超限 
		flag[r] =1;	
		
		if(n==-1&&m==-1){
			return 0;
		}
		else{
			count = 0;
			while(count<n){
				r=r+m;
				if(r>n){
					r=r%n;
				}
				flag[r]=1;
				count++;	    //计数寻找了几个人了 
			}
		}
		for(int i=1;i<=n;i++){	//i从1开始,因为小朋友是1开始数的 
			if(flag[i]==0){
				cout<<"POOR Haha"<<endl;
				break;
			}
			if(i==n){           //遍历结束无0 
				cout<<"YES"<<endl;
			} 
		}
	}
	return 0;
}

4.1HDOJ 1064 求平均数

Problem Description

Larry graduated this year and finally has a job. He’s making a lot of money, but somehow never seems to have enough. Larry has decided that he needs to grab hold of his financial portfolio and solve his financing problems. The first step is to figure out what’s been going on with his money. Larry has his bank account statements and wants to see how much money he has. Help Larry by writing a program to take his closing balance from each of the past twelve months and calculate his average account balance.

Input

The input will be twelve lines. Each line will contain the closing balance of his bank account for a particular month. Each number will be positive and displayed to the penny. No dollar sign will be included.

Output

The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by the end-of-line. There will be no other spaces or characters in the output.

Sample Input

100.00 
489.12 
12454.12 
1234.10 
823.05 
109.20 
5.27 
1542.25 
839.18 
83.99 
1295.01 
1.75

Sample Output

$1581.42
#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) {
	double ave=0,n=0,sum=0;
	int m=0;
	while(m<12){
		cin>>n;
		m++;
		sum+=n;
	}
	ave=sum/12;
	cout<<"$"<<ave<<endl;
	return 0;
}

4.2HDOJ 2734

ACM: 11 + 23 + 313 = 46MID CENTRAL: 113 + 29 + 34 + 40 + 53 + 65 + 714 + 820 + 918 + 101 + 1112 = 650

Input

The input consists of one or more packets followed by a line containing only # that signals the end of the input. Each packet is on a line by itself, does not begin or end with a space, and contains from 1 to 255 characters.

Output

For each packet, output its Quicksum on a separate line in the output.

Sample Input

ACM
MID CENTRAL
REGIONAL PROGRAMMING CONTEST
ACN
A C M
ABC
BBC
#

Sample Output

46
650
4690
49
75
14
15
#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) {
	char c;
	char str[1000];
	int count,num;
	while(gets(str)){
		if(str[0] =='#'){	//这里是数组,写str=="#",不对 
			return 1;
		} 
		
		int len=strlen(str); //strlen是针对数组用的,不是字符串 
		int i = 0 ;
		int sum=0;
		while(i!=len){
			while(str[i]==' '){
				i++; 
			} 
			num=str[i]-'A'+1;
			num*=(i+1);
			sum+=num;
			i++;	
		}
		cout<<sum<<endl;		
	}
	return 0;
}

4.3 HDOJ 1170 计算器

Problem Description

The contest starts now! How excited it is to see balloons floating around. You, one of the best programmers in HDU, can get a very beautiful balloon if only you have solved the very very very… easy problem.
Give you an operator (+,-,*, / --denoting addition, subtraction, multiplication, division respectively) and two positive integers, your task is to output the result.
Is it very easy?
Come on, guy! PLMM will send you a beautiful Balloon right now!
Good Luck!

Input

Input contains multiple test cases. The first line of the input is a single integer T (0<T<1000) which is the number of test cases. T test cases follow. Each test case contains a char C (+,-,*, /) and two integers A and B(0<A,B<10000).Of course, we all know that A and B are operands and C is an operator.

Output

For each case, print the operation result. The result should be rounded to 2 decimal places If and only if it is not an integer.

Sample Input

4
+ 1 2
- 1 2
* 1 2
/ 1 2
#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 */
double op(char c,int a,int b){
	double r;
	if(c=='+')	{
		r=a+b;
	}
	else if(c=='-')	{
		r=a-b;
	}
	else if(c=='*')	{
		r=a*b;
	}
	else if(c=='/')	{
		if(b!=0){
			r=(double)a/b;  	
		}  //除法默认是整数,要保留两位小数转float 
	}
	return r;
}

int main(int argc, char** argv) {
	int t,a,b;
	char c;
	double r;
	cin>>t;
	while(t--){
		cin>>c>>a>>b;
		r=op(c,a,b);
		if(c=='/'&&a%b!=0)
		{
			cout<<fixed<<setprecision(2)<<r<<endl;//除法考虑小数的情况 
		}
		else{
			cout<<(int)r<<endl;	            //如果这里不转化r 就还是double型 
		}
	}
	return 0;
}

4.4HDOJ 1197 转进制

Problem Description

Find and list all four-digit numbers in decimal notation that have the property that the sum of its four digits equals the sum of its digits when represented in hexadecimal (base 16) notation and also equals the sum of its digits when represented in duodecimal (base 12) notation.

For example, the number 2991 has the sum of (decimal) digits 2+9+9+1 = 21. Since 2991 = 11728 + 8144 + 9*12 + 3, its duodecimal representation is 1893(12), and these digits also sum up to 21. But in hexadecimal 2991 is BAF16, and 11+10+15 = 36, so 2991 should be rejected by your program.

The next number (2992), however, has digits that sum to 22 in all three representations (including BB016), so 2992 should be on the listed output. (We don’t want decimal numbers with fewer than four digits - excluding leading zeroes - so that 2992 is the first correct answer.)

Input

There is no input for this problem.

Output

Your output is to be 2992 and all larger four-digit numbers that satisfy the requirements (in strictly increasing order), each on a separate line with no leading or trailing blanks, ending with a new-line character. There are to be no blank lines in the output. The first few lines of the output are shown below.

Sample Input

There is no input for this problem.

Sample Output

2992
2993
2994
2995
2996
2997
2998
2999
#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) {
	int a,b,c,d,e,s1,s2,s3;
	for(a=1000;a<10000;a++){
		b=a%10;
		c=a/10%10;
		d=a/100%10;
		e=a/1000%10;
		s1=b+c+d+e;
	
		 
		b=a%12;					//注意十二进制数转法折腾了好几遍
		c=a/12%12;
		d=a/12/12%12;
		e=a/12/12/12%12; 
		s2=b+c+d+e;
	
			
		b=a%16;
		c=a/16%16;
		d=a/16/16%16;
		e=a/16/16/16%16;
		s3=b+c+d+e;
	
		
		if(s1==s2 &&s2==s3){
			cout<<a<<endl;
	}
		
	} 
	return 0;
}

5.1HDOJ 2629 身份证对应

Problem Description

Do you own an ID card?You must have a identity card number in your family’s Household Register. From the ID card you can get specific personal information of everyone. The number has 18 bits,the first 17 bits contain special specially meanings:the first 6 bits represent the region you come from,then comes the next 8 bits which stand for your birthday.What do other 4 bits represent?You can Baidu or Google it.
Here is the codes which represent the region you are in.
img
However,in your card,maybe only 33 appears,0000 is replaced by other numbers.
Here is Samuel’s ID number 331004198910120036 can you tell where he is from?The first 2 numbers tell that he is from Zhengjiang Province,number 19891012 is his birthday date (yy/mm/dd).

Input

Input will contain 2 parts:
A number n in the first line,n here means there is n test cases. For each of the test cases,there is a string of the ID card number.

Output

Based on the table output where he is from and when is his birthday. The format you can refer to the Sample Output.

Sample Input

1
330000198910120036

Sample Output

He/She is from Zhejiang,and his/her birthday is on 10,12,1989 based on the table.
#include <iostream>
#include<string.h>
#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) {
	char id[18];
	int n,p;
	cin>>n;
	getchar(); //接收后面的回车 ,总是差点误以为是while(n--)导致少接受一组 
	while(n--){
		gets(id);
		p=(id[0]-'0')*10+(id[1]-'0');
		cout<<"He/She is from ";
		if(p==33){
		cout<<"Zhejiang,";
		}
		if(p==11){
		cout<<"Beijing,";
		}
		if(p==71){
		cout<<"Taiwan,";
		}
		if(p==81){
		cout<<"Hong Kong,";
		}
		if(p==82){
		cout<<"Macao,";
		}
		if(p==54){
		cout<<"Tibet,";
		}
		if(p==21){
		cout<<"Liaoning,";
		}
		if(p==31){
		cout<<"Shanghai,";
		}
		cout<<"and his/her birthday is on ";
		cout<<id[10]<<id[11]<<","<<id[12]<<id[13] <<','<<id[6]<<id[7]<<id[8]<<id[9];
		cout<<" based on the table."<<endl;
	
	}
	return 0;
}

5.2HDOJ 2012 素数判定

Problem Description

对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。

Input

输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。

Output

对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。

Sample Input

0 1
0 0

Sample Output

OK
#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 prime(int i){//四个条件,少一算错 
 if(i==1){
 	return 0;
 }
 else if(i==2){    //注意前三条素数判定条件 
 	return 1;
 }
 else if( i<=0){
 	return 0;
 }
 else {
 	for (int j=2;j<i;j++){ //注意j从2开始 ,j<i不是<= 
 		if(i%j==0){
 			return 0;
		 }
	 }
	 return 1;
 }
}
int main(int argc, char** argv) {
	int a,b,c,flag=0;
	while(cin>>a>>b){
		flag=0;
		if(a<-39||b>50)
			break;
		else{
			if(a==0&&b==0){
				break;
			}
			for(int i=a;i<=b;i++){
				c=i*i+i+41;
				if(prime(c)==1) {
					flag=1;
					continue;
				}
				else if(!prime(c)){
					flag=0;
					break;
				} 
			}
			if(flag==1){
				cout<<"OK"<<endl;
			}
			else if(flag==0){
				cout<<"Sorry"<<endl;
			}
		}
	}
	return 0;
}

5.3HDOJ2013 吃蟠桃(复习)

Problem Description

喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!
什么问题?他研究的问题是蟠桃一共有多少个!
不过,到最后,他还是没能解决这个难题,呵呵-
当时的情况是这样的:
第一天悟空吃掉桃子总数一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第n天准备吃的时候只剩下一个桃子。聪明的你,请帮悟空算一下,他第一天开始吃的时候桃子一共有多少个呢?

Input

输入数据有多组,每组占一行,包含一个正整数n(1<n<30),表示只剩下一个桃子的时候是在第n天发生的。

Output

对于每组输入数据,输出第一天开始吃的时候桃子的总数,每个测试实例占一行。

Sample Input

2
4

Sample Output

4
22
#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 num(int n){
	int sum=(n+1)*2;
	return sum;
}
int main(int argc, char** argv) {
	int n,s;
	while(cin>>n){
		if(n<=1||n>=30)
			break;
		else{
			s=1;
			int i =1;
			while(n){
				if(n>1){
				   s=num(s); //因为是只剩下一个桃子的时候是在第n天发生的。所以不能把最后一天也吃了 
				}
				n--;        //此时不能while n--,这样在IF用n的时候n已经变了 
			}	
			cout<<s<<endl;
		}
	}
	return 0;
}

5.4 HDOJ 2014(平均值)

Problem Description

青年歌手大奖赛中,评委会给参赛选手打分。选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分。

Input

输入数据有多组,每组占一行,每行的第一个数是n(2<n<=100),表示评委的人数,然后是n个评委的打分。

Output

对于每组输入数据,输出选手的得分,结果保留2位小数,每组输出占一行。

Sample Input

3 99 98 97
4 100 99 98 97

Sample Output

98.00
98.50
#include <iostream>
#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) {
	int num,sco,min,max;
	double ave;
	while(cin>>num){
//		if(num>100||num<3){  //要不要都行 
//			break;
//		} 
	    int n=num-2;
		sco=max=ave=0;
		min=100;
		while(num--){
			cin>>sco;
			ave+=sco;
			if(sco<min){
			    min=sco;
			} 
			if(sco>max){
				max=sco;
			}
		}
		ave=ave-min-max;
		ave/=n;
		cout<<fixed<<setprecision(2)<<ave<<endl;
	}
	
	return 0;
}

5.5HDOJ 2015 偶数求和

Problem Description

有一个长度为n(n<=100)的数列,该数列定义为从2开始的递增有序偶数,现在要求你按照顺序每m个数求出一个平均值,如果最后不足m个,则以实际数量求平均值。编程输出该平均值序列。

Input

输入数据有多组,每组占一行,包含两个正整数n和m,n和m的含义如上所述。

Output

对于每组输入数据,输出一个平均值序列,每组输出占一行。

Sample Input

3 2
4 2

Sample Output

3 6
3 7
#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 m,n,a,s;
	while(cin>>n>>m){
		a=0;
		s=0;
		while(n){
			if(n<=0||m<=0){			//对于正整数可以不判断 
				break;
			} 
			if(m<n){
				s=0;              //少来这一步会产生累加 
				for(int i=0;i<m;i++){
					a+=2; 
					s+=a;
				}
				cout<<s/m<<' ';    //这里后面带空格,if盘点条件就只能是m<n,若<=就是 Presentation Error
				n-=m;
			}	
			else{
				s=0;
				for(int i=0;i<n;i++){
					a+=2; 
					s+=a;
				}
				s=s/n;
				cout<<s<<endl;
				break;
			}
		}
	}
	return 0;
}

5.6 2016

Problem Description

输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。

Input

输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。

Output

对于每组输入数据,输出交换后的数列,每组输出占一行。

Sample Input

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

Sample Output

1 2 3 4
1 4 3 2 5
#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,min,k;
	int n[100];
	while(cin>>num){
		if(num==0){
			break;
		} 
		else{
			k=0;
			for(int i=0;i<num;i++){
				cin>>n[i];
				if(i==0){
					min=n[0];
				}
				if(min>n[i]){
					min=n[i];
					k=i;
				}
			}
			n[k]=n[0];
			n[0]=min;
			for(int i=0;i<num-1;i++){
				cout<<n[i]<<' ';
			}
			cout<<n[num-1]<<endl;
		}
	}
	return 0;
}

6.1HDOJ2017 字符串里数数字

Problem Description

对于给定的一个字符串,统计其中数字字符出现的次数。

Input

输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

Output

对于每个测试实例,输出该串中数值的个数,每个输出占一行。

Sample Input

2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf

Sample Output

6
9
#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) {
	char c[2000];
	int n,len;
	cin>>n;
	getchar();
	while(n--){
		gets(c);
		len = strlen(c);
		int count = 0 ;
		for(int i=0;i<len;i++){
			if(c[i]-'0'<=9&&c[i]-'0'>=0){
				count++;
			}
		}
		cout<<count<<endl;
	}
	return 0;
}

6.2HDOJ 2018 母牛的故事

Problem Description

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?

Input

输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。

Output

对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。

Sample Input

2
4
5
0

Sample Output

2
4
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;//year
	int a=0;//1 年熟牛 
	int b=0;//2 年熟牛
	int c=0;//3 年熟牛
	int d=0;//4 年熟牛
	int sum;
	while(cin>>n){
		a=b=c=0;
		d=1;
		if(n==0) {
			break;	
		}
		else{
			for(int i=1;i<n;i++){  //第n年的时候,共有多少头母牛?根据数字判得第n年新出生的牛不计入 
				d+=c;//老牛+变老的嫩牛 
				c=b;
				b=a;
				a=d;//刚生的嫩牛 
				sum=a+b+c+d;
			}
			cout<<sum<<endl;  
		}
	} 
	return 0;
}

6.3HDOJ2019插入数字

Problem Description

有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然有序。

Input

输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0标示输入数据的结束,本行不做处理。

Output

对于每个测试实例,输出插入新的元素后的数列。

Sample Input

3 3
1 2 4
0 0

Sample Output

1 2 3 4
#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 m,n,temp;
	int num[100];
	while(cin>>n>>m){
		if(n==0&&m==0){ //不可n==m==0,判错 
			break;
		}
		for(int i=0;i<n;i++){
			cin>>num[i];
		}
		for(int i=0;i<n;i++){
			
			if(m<num[i]){
				int k=n;
				while(k>i){
					num[k]=num[k-1];
					k--; 	
				}
				num[i]=m;
				break;	
			}
			
		}
		
			for(int i=0;i<n;i++){
				cout<<num[i]<<" ";
			}
			cout<<num[n]<<endl;
	}
	return 0;
}

6.4HDOJ 2020 绝对值排序

Problem Description

输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。

Input

输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。

Output

对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。

Sample Input

3 3 -4 2
4 0 1 2 -3
0

Sample Output

-4 3 2
-3 2 1 0
#include <iostream>
using namespace std; 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int jue(int n){    //注意是对绝对值排完序之后还是按原数字输出,而不是输出绝对值
	if(n>0){
		return n;
	} 
	else if(n<0){
		n=0-n;
		return n;
	}
}


int main(int argc, char** argv) {
	int n,num,temp,flag;
	int sor[100];
	while(cin>>n){
		if(n==0) break;
		for(int i =0;i<n;i++){
			cin>>num;
			sor[i]=num;
		}

		for(int i=n-1;i>=1;i--){
			flag=0;
			for(int j=1;j<=i;j++){
				if(jue(sor[j])<jue(sor[j-1])){
					temp=sor[j];
					sor[j]=sor[j-1];
					sor[j-1]=temp;
					flag=1;
				} 
			}
			
			if(flag==0){
				for(int i=n-1;i>0;i--){
					cout<<sor[i]<<' ';
				}
				cout<<sor[0]<<endl;
				break; 
			}	
		}
		
	}
	return 0;
}

7.1 HDOJ 2021 发工资

Problem Description

作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵
但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡老师最近就在考虑一个问题:如果每个老师的工资额都知道,最少需要准备多少张人民币,才能在给每位老师发工资的时候都不用老师找零呢?
这里假设老师的工资都是正整数,单位元,人民币一共有100元、50元、10元、5元、2元和1元六种。

Input

输入数据包含多个测试实例,每个测试实例的第一行是一个整数n(n<100),表示老师的人数,然后是n个老师的工资。
n=0表示输入的结束,不做处理。

Output

对于每个测试实例输出一个整数x,表示至少需要准备的人民币张数。每个输出占一行。

Sample Input

3
1 2 3
0

Sample Output

4
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int sol(int s){
	int n[6]={100,50,10,5,2,1};
	int x=0,a;
	for(int i=0;i<6;i++){
		if(s>=n[i]){
			x=s/n[i];
			a=s%n[i];
			if(a==0){
				return x;
			}
			else if(a!=0){
				return x+sol(a);
			}
		}
	}
} 

int main(int argc, char** argv) {
	int n,s,sum;
	while(cin>>n){
		if(n==0) break;
		sum=0;
		for(int i = 0;i<n;i++){
			cin>>s;
			sum+=sol(s);
		}
		cout<<sum<<endl;
	}
	return 0;
}

7.2 HDOJ2022海选女主

Problem Description

potato老师虽然很喜欢教书,但是迫于生活压力,不得不想办法在业余时间挣点外快以养家糊口。
“做什么比较挣钱呢?筛沙子没力气,看大门又不够帅…”potato老师很是无奈。
“张艺谋比你还难看,现在多有钱呀,听说还要导演奥运开幕式呢!你为什么不去娱乐圈发展呢?”lwg在一旁出主意。
嗯,也是,为了生存,就委屈点到娱乐圈混混吧,马上就拍一部激光电影《杭电记忆——回来我的爱》。
说干就干,马上海选女主角(和老谋子学的,此举可以吸引媒体的眼球,呵呵),并且特别规定,演员必须具有ac的基本功,否则直接out!
由于策划师风之鱼(大师级水王)宣传到位,来应聘的MM很多,当然包括nit的蛋糕妹妹等呼声很高的美女,就连zjut的jqw都男扮女装来应聘(还好被安全顾问hdu_Bin-Laden认出,给轰走了),看来娱乐圈比acm还吸引人哪…
面试那天,刚好来了mn个MM,站成一个mn的队列,副导演Fe(OH)2为每个MM打了分数,分数都是32位有符号整数。
一开始我很纳闷:分数怎么还有负的?Fe(OH)2解释说,根据选拔规则,头发染成黄色、化妆太浓、穿的太少等等都要扣分数的,扣的多了就可能是负分了,当然,如果发现话语中夹有日语,就直接给-2147483648分了。
分数送上来了,是我做决定的时候了,我的一个选拔原则是,要选一个面试分数绝对值(必须还是32位整数)最大的MM。
特别说明:如果不幸选中一个负分的MM,也没关系,因为我觉得,如果不能吸引你,那要想法恶心你。

Input

输入数据有多组,每组的第一行是两个整数m和n,表示应聘MM的总共的行列数,然后是m行整数,每行有n个,m和n的定义见题目的描述。

Output

对于每组输入数据,输出三个整数x,y和s,分别表示选中的MM的行号、列号和分数。
note:行号和列号从一开始,如果有多个MM的分数绝对值一样,那么输出排在最前面的一个(即行号最小的那个,如果行号相同则取列号最小的那个)。

Sample Input

2 3
1 4 -3
-7 3 0

Sample Output

2 1 -7
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int jue(int n){    //注意是对绝对值排完序之后还是按原数字输出,而不是输出绝对值
	if(n>0){
		return n;
	} 
	else if(n<0){
		n=0-n;
		return n;
	}
}

int main(int argc, char** argv) {
	int m,n,score,max=0,maxj,row,col;
	while(cin>>m>>n){
		int MM[100][100];  //我这MM[m][n],dev编译通过、oj不过 
		maxj=0;
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				cin>>score;
				if(jue(score)>maxj){
					row=i;
					col=j;
					maxj=jue(score); //要记录最大的绝对值,用来if比较 
					max=score;
				}
				
			}
		}
		cout<<row+1<<' '<<col+1<< ' '<<max<<endl;
		
	}
	return 0;
}

7.3HDOJ2023求平均成绩

Problem Description

假设一个班有n(n<=50)个学生,每人考m(m<=5)门课,求每个学生的平均成绩和每门课的平均成绩,并输出各科成绩均大于等于平均成绩的学生数量。

Input

输入数据有多个测试实例,每个测试实例的第一行包括两个整数n和m,分别表示学生数和课程数。然后是n行数据,每行包括m个整数(即:考试分数)。

Output

对于每个测试实例,输出3行数据,第一行包含n个数据,表示n个学生的平均成绩,结果保留两位小数;第二行包含m个数据,表示m门课的平均成绩,结果保留两位小数;第三行是一个整数,表示该班级中各科成绩均大于等于平均成绩的学生数量。
每个测试实例后面跟一个空行。

Sample Input

2 2
5 10
10 20

Sample Output

7.50 15.00
7.50 15.00
1
#include <iostream>
#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) {
	int n,m;
	int s[60][10];
	double stu[60]; //学生 
	double sor[6]; 
	double sum;	//课程 
	int count;
	while(cin>>n>>m){
		count = 0;
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				cin>>s[i][j]; 
			}
		}
		
		//每课程平均成绩
		for(int i=0;i<m;i++){
			sum=0; 
			for(int j=0;j<n;j++){
			sum+=s[j][i];//i定j变,j是学生数	
			if(j==(n-1)){
				sor[i]=sum/n; //i代表第几门课 ,sum必须是double形,/以后才能让sor是double型 
			}
			} 
		} 
		//学生平均成绩 
		for(int i=0;i<n;i++){
			sum=0; 
			for(int j=0;j<m;j++){
			sum+=s[i][j];//i定j变,j是课程 
			if(j==(m-1)){
				stu[i]=sum/m; //i代表第几学生 
			}
			} 
		} 
		//各科成绩均大于等于平均成绩
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(s[i][j]<sor[j]){
					break;
				}
				if(j==m-1){ //已经比较到最后一门,说明前面几门都>平均值 
					count++;	
				}
			}
		}
		
		for(int i=0;i<n-1;i++){
		cout<<fixed<<setprecision(2)<<stu[i]<<' ';	
		}
		cout<<fixed<<setprecision(2)<<stu[n-1]<<endl;
		
		for(int i=0;i<m-1;i++){
		cout<<fixed<<setprecision(2)<<sor[i]<<' ';	
		}
		cout<<fixed<<setprecision(2)<<sor[m-1]<<endl;
		
		cout<<count<<endl<<endl; 								//每个测试实例后面一个空行 
		
		
	}
	return 0;
}

7.4HDOJ2024 判断合法标识符

Problem Description

输入一个字符串,判断其是否是C的合法标识符。

Input

输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。

Output

对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。

Sample Input

3
12ajf
fi8x_a
ff  ai_2

Sample Output

no
yes
no
#include <iostream>
#include <cctype>
#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) {
	//标识符由字母、下划线、数字这三个方面组成,
	//但开头必须是字母或下划线。 另外,关键字不能是标识符
	char s[200];
	int n,flag;
	cin>>n;
	getchar();
	while(n--){
		gets(s);
		flag=1;
		int i=1;
		if(!isalpha(s[0])&& s[0]!='_'){			//判断字符ch是否为英文字母,若为英文字母,返回非0(小写字母为2,大写字母为1)
				flag=0;
			}
		while(s[i]!='\0'&&flag==1){
			if(isalnum(s[i])||s[i]=='_'){		//判断字符变量c是否为字母或数字,若是则返回非零,否则返回零
				flag=1;
			}
			else{
				flag=0;
				break;
			}
			i++;
		}
		if(flag==0){
			cout<<"no"<<endl;
		}
		else if(flag==1){
			cout<<"yes"<<endl;
		} 
		
	}
	return 0;
}

8.1HDOJ 2025 找最大元素

Problem Description

对于输入的每个字符串,查找其中的最大字母,在该字母后面插入字符串“(max)”。

Input

输入数据包括多个测试实例,每个实例由一行长度不超过100的字符串组成,字符串仅由大小写字母构成。

Output

对于每个测试实例输出一行字符串,输出的结果是插入字符串“(max)”后的结果,如果存在多个最大的字母,就在每一个最大字母后面都插入"(max)"。

Sample Input

abcdefgfedcba
xxxxx

Sample Output

abcdefg(max)fedcba
x(max)x(max)x(max)x(max)x(max)
#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) {
	char s[101];
	while(gets(s)){     						 //会将空格一并接收,并不会遇空格停止,gets从标准输入设备读字符串函数,其可以无限读取,不会判断上限,以回车结束读取
		int len=strlen(s);                     //要用strlen函数。头文件string.h,string不可
		char max='a';
		for(int i=0;i<len;i++){
			if(s[i]>max){
				max=s[i];
			}
		} 
		for(int i=0;i<len;i++){
			cout<<s[i];
			if(s[i]==max){
				cout<<"(max)";
			}
		}
		cout<<endl;
	}
	return 0;
}

8.2HDOJ2027数元音

Problem Description

统计每个元音字母在字符串中出现的次数。

Input

输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。

Output

对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。

请特别注意:最后一块输出后面没有空行:)

Sample Input

2
aeiou
my name is ignatius

Sample Output

a:1
e:1
i:1
o:1
u:1

a:2
e:1
i:3
o:0
u:1
#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) {
	char s[101];
	char y[5]={'a','e','i','o','u'};
	int n;
	cin>>n;
	getchar();
	while(n){
		gets(s);
		int num[5]={0,0,0,0,0};
		int len = strlen(s);
		for(int i=0;i<len;i++){
			if(s[i]=='a'){
	 		 	num[0]++;
	 		}
	 		else if(s[i]=='e'){
	 			num[1]++;
			 }
			 else if(s[i]=='i'){
	 			num[2]++;
			 }
			 else if(s[i]=='o'){
	 			num[3]++;
			 }
			 else if(s[i]=='u'){
	 			num[4]++;
			 }
  		}
  		for(int i=0;i<5;i++){
  			cout<<y[i]<<":"<<num[i]<<endl;
		  }
		n--;
		if(n!=0){
		  	cout<<endl;
		}	
	}
	return 0;
}

8.3HDOJ2026首字母变大写

Problem Description

输入一个英文句子,将每个单词的第一个字母改成大写字母。

Input

输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。

Output

请输出按照要求改写后的英文句子。

Sample Input

i like acm
i want to get an accepted

Sample Output

I Like Acm
I Want To Get An Accepted
#include <iostream>
#include <string.h> 
#include <algorithm>
#include <cctype>
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 s[101];
	while(gets(s)){
		int len=strlen(s);
		s[0]= toupper(s[0]);           //忘了用s[0]接upper函数 
		for(int i=1;i<len;i++){
			while(s[i]==' '){
				i++; 
				s[i] = toupper(s[i]);  //忘了用s[i]接函数 
			}
		}
		for(int i=0;i<len;i++){
			cout<<s[i];
		}
		cout<<endl;
	}
	return 0;
}

8.4HDOJ2028求n个数的最小公倍数。

Problem Description

求n个数的最小公倍数。

Input

输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。

Output

为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。

Sample Input

2 4 6
3 2 5 7

Sample Output

12
70
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//1、两个数的最小公倍数=两个数的乘积÷两个数的最大公约数
//2、用辗转相除法求两个数的最大公约数:
//     319÷377余319;
//     377÷319余58;
//     319÷58余29;
//     58÷29余0;
//     故319和377的最大公约数是29;319和377的最小公倍数是319*377÷29=4147;

//公约数Greatest Common Divisor 最大公约数
unsigned long  gcd(int a,int b){
	int c=a%b;
	while(c!=0){
		a=b;
		b=c;
		c=a%b;
	}
	return b;
}
int main(int argc, char** argv) {
	unsigned long n,bei,r;			////必须用unsigned long型,最后可能为32位的整数 
	unsigned long num[1000];
	while(cin>>n){
		for(int i=0;i<n;i++){
			cin>>num[i];
			r=num[i];
			if(i!=0){
				bei=num[i]*num[i-1];
				num[i]=gcd(num[i],num[i-1]);//num[i] is GCD
				num[i]=bei/num[i];  
				r=num[i];        //num[i]存最小公倍数 
			}
		}
		cout<<r<<endl;
		
	}
	return 0;
}

8.5HDOJ 2029 回文数

Problem Description

“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。

Input

输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。

Output

如果一个字符串是回文串,则输出"yes",否则输出"no".

Sample Input

4
level
abcde
noon
haha

Sample Output

yes
no
yes
no

解法一:自写繁琐

#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 hui(char s[],int len){
	int i,flag=1;
	if(len%2==0){
		i=len/2-1;                    //回文串要注意数组与除法的对应 
		int j=i+1;
		while(i!=-1){
			if(s[i]==s[j]){
				i--;
				j++;
				flag=1;
			}
			else{
				flag=0;
				break;
			}	
		}
	}
	else{
		i=len/2;
		int j=0;
		int m=0;
		flag=1;
		while(m!=-1){
			j++;
			m=i-j;
			if(s[i-j]!=s[i+j]){
				flag=0;
				break;
			}
			else{
				flag=1;	
			}
		} 
		
	}	
	return flag;
}

int main(int argc, char** argv) {
	int n;
	char s[1000];
	cin>>n;
	getchar();
	while(n--){
		gets(s);
		int len=strlen(s);
		if(hui(s,len)==1){
			cout<<"yes"<<endl;
		}
		else if(hui(s,len)==0){
			cout<<"no"<<endl;
		}
		
	}
	return 0;
}

解二:借鉴简单

#include <stdio.h>
#include <string.h>
int main(){
	int n;
	scanf("%d\n",&n);
	char input[1024];
	bool flag;
	while(n--){
		flag = true;
		gets(input);
		for(int i = 0;i < strlen(input)/2.0;i++){
			if(input[i] != input[strlen(input)-1-i]){
				printf("no\n");
				flag = false;
				break;
			}
		}
		if(flag)
			printf("yes\n");
	}
	return 0;
}

9.1 HDOJ 2030 汉字统计

Problem Description

统计给定文本文件中汉字的个数。

Input

输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本。

Output

对于每一段文本,输出其中的汉字的个数,每个测试实例的输出占一行。

[Hint:]从汉字机内码的特点考虑~

Sample Input

2
WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa!
马上就要期末考试了Are you ready?

Sample Output

14
9

说明

这个题我觉得我有必要讲一下,这个题对我来说很新鲜,因为我没处理过汉字的问题,在处理这个问题之前我们需要了解一下汉字机内码在计算机内部的表示方式。
首先我们都知道由于计算机是由美国人发明的,所以ASCII码只能用来存储英文,ASCII只占一个字节,那么后来的汉字怎么用计算机表示呢,这个时候就提出来用两个字节来存储汉字和其他文字,每个字节最高位置1,而计算机中都是用补码来存储的,这就意味着汉字的每一个字节都是负数,所以求汉字的个数其实就是求字符串中小于0的个数有几个再除2即可
答案

#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 n,num;
	char s[1000];
	cin>>n;
	getchar();
	while(n--){
		num=0;
		gets(s);
		int len=strlen(s);
		for(int i=0;i<len;i++){
			if(s[i]<0){
				num++;
			}
		}
		cout<<num/2<<endl;
	} 
	return 0;
}

9.2 HDOJ 2032 杨辉三角

Problem Description

还记得中学时候学过的杨辉三角吗?具体的定义这里不再描述,你可以参考以下的图形:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Input

输入数据包含多个测试实例,每个测试实例的输入只包含一个正整数n(1<=n<=30),表示将要输出的杨辉三角的层数。

Output

对应于每一个输入,请输出相应层数的杨辉三角,每一层的整数之间用一个空格隔开,每一个杨辉三角后面加一个空行。

Sample Input

2 3

Sample Output

1
1 1

1
1 1
1 2 1
#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 n;
     int tr[32][32];                    //最开始这里定义的tr[30][30],下面for数组越界不自知 
	while(cin>>n){
		for(int i=0;i<30;i++){
			for(int j=0;j<30;j++){
				tr[i][j]=0;
			}
		}

	
		
		
	    if(n==1){
	    	cout<<1<<endl<<endl;
	    	continue; 
		}
	//	杨辉三角 
		for(int i=0;i<n;i++){
				tr[i][0]=1;
			for(int j=1;j<=i+1;j++){
				tr[i+1][j]=tr[i][j-1]+tr[i][j];
			} 
		}

		cout<<1<<endl;//下面for无法输出tr[0] [0]
		for(int i=1;i<n;i++){
			for(int j=0;j<i;j++){
				cout<<tr[i][j]<<' ';
			}
		cout<<tr[i][i]<<endl;
		}
	
	cout<<endl;
		
	}
	return 0;
}

9.3 HDOJ 2040 亲和数

Problem Description

古希腊数学家毕达哥拉斯在自然数研究中发现,220的所有真约数(即不是自身的约数)之和为:

1+2+4+5+10+11+20+22+44+55+110=284。

而284的所有真约数为1、2、4、71、 142,加起来恰好为220。人们对这样的数感到很惊奇,并称之为亲和数。一般地讲,如果两个数中任何一个数都是另一个数的真约数之和,则这两个数就是亲和数。

你的任务就编写一个程序,判断给定的两个数是否是亲和数

Input

输入数据第一行包含一个数M,接下有M行,每行一个实例,包含两个整数A,B; 其中 0 <= A,B <= 600000 ;

Output

对于每个测试实例,如果A和B是亲和数的话输出YES,否则输出NO。

Sample Input

2
220 284
100 200

Sample Output

YES
NO
#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 y(int a,int b, int flag){
	int s1=1,s2=1;
	for(int i =2;i<a;i++){
		if(a%i==0){
			s1+=i; 
		}
	}

	for(int i =2;i<b;i++){
		if(b%i==0){
			s2+=i;
		}
	}
	
	if(s1==b&&s2==a){
		flag=1;
	}else
	{
		flag=0;	
	}
	return flag;
}

int main(int argc, char** argv) {
	int n; 
	int a,b,flag;
	cin>>n;
	getchar();
	while(n--){
		cin>>a>>b;
		if(y(a,b,flag)==1){
		cout<<"YES"<<endl; 
		}
		else{
			cout<<"NO"<<endl; 	
		}	
	}
	return 0;
}

9.4HDOJ 2042 不容易系列二

Problem Description

由于徐老汉没钱,收费员就将他的羊拿走一半,看到老汉泪水涟涟,犹豫了一下,又还给老汉一只。巧合的是,后面每过一个收费站,都是拿走当时羊的一半,然后退还一只,等到老汉到达市场,就只剩下3只羊了。

你,当代有良知的青年,能帮忙算一下老汉最初有多少只羊吗?

Input

输入数据第一行是一个整数N,下面由N行组成,每行包含一个整数a(0<a<=30),表示收费站的数量。

Output

对于每个测试实例,请输出最初的羊的数量,每个测试实例的输出占一行。

Sample Input

2
1
2

Sample Output

4
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,x,sum;
	cin>>n;
	getchar();
	while(n--){
		cin>>m;
		x=sum=3;
		for(int i=0;i<m;i++){
			sum=(x-1)*2;
			x=sum;
		}
		cout<<sum<<endl;
	}
	return 0;
}

9.5 HDOJ 2055 AN EASY PROBLEM

Problem Description

we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, … f(Z) = 26, f(z) = -26;
Give you a letter x and a number y , you should output the result of y+f(x).

Input

On the first line, contains a number T.then T lines follow, each line is a case.each case contains a letter and a number.

Output

for each case, you should the result of y+f(x) on a line.

Sample Input

6
R 1
P 2
G 3
r 1
p 2
g 3

Sample Output

19
18
10
-17
-14
-4
#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;
	int n,m;
	cin>>n;
	getchar();
	while(n--){
		cin>>c>>m;
		if('a'<=c&&c<='z'){
			cout<<'a'-c-1+m<<endl;
		} 
		else if('A'<=c&&c<='Z'){
			cout<<c-'A'+1+m<<endl;
		}
	}
	return 0;
}

10.1HDOJ1050 移动桌子(贪心算法)

血泪教训:不要设置太多变量,极其容易混淆

思路:将走廊两边的房间映射到一个位置,(因为搬桌子的话,他们本质上是一位置),这样就映射到了一维坐标系,循环进行遍历,路过次数最多的地方就可以推到时间

Problem Description

The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure.

img

The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving.

img

For each room, at most one table will be either moved in or moved out. Now, the manager seeks out a method to minimize the time to move all the tables. Your job is to write a program to solve the manager’s problem.

Input

The input consists of T test cases. The number of test cases ) (T is given in the first line of the input. Each test case begins with a line containing an integer N , 1<=N<=200 , that represents the number of tables to move. Each of the following N lines contains two positive integers s and t, representing that a table is to move from room number s to room number t (each room number appears at most once in the N lines). From the N+3-rd line, the remaining test cases are listed in the same manner as above.

Output

The output should contain the minimum time in minutes to complete the moving, one per line.

Sample Input

3 
4 
10 20 
30 40 
50 60 
70 80 
2 
1 3 
2 200 
3 
10 100 
20 80 
30 50 

Sample Output

10
20
30
#include <iostream>
#include <string.h>
#include <math.h>
#include <cstring>
#include <cstdio>
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,n,s1,t1,ro[405],s,t;//ro表示原来的样子,s,t,表示从s移到t;
	int temp,max;
	cin>>T;
	getchar();
	while(T--) {
		cin>>n;
		getchar();
		for(int i=0; i<405; i++) {
			ro[i]=0;   //初始化路过次数
		}
	
		max=0;
		for(int i=0; i<n; i++) {

			cin>>s>>t;
			
			if(s>t) { //使得移动起始房间小于终点
				temp=s;
				s=t;
				t=temp;
			}

			s=(s+1)/2;

			t=(t+1)/2;

			for( int j=s; j<=t; j++) {
				ro[j]++;
				if(ro[j]>max) {		//路过的最大次数*10就是最少所需时间
					max=ro[j];
				}
			}
		}

		cout<<max*10<<endl;
	}
	return 0;
}

10.2HDOJ1051木棒问题(贪心算法)

题意:机器重启一次要1分钟 ,下一根木棒比上一根木棒长且重就可以通过,不然机器要重启

思路:1.为木棒定义结构体记录长、重,自定义函数comp将木棒按照长度由小到大排列,长度一样就按照重量比较

**2.两个for循环筛掉下一根木棒比上一根木棒轻的(重的就通过了,不重启,标记此木棒-1,代表已经用过),并且用sum标记重启次数 **

Problem Description

There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:

(a) The setup time for the first wooden stick is 1 minute.
(b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l’ and weight w’ if l<=l’ and w<=w’. Otherwise, it will need 1 minute for setup.

You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, …, ln, wn, each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.

Output

The output should contain the minimum setup time in minutes, one per line.

Sample Input

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

Sample Output

2
1
3
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct wo {
	int l;
	int w;
};
bool comp(wo a,wo b ) {      //将木棒排序,排序按照l从大到小。若l一致,就比较w 
	if(a.l!=b.l) return a.l<b.l;
	else return a.w<b.w;
}

int main(int argc, char** argv) {
	int t,n,l,w;
	wo wood[5005];
	cin>>t;
	getchar();
	while(t--) {
		cin>>n;
		getchar();
		int i=0;
		for(int i=0;i<n;i++){
			cin>>wood[i].l>>wood[i].w;
		}
		sort(wood,wood+n,comp);    //按照cmp的方式排序 
		int min,sum=0;
		
		for(int j=0;j<n;j++){
			if(wood[j].w!=-1){    	//wood[i].w==-1代表此条木棒已经用过了 
				min=wood[j].w;
				sum++;
				for(int k=j+1;k<n;k++){
					if(wood[k].w>=min&&wood[k].w!=-1){
						min=wood[k].w;
						wood[k].w=-1;
					}
				}
			}
		}
		
		cout<<sum<<endl;

	}

	return 0;
}

Sample Output

10
20
30
#include <iostream>
#include <string.h>
#include <math.h>
#include <cstring>
#include <cstdio>
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,n,s1,t1,ro[405],s,t;//ro表示原来的样子,s,t,表示从s移到t;
	int temp,max;
	cin>>T;
	getchar();
	while(T--) {
		cin>>n;
		getchar();
		for(int i=0; i<405; i++) {
			ro[i]=0;   //初始化路过次数
		}
	
		max=0;
		for(int i=0; i<n; i++) {

			cin>>s>>t;
			
			if(s>t) { //使得移动起始房间小于终点
				temp=s;
				s=t;
				t=temp;
			}

			s=(s+1)/2;

			t=(t+1)/2;

			for( int j=s; j<=t; j++) {
				ro[j]++;
				if(ro[j]>max) {		//路过的最大次数*10就是最少所需时间
					max=ro[j];
				}
			}
		}

		cout<<max*10<<endl;
	}
	return 0;
}

10.3HDOJ1051木棒问题(贪心算法)

题意:机器重启一次要1分钟 ,下一根木棒比上一根木棒长且重就可以通过,不然机器要重启

思路:1.为木棒定义结构体记录长、重,自定义函数comp将木棒按照长度由小到大排列,长度一样就按照重量比较

**2.两个for循环筛掉下一根木棒比上一根木棒轻的(重的就通过了,不重启,标记此木棒-1,代表已经用过),并且用sum标记重启次数 **

Problem Description

There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:

(a) The setup time for the first wooden stick is 1 minute.
(b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l’ and weight w’ if l<=l’ and w<=w’. Otherwise, it will need 1 minute for setup.

You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are (4,9), (5,2), (2,1), (3,5), and (1,4), then the minimum setup time should be 2 minutes since there is a sequence of pairs (1,4), (3,5), (4,9), (2,1), (5,2).

Input

The input consists of T test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line has an integer n , 1<=n<=5000, that represents the number of wooden sticks in the test case, and the second line contains n 2 positive integers l1, w1, l2, w2, …, ln, wn, each of magnitude at most 10000 , where li and wi are the length and weight of the i th wooden stick, respectively. The 2n integers are delimited by one or more spaces.

Output

The output should contain the minimum setup time in minutes, one per line.

Sample Input

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

Sample Output

2
1
3
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct wo {
	int l;
	int w;
};
bool comp(wo a,wo b ) {      //将木棒排序,排序按照l从大到小。若l一致,就比较w 
	if(a.l!=b.l) return a.l<b.l;
	else return a.w<b.w;
}

int main(int argc, char** argv) {
	int t,n,l,w;
	wo wood[5005];
	cin>>t;
	getchar();
	while(t--) {
		cin>>n;
		getchar();
		int i=0;
		for(int i=0;i<n;i++){
			cin>>wood[i].l>>wood[i].w;
		}
		sort(wood,wood+n,comp);    //按照cmp的方式排序 
		int min,sum=0;
		
		for(int j=0;j<n;j++){
			if(wood[j].w!=-1){    	//wood[i].w==-1代表此条木棒已经用过了 
				min=wood[j].w;
				sum++;
				for(int k=j+1;k<n;k++){
					if(wood[k].w>=min&&wood[k].w!=-1){
						min=wood[k].w;
						wood[k].w=-1;
					}
				}
			}
		}
		
		cout<<sum<<endl;

	}

	return 0;
}
发布了4 篇原创文章 · 获赞 0 · 访问量 62

猜你喜欢

转载自blog.csdn.net/qq_39814753/article/details/104278235
今日推荐