ACM入门练习-hdu

目录

A - A+B for Input-Output Practice (I)

B - A+B for Input-Output Practice (II)

C - A+B for Input-Output Practice (III)

D - A+B for Input-Output Practice (IV)

E - A+B for Input-Output Practice (V)

F - A+B for Input-Output Practice (VI)

G - A+B for Input-Output Practice (VII)

H - A+B for Input-Output Practice (VIII)

I - Download Manager

J - sort

 K - 人见人爱A+B

L - 人见人爱A-B

M - 人见人爱A^B


A - A+B for Input-Output Practice (I)

Your task is to Calculate a + b.
Too easy?! Of course! I specially designed the problem for acm beginners.
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.

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

Inputcopy Outputcopy
1 5 
10 20 
6 
30

理解 

 多组输入,输入A B,输出A+B

 代码

#include
int main()
{
	long long a,b;
	while(~scanf("%lld%lld",&a,&b)){
		printf("%lld\n",a+b);
	}
	return 0;
}

B - A+B for Input-Output Practice (II)

Your task is to Calculate a + b.

Input

Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair 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

Inputcopy Outputcopy
2
1 5 
10 20 
6 
30 

理解

输入N,后有N组A B,输出A+B

代码 

#include
int main()
{
	long long a,b,n;
	scanf("%lld",&n);
	while(n--){
		scanf("%lld%lld",&a,&b);
		printf("%lld\n",a+b);
	}
	return 0;
}

C - A+B for Input-Output Practice (III)

Your task is to Calculate a + b.

Input

Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.

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

Inputcopy Outputcopy
1 5 
10 20 
0 0 
6 
30

理解

输入多组A B,直至读取到0 0停止,输出A+B

代码 

#include
int main()
{
	long long a,b;
	while(~scanf("%lld%lld",&a,&b)){
		if(a==0&&b==0){
			break;
		}
		printf("%lld\n",a+b);
	}
	return 0;
}

D - A+B for Input-Output Practice (IV)

Your task is to Calculate the sum of some integers.

Input

Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.

Output

For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

Sample

Inputcopy Outputcopy
4 1 2 3 4 
5 1 2 3 4 5 
0 
10 
15 

理解 

多组输入,输入N及N个数,输出这N个数的和,直至N为0停止

代码 

#include
int main()
{
	long long n,z,a,i;
	while(~scanf("%lld",&n)){
		z=0;
		if(n==0){
			break;
		}
		for(i=1;i<=n;i++){
			scanf("%lld",&a);
			z+=a;}
		printf("%lld\n",z);
	}
	return 0;
}

E - A+B for Input-Output Practice (V)

Your task is to calculate the sum of some integers.

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 with one line of output for each line in input.

Sample

Inputcopy Outputcopy
2 
4 1 2 3 4 
5 1 2 3 4 5 
10 
15 

理解 

输入有N组数据,每组数据有M个数,输出这M个数据的和 

代码 

#include
int main()
{
	long long t,n,z,a,i;
	scanf("%lld",&t);
	while(t--){
	    z=0;
	    scanf("%lld",&n);
		for(i=1;i<=n;i++){
			scanf("%lld",&a);
			z+=a;}
		printf("%lld\n",z);
	}   
	return 0;
}

F - A+B for Input-Output Practice (VI)

Your task is to calculate the sum of some integers.

Input

Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.

Output

For each test case you should output the sum of N integers in one line, and with one line of output for each line in input.

Sample

Inputcopy Outputcopy
4 1 2 3 4 
5 1 2 3 4 5 
10 
15 

理解

 多组输入,每组数据有N个数,输出这N个数据的和 

代码 

#include
int main()
{
	long long n,z,a,i;
	while(~scanf("%lld",&n)){
		z=0;
		for(i=1;i<=n;i++){
			scanf("%lld",&a);
			z+=a;}
		printf("%lld\n",z);
	}   
	return 0;
}

G - A+B for Input-Output Practice (VII)

Your task is to Calculate a + b.

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, and followed by a blank line.

Sample

Inputcopy Outputcopy
1 5 
10 20 
6 

30 

理解

  多组输入,每组数据有N个数,输出这N个数据的和 ,后有空行

代码 

#include
int main()
{
	long long a,b;
	while(~scanf("%lld%lld",&a,&b)){
		printf("%lld\n\n",a+b);
	}   
	return 0;
}

H - A+B for Input-Output Practice (VIII)

Your task is to calculate the sum of some integers.

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

Inputcopy Outputcopy
3
4 1 2 3 4 
5 1 2 3 4 5 
3 1 2 3 
10
 
15 

6 

理解

 N组输入,每组数据有M个数,输出这M个数据的和 ,后有空行

代码 

#include
int main()
{
	long long a,z,t,n,i;
	scanf("%lld",&t);
	while(t--){
		z=0;
		scanf("%lld",&n);
		for(i=0;i

I - Download Manager

Jiajia downloads a lot, a lot more than you can even imagine. Some say that he starts downloading up to 20,000 files together. If 20,000 files try to share a limited bandwidth then it will be a big hazard and no files will be downloaded properly. That is why, he uses a download manager.

If there are T files to download, the download manger uses the following policy while downloading files:

1. The download manager gives the smaller files higher priority, so it starts downloading the smallest n files at startup. If there is a tie, download manager chooses the one with less bytes remaining (for download). We assume that with at least 50 Mega Bytes/sec of bandwidth, n files can be downloaded simultaneously without any problem.

2. The available bandwidth is equally shared by the all the files that are being downloaded. When a file is completely downloaded its bandwidth is instantaneously given to the next file. If there are no more files left except the files that are being downloaded, this bandwidth is immediately shared equally by all remaining files that are being downloaded.

Given the size and completed percentage of each file, your task is to intelligently simulate the behavior of the download manager to find the total time required to download all the files.

Input

The will be at most 10 test cases. Each case begins with three integers T (1 <= T <= 20000), n (1 <= n <= 2000 and 1 <= n <= T) and B (50 <= B <= 1000). Here B denotes the total bandwidth available to Jiajia (In Megabytes/sec). Please note that the download manager always downloads n files in parallel unless there are less than n files available for download. Each of next T lines contains one non-negative floating-point number S (less than 20,000, containing at most 2 digits after the decimal places) and one integer P (0 <= P <= 100). These two numbers denote a file whose size is S megabyte and which has been downloaded exactly P% already. Also note that although theoretically it is not possible that the size of a file or size of its remaining part is a fraction when expressed in bytes, for simplicity please assume that such thing is possible in this problem. The last test case is followed by T=n=B=0, which should not be processed.

Output

For each case, print the case number and the total time required to download all the files, expressed in hours and rounded to 2 digits after the decimal point. Print a blank line after the output of each test case.

Sample

Inputcopy Outputcopy
6 3 90 
100.00 90 
40.40 70 
60.30 70 
40.40 80 
40.40 85 
40.40 88 
1 1 56 
12.34 100 
0 0 0
Case 1: 0.66 

Case 2: 0.00

Explanation

In the first sample, there are 6 files and the download manager can download 3 files simultaneously. The size of the smallest file is 40.40 Megabyte but there are
four such files (2nd, 4th, 5th and 6th files). So the download manager chooses the 6th, 5th and 4th files for download as they have less bytes remaining. All these
files get equal bandwidth (30.00 Megabyte/Sec). Of these three files the 8th file is finished first. So instantaneously the 2nd file starts downloading. Then, 5th file
is finished. So the next larger file (3rd file) starts downloading. This process goes on until all files are downloaded.

题目大意

模拟下载器下载文件,每次下次从最小的开始,并且每次最多同时下载三个文件,带宽一定,平均分配,求总时间。按照优先规则排序,然后可以维护一个长度为m的数组,记录该文件剩余的字节,每次一定是字节最少的文件行下载完累加所需时间,然后更新剩余正在下载的m-1个文件的字节,直到所有文件都下载完,最后累加时间即为输出结果。

理解 

·下载的总量一定,总的下载速度及带宽也一定,消耗时间不受文件下载的先后顺寻影响。

多组数据,输入总任务数T,可同时下载任务数n,总带宽B,T组的任务大小s及已下载p%,

求出下载任务余量,除总带宽,输出case是第几组数据及下载任务所需时间。

代码 

#include 
int main()
{
    int T,n,B;
	int num=0;
    while(scanf("%d%d%d",&T,&n,&B))
    {
        if(T==0&&n==0&&B==0){
        	break;
		}
        int p;
        double s,sum=0;
        for(int i=0;i

 J - sort

给你n个整数,请按从大到小的顺序输出其中前m大的数。

Input

每组测试数据有两行,第一行有两个数n,m(0

Output

对每组测试数据按从大到小的顺序输出前m大的数。

Sample

Inputcopy Outputcopy
5 3 
3 -35 92 213 -644 
213 92 3

注意

本题用冒泡排序会超时

理解 

可用sort快速排序或哈希算法 

代码 

  sort快速排序

#include
using namespace std;
const int N = 1e6 + 5;  
int a[N];     
int n,m; 

int main(){
	while(~scanf("%d%d", &n, &m)){ 
		for(int i = 0; i < n; i++){   
			scanf("%d", &a[i]);
		}
		sort(a ,a + n);    
		for(int i = n - 1; i >= n-m; i--){
			if(i == n-m) printf("%d\n", a[i]);
			else printf("%d ", a[i]);
		}	
	}
	return 0;
}

 哈希算法

#include
#include
const int N=1e6+5;
int a[N];
int main()
{
	int n,m,i;
	while(~scanf("%d%d",&n,&m)){
		for(i=0;i=0;i--){  //遍历数组
			if(a[i]){    //如果a[i]不为0
				printf("%d",i-500000);
				k++;
				if(k==m){  //输出m个停止
					printf("\n");
					break;
				 }
				else{
					printf(" ");
				}
			}
		}
	}
	return 0;
}

 K - 人见人爱A+B

HDOJ上面已经有10来道A+B的题目了,相信这些题目曾经是大家的最爱,希望今天的这个A+B能给大家带来好运,也希望这个题目能唤起大家对ACM曾经的热爱。
这个题目的A和B不是简单的整数,而是两个时间,A和B 都是由3个整数组成,分别表示时分秒,比如,假设A为34 45 56,就表示A所表示的时间是34小时 45分钟 56秒。

Input

输入数据有多行组成,首先是一个整数N,表示测试实例的个数,然后是N行数据,每行有6个整数AH,AM,AS,BH,BM,BS,分别表示时间A和B所对应的时分秒。题目保证所有的数据合法。

Output

对于每个测试实例,输出A+B,每个输出结果也是由时分秒3部分组成,同时也要满足时间的规则(即:分和秒的取值范围在0~59),每个输出占一行,并且所有的部分都可以用32位整数表示。

Sample

Inputcopy Outputcopy
2
1 2 3 4 5 6 
34 45 56 12 23 34 
5 7 9 
47 9 30

理解 

输入N组数据,将每组AH,AM,AS,BH,BM,BS时分转成秒,累加后再转回时分秒形式输出。

 代码

#include
int main()
{
	int  n,AH,AM,AS,BH,BM,BS,s,f,m;
	long long z; //防止数据太大溢出
	scanf("%d",&n);
	while(n--){
	    scanf("%d %d %d %d %d %d",&AH,&AM,&AS,&BH,&BM,&BS);
	    z=(AH+BH)*3600+(AM+BM)*60+(AS+BS); //转成秒
	    m=z%60;
	    s=z/3600;
	    f=(z-m-s*3600)/60;  //转回时分秒
	    printf("%d %d %d\n",s,f,m);
	}
	return 0;
}

L - 人见人爱A-B

参加过上个月月赛的同学一定还记得其中的一个最简单的题目,就是{A}+{B},那个题目求的是两个集合的并集,今天我们这个A-B求的是两个集合的差,就是做集合的减法运算。(当然,大家都知道集合的定义,就是同一个集合中不会有两个相同的元素,这里还是提醒大家一下)

呵呵,很简单吧?

Input

每组输入数据占1行,每行数据的开始是2个整数n(0<=n<=100)和m(0<=m<=100),分别表示集合A和集合B的元素个数,然后紧跟着n+m个元素,前面n个元素属于集合A,其余的属于集合B. 每个元素为不超出int范围的整数,元素之间有一个空格隔开.
如果n=0并且m=0表示输入的结束,不做处理。

Output

针对每组数据输出一行数据,表示A-B的结果,如果结果为空集合,则输出“NULL”,否则从小到大输出结果,为了简化问题,每个元素后面跟一个空格.

Sample

Inputcopy Outputcopy
3 3 1 2 3 1 4 7 
3 7 2 5 8 2 3 4 5 6 7 8 
0 0 
2 3 
NULL

理解  

输出集合A中不于集合B中元素重复的剩余元素,无剩余元素则输出NULL

代码 

#include
#include  //使用qsort函数的头文件
int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}  //数组排序
int main()
{
	int n,m,i,j;
	int a[120],b[120],c[120];
	while(~scanf("%d%d",&n,&m)){
		int z=n;
		if(n==0&&m==0){
			break;
		}
	    for(i=0;i

M - 人见人爱A^B

求A^B的最后三位数表示的整数。
说明:A^B的含义是“A的B次方”

Input

输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。

Output

对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。

Sample

Inputcopy Outputcopy
2 3 
12 6 
6789 10000 
0 0 
8 
984 
1 

理解 

A的B次方小于1000直接输出,大于1000就%1000输出

代码 

#include
int main()
{
	long long cj; 
	int a,b,i;
	while(~scanf("%d%d",&a,&b)){
		if(a==0&&b==0){
			break;
		}
		
		cj=a;
		for(i=1;i1000) {
				cj%=1000;  //若不先%1000,则会溢出
			}
		}
	    printf("%d\n",cj);
	}
	
	return 0;
}

qsort函数

qsort函数使用方法总结(详细全面+代码) - 知乎 (zhihu.com)

你可能感兴趣的:(算法,c#)