# HDOJ 十天刷题及答案 c++

HDOJ 专栏收录该内容
3 篇文章 0 订阅

HDOJ10天刷题顺序

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

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;
}



#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

Input

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) {
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

90~100为A;
80~89为B;
70~79为C;
60~69为D;
0~59为E;

Input

Output

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

Output

Sample Input

1985/1/20
2006/3/12


Sample Output

20
71

#include <iostream>
using namespace std;
//judge leap year
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。

Input

Output

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

Output

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

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

Input

Output

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

Input

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

Output

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

Input

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

Input

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

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

Input

n=0表示输入数据的结束，不做处理。

Output

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

Input

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

Input

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

Input

n=0表示输入的结束，不做处理。

Output

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在一旁出主意。

Input

Output

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

Input

Output

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

Input

Output

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

Input

Output

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

Output

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

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

Input

Output

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

Output

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

Output

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

Sample Input

2
WaHaHa! WaHaHa! 今年过节不说话要说只说普通话WaHaHa! WaHaHa!



Sample Output

14
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) {
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

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

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

Input

Output

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

Input

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.

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.

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木棒问题（贪心算法）

**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木棒问题（贪心算法）

**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;
}
• 2
点赞
• 0
评论
• 12
收藏
• 一键三连
• 扫一扫，分享海报

08-31

09-12
11-12
03-05 251
11-13 960
02-22 869
03-04 334
10-17 127
09-27 5万+
02-22 857
10-16 1136
09-12
09-12
11-06
07-28 140
08-04 101