量子团队的算法训练(20190508)

量子团队的算法训练(20190508)

在这里插入图片描述

#include<bits\stdc++.h>

using namespace std;

int main() {
	int n;
	while(cin>>n) {
		if(n%4 == 2) {
			cout<<"yes"<<endl;
		} else {
			cout<<"no"<<endl;
		}
	}
	return 0;
}

/*
1 10
100 200
201 210
900 1000
*/

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

#include<stdio.h>
int main()
{
	int n,m;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		int s,b;
		if(n>m)
		{
			s=m;
			b=n;
		}
		else
		{
			s=n;
			b=m;
		}
		int i,max=0;
		for(i=s;i<=b;i++)
		{
			int count=1;
			s=i;
			while(s!=1)
			{
				if(s%2!=0)
					s=3*s+1;
				else
					s=s/2;
				count++;
			}
			if(max<count)
				max=count;
		}
		printf("%d %d %d\n",n,m,max);
	}
	return 0;
}

C - Keep on Truckin’
Boudreaux and Thibodeaux are on the road again . . .

“Boudreaux, we have to get this shipment of mudbugs to Baton Rouge by tonight!”

“Don’t worry, Thibodeaux, I already checked ahead. There are three underpasses and our 18-wheeler will fit through all of them, so just keep that motor running!”

“We’re not going to make it, I say!”

So, which is it: will there be a very messy accident on Interstate 10, or is Thibodeaux just letting the sound of his own wheels drive him crazy?
Input
Input to this problem will consist of a single data set. The data set will be formatted according to the following description.

The data set will consist of a single line containing 3 numbers, separated by single spaces. Each number represents the height of a single underpass in inches. Each number will be between 0 and 300 inclusive.
Output
There will be exactly one line of output. This line will be:

NO CRASH

if the height of the 18-wheeler is less than the height of each of the underpasses, or:

CRASH X

otherwise, where X is the height of the first underpass in the data set that the 18-wheeler is unable to go under (which means its height is less than or equal to the height of the 18-wheeler).
The height of the 18-wheeler is 168 inches.
Sample Input
180 160 170
Sample Output
CRASH 160

#include<bits\stdc++.h>

using namespace std;

void fun(){
	int a,b,c;
	cin>>a>>b>>c;
	int h=168;
	if(a<=h){
		cout<<"CRASH "<<a<<endl;
		return;
	}
	else if(b<=h){
		cout<<"CRASH "<<b<<endl;
		return;
	}
	else if(c<=h){
		cout<<"CRASH "<<c<<endl;
		return;
	}
	else{
		cout<<"NO CRASH"<<endl;
		return;
	}
}
int main() {
	fun();
	return 0;
}

/*
180 160 170
*/

4.D - Moving Tables
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<bits\stdc++.h>

using namespace std;

int rooms[210]= {0};
int main() {
	int t,n,a,b,max;
	cin>>t;
	for(int i=0; i<t; i++) {
		max=0;
		for(int i=0; i<201; i++)
			rooms[i]=0;
		cin>>n;
		for(int i=0; i<n; i++) {
			cin>>a>>b;
			if(a>b) {
				int t=a;
				a=b,b=t;
			}
			for(int k=(a+1)/2; k<=(b+1)/2; k++)
				rooms[k]++;
		}
		for(int g=0; g<201; g++)
			if(rooms[g]>max)
				max=rooms[g];
		cout<<max*10<<endl;
	}
	return 0;
}

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

5.E - Max Sum
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<bits\stdc++.h>

using namespace std;

int main() {
	int t;  //有t组数据
	int n;  //每组数据中有n个元素
	int first,last;//代表每组数据最大子段的起始和终止位置
	int sum;  //记录子段的和
	int maxSum;///记录子段的最大值
	int temp;  //记录可能为最大子元素的起始位置
	int a[100005];
	cin>>t;
	for(int i=0; i<t; i++) {
		cin>>n;
		for(int j=0; j<n; j++) {
			cin>>a[j];
		}
		//初始化
		first=1;
		last=1;
		sum=0;
		maxSum=-1001;
		temp=1;
		//查找
		for(int k=0; k<n; k++) {
			sum+=a[k];
			if(sum>maxSum) {
				maxSum=sum;
				first=temp;
				last=k+1;   //k代表下标,最后一个位置为下标+1
			}
			if(sum<0) {
				sum=0;
				temp=k+2;
			}
		}
		//输出
		cout<<"Case "<<i+1<<":"<<endl;
		cout<<maxSum<<" "<<first<<" "<<last<<endl;
		if(i!=t-1) {
			cout<<endl;
		}
	}
	return 0;
}

/*
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
*/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值