HDOJ ACM整学期完成题目(部分)(岭南师范学院)

1002 A + B Problem II

1003 Max Sum

1004 Let the Balloon Rise

1009 FatMouse’ Trade

1020 Encoding

1032 The 3n + 1 problem

1034 Candy Sharing Game

1040 As Easy As A+B

1048 The Hardest Problem Ever

1062 Text Reverse

1070 Milk

1097 A hard puzzle

1108 最小公倍数

1159 Common Subsequence

1257 最少拦截系统

1328 IBM Minus One

1331 Function Run Fun

2000 ASCII码排序

2001 计算两点间的距离

2002 计算球体积

2003 求绝对值

2004 成绩转换

2005 第几天?

2012 素数判定

2017 字符串统计

2021 发工资咯:)

2024 C语言合法标识符

2025 查找最大元素

2026 首字母变大写

2037 今年暑假不AC

2052 Picture

2084 数塔

2553 N皇后问题

1001/*In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
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<stdio.h>
int main(){
	
	int temp; 
	while(scanf("%d",&temp)!=EOF){
		int SUM = 0;
		for(int i = 1 ; i <= temp ; i++){
			SUM += i ; 
		}  
		printf("%d\n\n",SUM); 
	} 
	return 0; 
} 

1002A + B Problem II

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 545155 Accepted Submission(s): 104102
*

Problem Description

I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

Output

For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

Sample Input

2
1 2
112233445566778899 998877665544332211

Sample Output

Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
#include<stdio.h>
#include<string.h>

#define MAX 1000
#define T 20

void Add(char* a,char* b)
{
	char result[MAX];
	int an=strlen(a);//计算a字符串的长度 
	int bn=strlen(b);//计算b字符串的长度 
	int x=0;		 //进位 
	int n=0;		 //n表示结果的当前位置 
	
	while(an!=0 && bn!=0){if((result[n++]=a[--an]+b[--bn]-'0'+x)>'9'){result[n-1]-=10;x=1;}else{x=0;}}
	//当两个字符串都存在字符时,末尾两数相加,当两数相加的和大于9的话就将这位的结果位的数-10然后进一
	//-'0'的目的是将字符的ascll码表中的前面的数消掉,如果规范的话就每个数都得-'0';
	//result[n++] = a[--an]-'0' + b[--bn]-'0' + x > '9'-'0'   
	while(an==0 && bn!=0){if((result[n++]=b[--bn]+x)>'9'){result[n-1]-=10;x=1;}else{x=0;}}
	while(bn==0 && an!=0){if((result[n++]=a[--an]+x)>'9'){result[n-1]-=10;x=1;}else{x=0;}}
	if(x==1){result[n++]='1';}
    result[n]=0;//存放的是一个ASCll码表的0;就是null 
	
	while(n!=0){printf("%c",result[--n]);} printf("\n");//逆置输出结果 
}

int main()
{
	int n;
	scanf("%d",&n);
	
	char a[T][MAX],b[T][MAX];	
	
	int i;
	for(i=0;i<n;i++)
		scanf("%s %s",a[i],b[i]);
	
	for(i=0;i<n;i++){
		printf("Case %d:\n",i+1);
		printf("%s",a[i]);
		printf(" + ");
		printf("%s",b[i]);
		printf(" = ");
		Add(a[i],b[i]);
		if(i!=n-1) printf("\n");}
		
	return 0;
}

1003Max Sum

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 364348 Accepted Submission(s): 87140
*

Problem Description

Given a sequence a[1],a[2],a[3]…a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output

For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input

2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5

Sample Output

Case 1:
14 1 4

Case 2:
7 1 6
#include <iostream>
using namespace std;
 
int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
        int n;
        cin>>n;
        int sum = 0, max = -99999;
        int curhead=1, rear=1, head=1;
        for(int j=0;j<n;j++){
            int temp;
            cin>>temp;
            if(sum<0){
                curhead = j+1;
                sum = temp;
 
            }else{
                sum += temp;
            }
            if(sum>max){
                rear = j + 1;
                head = curhead;
                max = sum;
            }
        }
        cout<<"Case "<<i+1<<":"<<endl;
        cout<<max<<' '<<head<<' '<<rear<<endl;
        if(i!=t-1)
            cout<<endl;
    }
}

Let the Balloon Rise

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 185881 Accepted Submission(s): 74402
*

Problem Description

Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges’ favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.

Input

Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) – the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.

Output

For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.

Sample Input

5
green
red
blue
red
red
3
pink
orange
pink
0

Sample Output

red
pink

#include <iostream>
#include <string>
#include <map>
using namespace std; 
map<string,int>Ballon;
int main(){
	int N;
	while((cin >> N)&&N!=0){
		getchar(); 
		while(N--){//输入map 
			string str;
			cin >> str; 
			if(!Ballon[str])Ballon[str]=1;
			else Ballon[str]++; 
		} 
		int max = 0; 
		string maxstr; 
		//遍历map 
		map<string, int>::reverse_iterator   iter;
	    for(iter = Ballon.rbegin(); iter != Ballon.rend(); iter++){
	          //cout<<iter->first<<" "<<iter->second<<endl;
	          if(iter->second>=max){
			  	max = iter->second;
			  	maxstr = iter->first; 
			  } 
	    } 
	    cout<<maxstr<<endl; 
	    Ballon.clear(); 
	} 
	return 0; 
} 

1009FatMouse’ Trade

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 123801 Accepted Submission(s): 42781
*

Problem Description

FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.

Input

The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.

Output

For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.

Sample Input

5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

Sample Output

13.333
31.500

#include<iostream>
#include <algorithm>
using namespace std;
struct node{
	double J;
	double F;
	double price;	
}; 
bool cmp(node A,node B){
	return A.price>B.price; 
} 
int main(){
	int M,N;
	double count; 
	while(cin>>M&&cin>>N){
		count=0; 
		if(M==-1&&N==-1){
			break; 
		} 
	 	node array[N]; 
	 	for(int i=0;i<N;i++){
			cin>>array[i].J;
			cin>>array[i].F;
			array[i].price = array[i].J/array[i].F; 
		}//打表完成,接下来排序既可 
		 sort(array,array+N,cmp);
//		  for(int i=0;i<N;i++){
//		  	cout<<array[i].J<<"\t";
//			cout<<array[i].F<<"\t";
//			cout<<array[i].price<<endl; 
//		  } 测试打表是否成功 
		for(int i=0;i<N;i++){
			if(M<=0)break;//如果没有猫粮了就退出
			if(M>=array[i].F){
				count +=array[i].J;
				M -= array[i].F;
			} else{ 
				count += M*array[i].price; 
				M = 0;
			} 
		} 
		printf("%.3lf\n",count); 
	} 
	return 0; 
} 

1020Encoding

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 69868 Accepted Submission(s): 31001
*

Problem Description

Given a string containing only ‘A’ - ‘Z’, we could encode it using the following method:

\1. Each sub-string containing k same characters should be encoded to “kX” where “X” is the only character in this sub-string.

\2. If the length of the sub-string is 1, ‘1’ should be ignored.

Input

The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only ‘A’ - ‘Z’ and the length is less than 10000.

Output

For each test case, output the encoded string in a line.

Sample Input

2
ABC
ABBCCC

Sample Output

ABC
A2B3C

#include<iostream> 
#include <string> 
using namespace std;
int main(){
	int T;
	string str;
	string tempstr; 
	char temp;
	int count=0; 
	cin >>T;
	getline(cin,str);//吸收回车 
	while(T--){
		getline(cin ,str);
		temp = str[0]; 
		for(int i=0;i<str.length();i++){//遍历str 
			if(temp == str[i]){
				count++;  
			}
			else {
				if(count!=1)cout << count; 
				cout <<temp;
				count = 1;
				temp = str[i];   
			} 
			if(i==str.length()-1){
				if(count!=1) cout << count;
				cout <<temp;
			} 
		}
		cout << endl; 
		count = 0; 
	}
	return 0;
}
  

1032The 3n + 1 problem

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 58444 Accepted Submission(s): 21181
*

Problem Description

Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs.

Consider the following algorithm:

\1. input n

\2. print n

\3. if n = 1 then STOP

\4. if n is odd then n <- 3n + 1

\5. else n <- n / 2

\6. GOTO 2
Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.

Input

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

You can assume that no opperation overflows a 32-bit integer.

Output

For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line).

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174

/*
这个题很水,主要是题意。还这个题涉及到了3*n+1问题:
3n+1问题是一个简单有趣而又没有解决的数学问题。这个问题是由L. Collatz在1937年提出的。克拉兹问题(Collatz problem)也被叫做hailstone问题、3n+1问题、Hasse算法问题、Kakutani算法问题、Thwaites猜想或者Ulam问题。

  问题如下:

  (1)输入一个正整数n;

  (2)如果n=1则结束;

  (3)如果n是奇数,则n变为3n+1,否则n变为n/2;

  (4)转入第(2)步。

  克拉兹问题的特殊之处在于:尽管很容易将这个问题讲清楚,但直到今天仍不能保证这个问题的算法对所有可能的输入都有效——即至今没有人证明对所有的正整数该过程都终止。
题意:
题的大意是输入两个数(注意,这里没有说第一个数一定要小与第二个数),然后对这两个数之间的所有整数包括端点的数,进行一种规律运算,并求出运算的次数,比较所有的数规律运算次数,输出最大的.
*/ 
#include <iostream> 
using namespace std;
int function(int n){//返回数字的循环长度 
	int count = 0; 
	while(n!=1){
		if(n%2!=0) n=3*n+1;
		else n=n/2;
		count++; 
	} 
	return ++count; 
} 
int main(){
	int i,j,max=0,temp;
	while(cin>>i>>j){ 
		if(i<=j){
			for(int k=i;k<=j;k++){
				  temp = function(k);
				  if(temp>max)max = temp; 
			} 
		} else{
			for(int k = j;k<=i;k++) {
				temp = function(k);
				if(temp>max)max = temp; 
			} 
		} 
		cout << i <<" "<< j <<" "<<max <<endl; 
		max = 0; 
	} 
	return 0; 
} 

1034Candy Sharing Game

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14198 Accepted Submission(s): 7863
*

Problem Description

A number of students sit in a circle facing their teacher in the center. Each student initially has an even number of pieces of candy. When the teacher blows a whistle, each student simultaneously gives half of his or her candy to the neighbor on the right. Any student, who ends up with an odd number of pieces of candy, is given another piece by the teacher. The game ends when all students have the same number of pieces of candy.
Write a program which determines the number of times the teacher blows the whistle and the final number of pieces of candy for each student from the amount of candy each child starts with.

Input

The input may describe more than one game. For each game, the input begins with the number N of students, followed by N (even) candy counts for the children counter-clockwise around the circle. The input ends with a student count of 0. Each input number is on a line by itself.

Output

For each game, output the number of rounds of the game followed by the amount of candy each child ends up with, both on one line.

Sample Input

6
36
2
2
2
2
2
11
22
20
18
16
14
12
10
8
6
4
2
4
2
4
6
8
0

Sample Output

15 14
17 22
4 8

Hint
The game ends in a finite number of steps because:
1. The maximum candy count can never increase.
2. The minimum candy count can never decrease.
3. No one with more than the minimum amount will ever decrease to the minimum.
4. If the maximum and minimum candy count are not the same, at least one student with the minimum amount must have their count increase.

#include<iostream>
#include<algorithm>
#include<vector> 
using namespace std;
std::vector<int> myvector; 
void traversal (int i){
	std::cout<< " " <<i; 
} 
void CandyPlusOne(){
	for(int i=0;i<myvector.size();i++){
		if(myvector[i]%2!=0){
			myvector[i]++;
		}
	}
}
void DivideCandy (){
	int temp_this=myvector[0];
	int temp_pro;
	for(int i=1;i<myvector.size();i++){
		temp_pro=temp_this;
		temp_this=myvector[i];
		myvector[i]=temp_this/2+temp_pro/2;
	} 
	myvector[0]=myvector[0]/2+temp_this/2;
} 
bool isInTheEndOfGame(){
	int temp=myvector[0];
	int flag=true;
	for(int i=0;i<myvector.size();i++){
		if(myvector[i]!=temp)flag=false;
	}
	return flag;
}
int main(){
	int N;//The Number N is the Number of student.
	int candyNumber;  
	while(cin >> N&&N!=0){
		int count=0;
		while(N--){//将数据存入向量 
			cin>>candyNumber; 
			myvector.push_back(candyNumber) ; 
		} 
		while(!isInTheEndOfGame()){
			count++;
			DivideCandy();
			CandyPlusOne();
		}
		cout << count<<" "<<myvector[0]<<endl;
		myvector.clear();
	}  
	return 0; 
} 

1040As Easy As A+B

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 94999 Accepted Submission(s): 39771
*

Problem Description

These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.
Give you some integers, your task is to sort these number ascending (升序).
You should know how easy the problem is now!
Good luck!

Input

Input contains multiple 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 an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line.
It is guarantied that all integers are in the range of 32-int.

Output

For each case, print the sorting result, and one line one case.

Sample Input

2
3 2 1 3
9 1 4 7 2 5 8 3 6 9

Sample Output

1 2 3
1 2 3 4 5 6 7 8 9

/*Problem Description
These days, I am thinking about a question, how can I get a problem as easy as A+B? 
It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.
Give you some integers, your task is to sort these number ascending (升序).
You should know how easy the problem is now!
Good luck!
 

Input
Input contains multiple 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 an integer N (1<=N<=1000 the number of integers to be sorted) 
and then N integers follow in the same line.
It is guarantied that all integers are in the range of 32-int.


Output
For each case, print the sorting result, and one line one case.
 

Sample Input
2
3 2 1 3
9 1 4 7 2 5 8 3 6 9
 

Sample Output
1 2 3
1 2 3 4 5 6 7 8 9
*/
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 1010;
int num[maxn];

int main()
{
    int T;  cin>>T; 
    while(T--)
    {
        int N ; scanf("%d",&N);
		for(int i=0 ; i<N ; i++){
			cin>>num[i]; 
		} 
		sort(num,num+N);
		for(int i=0 ; i<N ; i++){
			if(i) cout << " ";
			cout << num[i];  
		} 
		cout << endl; 
    }
    return 0;
}

1048The Hardest Problem Ever

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38023 Accepted Submission(s): 17648
*

Problem Description

Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that no one could figure it out without knowing how it worked.
You are a sub captain of Caesar’s army. It is your job to decipher the messages sent by Caesar and provide to your general. The code is simple. For each letter in a plaintext message, you shift it five places to the right to create the secure message (i.e., if the letter is ‘A’, the cipher text would be ‘F’). Since you are creating plain text out of Caesar’s messages, you will do the opposite:

Cipher text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character should remain the same, and all alphabetical characters will be upper case.

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. All characters will be uppercase.

A single data set has 3 components:

Start line - A single line, “START”

Cipher message - A single line containing from one to two hundred characters, inclusive, comprising a single message from Caesar

End line - A single line, “END”

Following the final data set will be a single line, “ENDOFINPUT”.

Output

For each data set, there will be exactly one line of output. This is the original message by Caesar.

Sample Input

START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT

Sample Output

IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string> 
using namespace std;
char s[100],s1[100],s2[100];
int main()
{
	while(gets(s1)&&strcmp(s1,"START")==0){
		gets(s);
		int n =strlen(s);
		for(int i = 0;i<n;i++){
			if(s[i]>='A'&&s[i]<='Z')
		       s[i]=(s[i]-'A'+21)%26+'A';
			   //如果是 s[i]=(s[i]-'A'-5)%26+'A';
			   //则会出现错误,原因是求余运算的商向负无穷取整 
			   //例如:-11%10 = -1; -5%10=-5; 
		}
		while(gets(s2)&&strcmp(s2,"END")==0){
			puts(s);
			break;
		}
	}
	return 0;
}


1062Text Reverse

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 58616 Accepted Submission(s): 22545
*

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> 
using namespace std;
void printreverse(string str,int start,int end){
	for(int i=end;i>=start;i--){
		cout << str[i]; 
	} 
} 
void printspace(){
	cout<<" "; 
} 
int main(){
	int N;
	while(cin>>N){
		getchar();
		while(N--){
			string str;
			int start=0; 
			getline(cin,str); 
			int end; 
		//	cout << str<<endl; 
			for(int i=0;i<str.length();i++){
	 			if(isspace(str[i])){
					 end=i-1;
					 printreverse(str,start,end);
					 printspace(); 
					 start=i+1; 
				} 
				if(i==str.length()-1){
					end=str.length()-1; 
					printreverse(str,start,end); 
				} 
			} 
			cout<<endl; 
		} 
	} 
	return 0; 
} 

1070Milk

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35910 Accepted Submission(s): 9980
*

Problem Description

Ignatius drinks milk everyday, now he is in the supermarket and he wants to choose a bottle of milk. There are many kinds of milk in the supermarket, so Ignatius wants to know which kind of milk is the cheapest.

Here are some rules:
\1. Ignatius will never drink the milk which is produced 6 days ago or earlier. That means if the milk is produced 2005-1-1, Ignatius will never drink this bottle after 2005-1-6(inclusive).
\2. Ignatius drinks 200mL milk everyday.
\3. If the milk left in the bottle is less than 200mL, Ignatius will throw it away.
\4. All the milk in the supermarket is just produced today.

Note that Ignatius only wants to buy one bottle of milk, so if the volumn of a bottle is smaller than 200mL, you should ignore it.
Given some information of milk, your task is to tell Ignatius which milk is the cheapest.

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 starts with a single integer N(1<=N<=100) which is the number of kinds of milk. Then N lines follow, each line contains a string S(the length will at most 100 characters) which indicate the brand of milk, then two integers for the brand: P(Yuan) which is the price of a bottle, V(mL) which is the volume of a bottle.

Output

For each test case, you should output the brand of the milk which is the cheapest. If there are more than one cheapest brand, you should output the one which has the largest volume.

Sample Input

2
2
Yili 10 500
Mengniu 20 1000
4
Yili 10 500
Mengniu 20 1000
Guangming 1 199
Yanpai 40 10000

Sample Output

Mengniu
Mengniu

HintIn the first case, milk Yili can be drunk for 2 days, it costs 10 Yuan. Milk Mengniu can be drunk for 5 days, it costs 20 Yuan. So Mengniu is the cheapest.In the second case,
milk Guangming should be ignored. Milk Yanpai can be drunk for 5 days, but it costs 40 Yuan. So Mengniu is the cheapest.

1097A hard puzzle

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 61626 Accepted Submission(s): 22449
*

Problem Description

lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.
this puzzle describes that: gave a and b,how to know the a^b’s the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.

Input

There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)

Output

For each test case, you should output the a^b’s last digit number.

Sample Input

7 66
8 800

Sample Output

9
6

/* 找规律,四个一组 
1是1、1、1、1
2是2、4、8、6
3是3、9、7、1
4是4、6、4、6
5是5、5、5、5
6是6、6、6、6
7是7、9、3、1
8是8、4、2、6
9是9、1、9、1
*/
#include<iostream> 
using namespace std;
int main()
{
    long a,b;
    int temp;
    while(cin >> a ,cin >> b){
        a = a % 10;
        b = (b>4) ? b%4 : b;
        if(b == 0)
        {
            b = 4;
        }
        for(temp=1;b>0;b--)
        {
            temp = temp * a; 
        }
        cout << temp%10 <<endl; 
    }
   return 0;
}

1108最小公倍数

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 90937 Accepted Submission(s): 48664
*

Problem Description

给定两个正整数,计算这两个数的最小公倍数。

Input

输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.

Output

对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。

Sample Input

10 14

Sample Output

70

#include<iostream> 
using namespace std;
int gcd(int a,int b){
	if(b == 0){
		return a; 
	} else{
		return gcd(b,a%b); 
	} 
} 
int main(){
	int a,b;
	while(cin >> a,cin >> b) {
		int temp = gcd(a,b); 
		cout << a*b/temp<<endl; 
	} 
	return 0; 
} 

1159Common Subsequence

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 62422 Accepted Submission(s): 28944
*

Problem Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, …, xm> another sequence Z = <z1, z2, …, zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, …, ik> of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.

Sample Input

abcfbc abfcab
programming contest 
abcd mnp

Sample Output

4
2
0

#include<iostream>
#include<string> 
using namespace std;
int main(){
	string strA,strB; 
	while(cin>>strA>>strB){
		int lenA,lenB;//i行j列,其中A为行数(竖着写)B为列数(横着写) 
		lenA = strA.length();
		lenB = strB.length(); 
		int array[lenA+1][lenB+1] ; 
		for(int i=0;i<lenA+1;i++){//二重for循环打表 
			for(int j=0;j<lenB+1;j++){
				if(i==0||j==0){
					array[i][j] = 0; 
				}
				if(i>0&&j>0&&strA[i-1]==strB[j-1]){
					array[i][j] = array[i-1][j-1]+1; 
				}
				if(i>0&&j>0&&strA[i-1]!=strB[j-1]) {
					array[i][j] = array[i][j-1]>array[i-1][j]?array[i][j-1]:array[i-1][j]; 
				} 
			} 
		}
//		for(int i=0;i<lenA+1;i++){
//			for(int j=0;j<lenB+1;j++){
//				cout<<array[i][j]<<" "; 
//			} 
//			cout<<endl; 
//		} 
		cout<<array[lenA][lenB]<<endl; 
	} 
	return 0; 
} 

1257最少拦截系统

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 88837 Accepted Submission(s): 33955
*

Problem Description

某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.

Input

输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔

Output

对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.

Sample Input

8 389 207 155 300 299 170 158 65

Sample Output

2

#include<stdio.h>
int a[30001];//存储每个列表中最后一项(也是最小的一项)
int main()
{
    int n,i,j;
    while (~scanf("%d",&n))
    {
        int k=0;
        for (i = 0; i<n; i++)
        {
            scanf("%d",&a[k]);//作为哨兵
			j=0;
			while(a[k]>a[j])
				j++;
			a[j]=a[k];//将新值放入这个合适的列表(任意合适的列表都可以,这里取第一个合适的)
			k=(k==j)?k+1:k;//如果扫描到哨兵,说明前面的列表没有一个可以存放当前值,创建新列表
        }
        printf("%d\n",k);//输出最后的列表数
    }
    return 0;
}

1328IBM Minus One

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9529 Accepted Submission(s): 4518
*

Problem Description

You may have heard of the book ‘2001 - A Space Odyssey’ by Arthur C. Clarke, or the film of the same name by Stanley Kubrick. In it a spaceship is sent from Earth to Saturn. The crew is put into stasis for the long flight, only two men are awake, and the ship is controlled by the intelligent computer HAL. But during the flight HAL is acting more and more strangely, and even starts to kill the crew on board. We don’t tell you how the story ends, in case you want to read the book for yourself 😃

After the movie was released and became very popular, there was some discussion as to what the name ‘HAL’ actually meant. Some thought that it might be an abbreviation for ‘Heuristic ALgorithm’. But the most popular explanation is the following: if you replace every letter in the word HAL by its successor in the alphabet, you get … IBM.

Perhaps there are even more acronyms related in this strange way! You are to write a program that may help to find this out.

Input

The input starts with the integer n on a line by itself - this is the number of strings to follow. The following n lines each contain one string of at most 50 upper-case letters.

Output

For each string in the input, first output the number of the string, as shown in the sample output. The print the string start is derived from the input string by replacing every time by the following letter in the alphabet, and replacing ‘Z’ by ‘A’.

Print a blank line after each test case.

Sample Input

2
HAL
SWERC

Sample Output

String #1
IBM

String #2
TXFSD

#include<iostream>
#include<string> 
using namespace std;
int main(){
	int T;
	int count=0; 
	string str; 
	cin >> T;
	while(T--){
		count++; 
		cin >> str;
		for(int i=0;i<str.length();i++){
			if(str[i]!='Z')str[i] += 1; 
			else str[i] = 'A'; 
		}
		cout << "String #"; 
		cout << count << endl; 
		cout << str << endl; 
		cout << endl; 
	} 
	return 0; 
} 

1331Function Run Fun

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8711 Accepted Submission(s): 3925
*

Problem Description

We all love recursion! Don’t we?

Consider a three-parameter recursive function w(a, b, c):

if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:
1

if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:
w(20, 20, 20)

if a < b and b < c, then w(a, b, c) returns:
w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c)

otherwise it returns:
w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1)

This is an easy function to implement. The problem is, if implemented directly, for moderate values of a, b and c (for example, a = 15, b = 15, c = 15), the program takes hours to run because of the massive recursion.

Input

The input for your program will be a series of integer triples, one per line, until the end-of-file flag of -1 -1 -1. Using the above technique, you are to calculate w(a, b, c) efficiently and print the result.

Output

Print the value for w(a,b,c) for each triple

Sample Input

1 1 1
2 2 2
10 4 6
50 50 50
-1 7 18
-1 -1 -1 

Sample Output

w(1, 1, 1) = 2
w(2, 2, 2) = 4
w(10, 4, 6) = 523
w(50, 50, 50) = 1048576
w(-1, 7, 18) = 1

#include<iostream>
using namespace std;
int w[21][21][21];
void makeTable(){
	for(int i=0;i<=20;i++){
		for(int j=0;j<=20;j++){
			for(int k=0;k<=20;k++){
				if(i*j*k==0)w[i][j][k]=1;
				else if(i<j&&j<k)
					w[i][j][k]=w[i][j][k-1]+w[i][j-1][k-1]-w[i][j-1][k];
				else
					w[i][j][k]=w[i-1][j][k]+w[i-1][j-1][k]+w[i-1][j][k-1]-w[i-1][j-1][k-1];
			}
		}
	}
}
int main()
{
	makeTable();
	int i,j,k; 
	while(cin>>i>>j>>k&&!(i==-1&&j==-1&&k==-1)){
		cout<<"w("<<i<<", "<<j<<", "<<k<<") = ";
		if(i<=0||j<=0||k<=0)
			cout<<1<<endl;
		else if(i>20||j>20||k>20)
			cout<<w[20][20][20]<<endl;
		else
			cout<<w[i][j][k]<<endl;
	}
	return 0;
}

2000ASCII码排序

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 316837 Accepted Submission(s): 123016
*

Problem Description

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

Input

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

Output

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

Sample Input

qwe
asd
zxc

Sample Output

e q w
a d s
c x z

#include<iostream>
#include<algorithm> 
using namespace std; 
int main(){
	char array[3]; 
	while(cin >> array){ 
	sort(array,array+3); 
	for(int i = 0; i<3 ; i++){
		if(i!=2)cout << array[i]<<" "; 
		else cout << array[i] << endl; 
	} 
	} 
	return 0; 
} 

2001计算两点间的距离

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 384197 Accepted Submission(s): 129443
*

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<math.h> 
using namespace std;
int main(){
	double x1,x2,y1,y2; 
	while(scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2)!=EOF){
		float result = sqrt(pow((x1-x2),2)+pow((y1-y2),2)); 
		printf("%.2f\n",result); 
	} 
	return 0; 
} 

2002计算球体积

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 294322 Accepted Submission(s): 113671
*

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> 
#define PI 3.1415927
using namespace std;
int main(){
	double r,V; 
	while(scanf("%lf",&r)!=EOF){
		 V = 4/3.0 * PI * r * r * r;
		 printf("%.3lf\n",V); 
	} 
	return 0; 
} 

2003求绝对值

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 236542 Accepted Submission(s): 114008
*

Problem Description

求实数的绝对值。

Input

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

Output

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

Sample Input

123
-234.00

Sample Output

123.00
234.00

#include<iostream>
#include<cmath>
using namespace std;
int main(){
	double n; 
	while(scanf("%lf",&n)!=EOF){
		printf("%.2lf\n",fabs(n)); 
	} 
	return 0; 
} 

2004成绩转换

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 261714 Accepted Submission(s): 111607
*

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;
int main(){
	int score;
	while(scanf("%d",&score)!=EOF){
		if(score>100||score<0){
			cout << "Score is error!" <<endl; 
		} else{ 
		switch(score/10){
			case 10:
			case 9:cout << "A" <<endl;break;
			case 8:cout << "B" <<endl;break;
			case 7:cout << "C" <<endl;break;
			case 6:cout << "D" <<endl;break; 
			default:cout << "E" <<endl; 
			} 
		
		} 
	} 
	return 0; 
} 

2005第几天?

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 278014 Accepted Submission(s): 95438
*

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;
bool isLeapYear(int year){
	if (year%400==0 || (year%4==0 && year%100!=0)) return true;
	else return false;  
} 
int main(){
	int year,month,day; 
	int monthList[12]={31,28,31,30,31,30,31,31,30,31,30,31} ;  
	while(scanf("%d/%d/%d",&year,&month,&day)!=EOF){
		if(isLeapYear(year)){
			monthList[1] =  29; 
		} else{
			monthList[1]= 28; 
		} 
		int temp = 0;
		for(int i=0;i<month-1;i++){
			temp += monthList[i]; 
		} 
		temp += day;
		printf("%d\n",temp); 
	} 
	return 0; 
} 

2012素数判定

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 245384 Accepted Submission(s): 86807
*

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;
bool isPrimeNumber(int n){
	bool flag=true; 
	for(int i=2;i<sqrt(n);i++){
		if(n%i==0)flag=false; 
	} 
	return flag; 
} 
int function(int n){
	return pow(n,2)+n+41; 
}  
int main(){
	int x,y;
	while(cin>>x>>y){
		if(x==0&&y==0) break; 
		bool flag=true; 
		for(int i=x;i<=y;i++){
			if(!isPrimeNumber(function(i)))flag=false; 
		} 
		if(flag==true)cout<<"OK"<<endl; 
		else cout << "Sorry"<<endl; 
	} 
	return 0; 
} 

2017字符串统计

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 147242 Accepted Submission(s): 80913
*

Problem Description

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

Input

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

Output

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

Sample Input

2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf

Sample Output

6
9

#include<iostream> 
#include<string> 
#include<algorithm> 
using namespace std;
int main(){
	int n;
	string str;
	int count = 0; 
	cin >> n; 
	while(n--){
		count = 0; 
		cin >> str;
		for(int i:str){
			if(i>='0'&&i<='9'){
				count++; 
			} 
		} 
		cout << count <<endl; 
	} 
	return 0 ; 
} 

2021发工资咯:)

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 113721 Accepted Submission(s): 59280
*

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>
#include<algorithm> 
#include<vector> 
using namespace std;
vector<int> coins;


int coinChange(vector<int>& coins, int amount) {// 数组??为 amount + 1,初始值也为 amount + 1
		vector<int> dp(amount + 1, amount + 1);
		dp[0] = 0;
		for (int i = 0; i < dp.size(); i++) {
		// 内层 for 在求所有?问题 + 1 的最?值
		for (int coin : coins) {// ?问题?解,跳过
			if (i - coin < 0) continue;
			dp[i] = min(dp[i], 1 + dp[i - coin]);
		}
	}
	return (dp[amount] == amount + 1) ? -1 : dp[amount];
}

int main(){
	coins.push_back(100);
	coins.push_back(50);
	coins.push_back(10);
	coins.push_back(5);
	coins.push_back(2);
	coins.push_back(1);
	int T,temp,sum; 
	while(cin>>T&&T!=0){
		sum = 0; 
		while(T--){
			cin>>temp;
			sum += coinChange(coins,temp);	
		} 
		cout<<sum<<endl; 
	} 
} 

2024C语言合法标识符

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 134860 Accepted Submission(s): 50715
*

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

using namespace std;

bool isLegal(string s){
	if (s[0] <= '9' && s[0] >= '0') return false;
	for (int i = 0; i < s.length(); i++){
		if (!(s[i] == '_' || s[i] <= '9' && s[i] >= '0' || s[i] <= 'z' && s[i] >= 'a' || s[i] <= 'Z' && s[i] >= 'A')){
			return false;
		}
	}
	return true;
}

int main()
{
	int n;
	string str;
	cin >> n;
	getline(cin, str);
	while (n--){
		getline(cin,str);
		if (isLegal(str)) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

2025查找最大元素

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 103957 Accepted Submission(s): 54052
*

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>
#include<string.h> 
#include<vector> 
using namespace std;
vector<string> v; 
/*
思路:1.接收字符串 
2.令第一个char为max
3.遍历选择排序,找出max
4.从max切开字符串,返回一个vector向量,里面存储着被分割的字符串 
*/ 
char finemaxchar(string str){
 char max = str[0]; 
 for(int i=0;i<str.length();i++){
  if(str[i]>max){max=str[i];} 
 } 
 return max; 
} 
vector<string> split(const string& str, const string& delim) {
 vector<string> res;
 if("" == str) return res;
 //先将要切割的字符串从string类型转换为char*类型
 char * strs = new char[str.length() + 1] ; //不要忘了
 strcpy(strs, str.c_str()); 
 
 char * d = new char[delim.length() + 1];
 strcpy(d, delim.c_str());
 
 char *p = strtok(strs, d);
 while(p) {
  string s = p; //分割得到的字符串转换为string类型
  res.push_back(s+delim+"(max)"); //存入结果数组
  p = strtok(NULL, d);
 }
    
    delete strs;
    delete d;
 return res;
}

int main(){
 string str;
 string max; 
 while(getline(cin,str)){
   max = finemaxchar(str); 
   std::vector<string> res = split(str,max);
   for(string i:res){
    cout << i; 
   } 
   cout <<endl; 
 } 
 return 0; 
} 

#include <iostream>
#include <string>
using namespace std;
char getMax(string& s){
	char M=s[0];
	for (int i = 0; i < s.length(); i++){
		if (s[i] > M) M = s[i];
	}
	return M;
}

void Full(string& s,char M){
	for (int i = 0; i < s.length(); i++){
		if (s[i] == M){
			s.insert(i + 1, "(max)");
			i = i + 5;
		}
	}
}

int main(){
	string s;
	while (cin >> s){
		char c = getMax(s);
		Full(s,c);
		cout <<s<< endl;
	}
}

2026首字母变大写

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 106603 Accepted Submission(s): 57828
*

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>
#include<string.h> 
#include<vector> 
using namespace std;
/*
思路: 1.接受
2.用split分隔开
3.对vector内的每个首字母变大写,遍历输出+空格 
*/ 
vector<string> split(const string& str, const string& delim) {
	vector<string> res;
	if("" == str) return res;
	//先将要切割的字符串从string类型转换为char*类型
	char * strs = new char[str.length() + 1] ; //不要忘了
	strcpy(strs, str.c_str()); 
 
	char * d = new char[delim.length() + 1];
	strcpy(d, delim.c_str());
 
	char *p = strtok(strs, d);
	while(p) {
		string s = p; //分割得到的字符串转换为string类型
		res.push_back(s); //存入结果数组
		p = strtok(NULL, d);
	}
    
    delete strs;
    delete d;
	return res;
}

int main(){
	string str; 
	string temp;
	vector<string> v; 
	while(getline(cin,str)){
		std::vector<string> res = split(str," ");
		for(int i = 0;i < res.size() ;i++){
		//	cout << (char)(res[i][0]-32) <<" "; 
		res[i][0]-=32; 
		} 
//		for(string j:res){
//			cout << j <<" "; 
//		}
		for(int j=0;j<res.size();j++){
			if(j!=res.size()-1){
				cout << res[j] << " "; 
			} else{
				cout << res[j]; 
			} 
		} 
		cout << endl;
	} 
	return 0; 
} 

2037今年暑假不AC

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 107073 Accepted Submission(s): 57077
*

Problem Description

“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%…”

确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)

Input

输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。

Output

对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。

Sample Input

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0

Sample Output

5

#include<stdio.h>
/*经典的贪婪算法练习题,贪婪算法简单来讲即自顶向下,求解出每个子最优解,且每个子问题不会对下一个问题产生影响
该题算法模型为:
1.若每次选取开始时间最早的节目,无法求出最优解
2.若每次选取播放时间最短的节目,无法求出最优解
3.若每次选取结束时间最短的节目,用数学归纳法可以求出最优解
算法设计:
1.采用结构体数组存放数据
2.用冒泡排序法对 节目结束时间 进行升序排序
3.遍历节目数组,找出每一个节目结束时间最早的节目,并且当前节目开始时间需大于等于上一个节目结束时间
4.计算出符合条件的节目总数
*/

struct{
    int start;
    int end;
}program[101];

int main(){
    int n;
    int i,j;
    int sum;

    while(scanf("%d",&n)!=EOF && n!=0){
        for(i=0;i<n;i++)
            scanf("%d%d",&program[i].start,&program[i].end);

        /*冒泡排序*/
        for(i=0;i<n;i++)
            for(j=0;j<n-i-1;j++)
                if(program[j].end>program[j+1].end){
                    program[100] = program[j];
                    program[j] = program[j+1];
                    program[j+1] = program[100];
                }

        /*求出最优解*/
        for(j=1,i=0,sum=1;j<n;j++)
            if(program[j].start >= program[i].end ){
                i = j;
                sum++;
            }

        printf("%d\n",sum);
    }

    return 0;
}

2052Picture

*Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 50953 Accepted Submission(s): 24483
*

Problem Description

Give you the width and height of the rectangle,darw it.

Input

Input contains a number of test cases.For each case ,there are two numbers n and m (0 < n,m < 75)indicate the width and height of the rectangle.Iuput ends of EOF.

Output

For each case,you should draw a rectangle with the width and height giving in the input.
after each case, you should a blank line.

Sample Input

3 2

Sample Output

+---+
|   |
|   |
+---+

#include<iostream>
using namespace std;
int main()
{
    int row,col;
    while(cin>>row>>col){
        for(int i=1;i<=col+2;i++){
            for(int j=1;j<=row+2;j++){
                if(i==1||i==col+2){
                    if(j==1||j==row+2)cout<<"+";
                    else cout<<"-";
                }
                else{
                    if(j==1||j==row+2)cout<<"|";
                    else cout<<" ";
                }
           }
            cout<<endl;
        }
        cout<<endl;
    }
    return 0;
}

2084数塔

*Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 82384 Accepted Submission(s): 47118
*

Problem Description

在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:

有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
img
已经告诉你了,这是个DP的题目,你能AC吗?

Input

输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内。

Output

对于每个测试实例,输出可能得到的最大和,每个实例的输出占一行。

Sample Input

1
5
7
3 8
8 1 0 
2 7 4 4
4 5 2 6 5

Sample Output

30

#include <iostream>  
#include <algorithm> 
using namespace std;

#define MAX 101

int D[MAX][MAX];    
int n;  
int maxSum[MAX][MAX];

int MaxSum(int i, int j){      
    if( maxSum[i][j] != -1 )         
        return maxSum[i][j];      
    if(i==n)   
        maxSum[i][j] = D[i][j];     
    else{    
        int x = MaxSum(i+1,j);       
        int y = MaxSum(i+1,j+1);       
        maxSum[i][j] = max(x,y)+ D[i][j];     
    }     
    return maxSum[i][j]; 
} 
int main(){    
    int i,j,T;
	cin>>T;    
    while(T--){
		cin >> n;    
    	for(i=1;i<=n;i++)   
	        for(j=1;j<=i;j++) {       
	            cin >> D[i][j];       
	            maxSum[i][j] = -1;   
	        }    
    cout << MaxSum(1,1) << endl; 
	} 
} 

2553N皇后问题

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 56902 Accepted Submission(s): 23417
*

Problem Description

在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上。
你的任务是,对于给定的N,求出有多少种合法的放置方法。

Input

共有若干行,每行一个正整数N≤10,表示棋盘和皇后的数量;如果N=0,表示结束。

Output

共有若干行,每行一个正整数,表示对应输入行的皇后的不同放置数量。

Sample Input

1
8
5
0

Sample Output

1
92
10

#include<iostream>
#include<stdio.h> 
#include<math.h> 
#include<string.h> 
using namespace std;
//广搜1430 
int ans[15],row;
int array[15];
 
bool place(int row){//输入行号 
	for(int i=1 ; i < row ;i++){//遍历输入行号前面所有的皇后,判断能否放下 
		if(array[i]==array[row] || (abs(array[i]-array[row])==abs(i-row))) {
		// 判断是否列冲突,判断是否对角线冲突
		//如果满足有列冲突或对角线冲突则反对false
		return false; 
		} 
		//遍历完所有的行数返回true 
	} 
	return true; 
}

void dfs(){
	for(int i=1 ; i<=10 ;i++){//遍历1到10皇后的所有情况 
		memset(array,0,sizeof(array)); 
		ans[i]=0;
		
		row = 1;//从第一行开始放置
		while(row > 0){//行数为0的时候,则已经枚举所有方案,退出循环 
			array[row]++;//第row行的皇后放入第一个位置开始试
			while((array[row] <= i)&&(!place(row))){
				//如果这个位置不能放置则向右移一位 
				array[row]++; 
			} 
			if(array[row] <= i){//如果当前行皇后能放置,array[row]必定小于棋盘格子数
				 if(row == i){
				 	ans[i]++; //n皇后全部放置,方案数+1 
				 } else{//未放完所有皇后 
				 	row++;//准备找下一行皇后
					array[row]=0;//初始化皇后 
				 } 
			} else{//如果整行都不能放置,则array[row]必定超过棋子格数,俗称爆格 
				row--;//爆格则回退到上一行继续找 ,回溯 
			} 
		} 
	} 
} 

int main(){
	int n;
	dfs();
	while(scanf("%d",&n) && n){
		printf("%d\n",ans[n]); 
	} 
	return 0; 
} 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ReactSpring

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值