天梯赛训练2020年1月11日

今年吗寒假在家不好玩,闲了来好好学习一些基础算法,努力向上😊😁
Given two integers n and m, count the number of pairs of integers (a,b) such that 0 < a < b < n and (a2+b2 +m)/(ab) is an integer.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.
Input
You will be given a number of cases in the input. Each case is specified by a line containing the integers n and m. The end of input is indicated by a case in which n = m = 0. You may assume that 0 < n <= 100.
Output
For each case, print the case number as well as the number of pairs (a,b) satisfying the given property. Print the output for each case on one line in the format as shown below.
Sample Input
1

10 1
20 3
30 4
0 0
Sample Output
Case 1: 2
Case 2: 4
Case 3: 5

核心思想就是遍历暴力求解,但是要注意输出格式的问题,每个样例要换一次行, 详见代码

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main() {
	int N;
	scanf("%d",&N);
	while(N--) {
		int cas=1;
		int n,m;
		while(~scanf("%d%d",&n,&m)&&(n+m)) {
			int cnt=0;
			for(int a=1; a<n; a++) {
				for(int b=a+1; b<n; b++) {
					if((a*a+b*b+m)%(a*b)==0)
						cnt++;
				}
			}
			printf("Case %d: %d\n",cas++,cnt);
		}
		if(N) printf("\n");
	}
	return 0;
}
/*测试样例1
1

10 1
20 3
30 4
0 0
*/

The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105.

Input
Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 … nm where m is the number of integers in the set and n1 … nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.
Output
For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.
Sample Input
2
3 5 7 15
6 4 10296 936 1287 792 1
Sample Output
105
10296
贪心思想求解

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int gcd(int a,int b) {
	if(b == 0)
		return a;
	else return gcd(b,a%b);
}
int lcm(int a,int b) {
	return a/gcd(a,b)*b;
}
int main() {
	int t,temp,n;
	scanf("%d",&t);
	for(int i=1; i <= t; i++) {
		scanf("%d",&n);
		int temp = 1;
		for(int j=1; j <= n ; j++) {
			int num;
			scanf("%d",&num);
			temp = lcm(temp,num);
		}
		printf("%d\n",temp);
	}
	return 0;
}
/*测试样例1
2
3 5 7 15
6 4 10296 936 1287 792 1
*/

There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
Input
Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).
Output
Print the word “yes” if 3 divide evenly into F(n).

Print the word “no” if not.
Sample Input
0
1
2
3
4
5
Sample Output
no
no
yes
no
no
no
先建一个表,然后直接通过表的值判断输出

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int f[1000100];
void fun() {
	f[0]=7;
	f[1]=11;
	for(int i=2; i<1000000; i++) {
		f[i]=f[i-1]+f[i-2];
		f[i]=f[i]%3;
	}
}
int main() {
	fun();
	int n;
	while(cin>>n) {
		if(f[n]==0) {
			cout<<"yes"<<endl;
		} else {
			cout<<"no"<<endl;
		}
	}
	return 0;
}
/*测试样例1
0
1
2
3
4
5
*/

“OK, you are not too bad, em… But you can never pass the next test.” feng5166 says.

“I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers.” feng5166 says.

“But what is the characteristic of the special integer?” Ignatius asks.

“The integer will appear at least (N+1)/2 times. If you can’t find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha…” feng5166 says.

Can you find the special integer for Ignatius?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
Sample Output
3
5
1
基数排序思想

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int N[999999];
int main() {
	int n;
	int i=0;
	while(cin>>n) {
		memset(N,0,sizeof(N));
		i=0;
		int mask=0;
		int temp=(n+1)/2;
		int num;
		while(n--) {
			cin>>num;
			N[num]++;
			if(N[num]>=temp) {
				mask=num;
			}
		}
		cout<<mask<<endl;
	}
	return 0;
}
/*测试样例1
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
*/

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;
typedef long long ll;
int main() {
	int a,b,c;
	while(cin>>a>>b>>c) {
		if(a<168) {
			printf("CRASH %d\n",a);
		}
		else if(b<168) {
			printf("CRASH %d\n",b);
		}
		else if(c<168) {
			printf("CRASH %d\n",c);
		}
	}
	return 0;
}
/*测试样例1
180 160 170
*/

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
输入后sort排序

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int a[1010];
int main() {
	int t;
	cin>>t;
	for(int i=0; i<t; i++) {
		int n;
		memset(a,0,sizeof(int)*n);
		cin>>n;
		for(int j=0; j<n; j++) {
			cin>>a[j];
		}
		sort(a,a+n);
		for(int j=0; j<n-1; j++) {
			cout<<a[j]<<' ';
		}
		cout<<a[n-1]<<endl;
	}
	return 0;
}
/*测试样例1
2
3 2 1 3
9 1 4 7 2 5 8 3 6 9
*/

How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We’re assuming that the cards must be perpendicular to the table.) With two cards you can make the top card overhang the bottom one by half a card length, and the bottom one overhang the table by a third of a card length, for a total maximum overhang of 1/2 + 1/3 = 5/6 card lengths. In general you can make n cards overhang by 1/2 + 1/3 + 1/4 + … + 1/(n + 1) card lengths, where the top card overhangs the second by 1/2, the second overhangs tha third by 1/3, the third overhangs the fourth by 1/4, etc., and the bottom card overhangs the table by 1/(n + 1). This is illustrated in the figure below.

The input consists of one or more test cases, followed by a line containing the number 0.00 that signals the end of the input. Each test case is a single line containing a positive floating-point number c whose value is at least 0.01 and at most 5.20; c will contain exactly three digits.

For each test case, output the minimum number of cards necessary to achieve an overhang of at least c card lengths. Use the exact output format shown in the examples.
Input
1.00
3.71
0.04
5.19
0.00
Output
3 card(s)
61 card(s)
1 card(s)
273 card(s)

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main() {
	double n,sum;
	while(cin>>n) {
		if(n<0.001)break;
		int i;
		sum=0.0;
		for(i=1; ; i++) {
			sum+=1.0/(i+1);
			if(sum>=n){
				break;
			}
		}
		printf("%d card(s)\n",i);
	}
	return 0;
}
/*测试样例1
1.00
3.71
0.04
5.19
0.00
*/

Given a positive integer N, you should output the most right digit of N^N.
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 positive integer N(1<=N<=1,000,000,000).
Output
For each test case, you should output the rightmost digit of N^N.
Sample Input
2
3
4
Sample Output
7
6

Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
利用快速幂取模算法

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int PowerMod(int a,int b,int c) {
	int ans=1;
	a=a%c;
	while(b>0) {
		if(b%2==1)
			ans=(ans*a)%c;
		b=b/2;
		a=(a*a)%c;
	}
	return ans;
}
int main () {
	int n,a;
	scanf("%d",&n);
	while(n--) {
		scanf("%d",&a);
		printf("%d\n",PowerMod(a,a,10));
	}
	return 0;
}
/*测试样例1
2
3
4
*/

给定两个正整数,计算这两个数的最小公倍数。
Input
输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.
Output
对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。
Sample Input
10 14
Sample Output
70
注意求最小公因数方法

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int gcd(int a,int b) {
	int t;
	while(b) {
		t = a % b;
		a = b;
		b = t;
	}
	return a;
}
int main() {
	int a,b;
	while(cin>>a>>b) {
		cout<<a*b/gcd(a,b)<<endl;
	}
	return 0;
}
/*测试样例1
10 14
*/
```、
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
Sample Output
3
-1
2
0.50
简单的分支判断就可以了
```c
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main() {
	int t,a,b;
	char m[2];
	scanf("%d",&t);
	while(t--) {
		scanf("%s%d%d",m,&a,&b);
		if(m[0]=='+') printf("%d\n",a+b);
		else if(m[0]=='-') printf("%d\n",a-b);
		else if(m[0]=='*') printf("%d\n",a*b);
		else if(m[0]=='/') {
			if(a%b==0) printf("%d\n",a/b);
			else printf("%.2f\n",(double)a/b);
		}
	}
	return 0;
}
/*测试样例1
4
+ 1 2
- 1 2
* 1 2
/ 1 2
*/

伊格纳修斯(Ignatius)出生于a年,所以他想知道什么时候可以举行生日聚会。你能告诉他吗?

给定正整数Y表示开始年,正整数N表示开始年,而您的任务是告诉Y年后的第N个leap年。

注意:如果Y年是a年,则第一个leap年就是Y年。
输入项
输入包含几个测试用例。输入的第一行是单个整数T,它是测试用例的数量。随后是T测试用例。
每个测试用例包含两个正整数Y和N(1 <= N <= 10000)。
输出量
对于每个测试用例,您应该从Y年输出第N个leap年。
样本输入
3
2005 25
1855 12
2004 10000
样本输出
2108
1904
43236

暗示
仅当(Y%4 == 0 && Y%100!= 0)或Y%400 == 0时,我们才将Y年称为leap年。
判断闰年记数就好了

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int judge(int y) {
	if((y%4==0&&y%100!=0)||(y%400==0))
		return 1;
	else return 0;
}
int main() {
	int t,n,y;
	cin>>t;
	while(t--) {
		int num=0;
		cin>>y>>n;
		if(judge(y)) {
			num++;
		}
		while(num!=n) {
			y++;
			if(judge(y)) {
				num++;
			}
		}
		cout<<y<<endl;
	}
	return 0;
}
/*测试样例1
3
2005 25
1855 12
2004 10000
*/

Superbowl Sunday is nearly here. In order to pass the time waiting for the half-time commercials and wardrobe malfunctions, the local hackers have organized a betting pool on the game. Members place their bets on the sum of the two final scores, or on the absolute difference between the two scores.

Given the winning numbers for each type of bet, can you deduce the final scores?
Input
The first line of input contains n, the number of test cases. n lines follow, each representing a test case. Each test case gives s and d, non-negative integers representing the sum and (absolute) difference between the two final scores.
Output
For each test case, output a line giving the two final scores, largest first. If there are no such scores, output a line containing “impossible”. Recall that football scores are always non-negative integers.
Sample Input
2
40 20
20 40
Sample Output
30 10
impossible

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main() {
	int N,a,b,x1,x2;
	scanf("%d",&N);
	while (N--) {
		scanf("%d %d",&a,&b);
		if(a<b)
			printf("impossible\n");
		else if ((a+b)%2==0&&(a-b)%2==0) {
			x1=(a+b)/2;
			x2=(a-b)/2;
			printf("%d %d\n",x1,x2);
		} else
			printf("impossible\n");
	}
	return 0;
}
/*测试样例1
2
40 20
20 40
*/

每学期的期末,大家都会忙于计算自己的平均成绩,这个成绩对于评奖学金是直接有关的。国外大学都是计算GPA(grade point average) 又称GPR(grade point ratio),即成绩点数与学分的加权平均值来代表一个学生的成绩的。那么如何来计算GPA呢?

一般大学采用之计分法

A90 - 100 4 点
B80 - 89 3 点
C70 - 79 2 点
D60 - 69 1 点
E0 - 59 0 点

例如:某位学生修习三门课,其课目、学分及成绩分别为:
英文:三学分、92 分;化学:五学分、80 分;数学:二学分、60分,则GPA的算法如下:

科目 学分 分数 点数 分数×点数
英文 3 92 4 12
化学 5 80 3 15
数学 2 60 1 2
合计 10 29
29/10=2.9
2.9即为某生的GPA
下面有请你写一个用于计算GPA的程序。
Input
包含多组数据,每组数据的第一行有一个数N,接下来N行每行表示一门成绩。每行有两个实型的数 s,p,s表示这门课的学分,p表示该学生的成绩(百分制)。如果p=-1则说明该学生这门课缺考,是不应该计算在内的。
Output
对每组数据输出一行,表示该学生的GPA,保留两位小数。如果GPA不存在,输出-1。
Sample Input
3
3 92
5 80
2 60
Sample Output
2.90
简单的判断求和输出

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
double grade[10000],s[10000];
int main() {
	int n,i;
	while(~scanf("%d",&n)) {
		double sum=0,sum1=0;
		for(i=0; i<n; i++) {
			scanf("%lf%lf",&s[i],&grade[i]);
			if(90 <= grade[i] && grade[i] <= 100) {
				sum += s[i] * 4;
			} else if(80 <= grade[i]) {
				sum += s[i] * 3;
			} else if(70 <= grade[i]) {
				sum += s[i] * 2;
			} else if(60 <= grade[i]) {
				sum += s[i] * 1;
			} else if(0 <= grade[i]) {
				sum += s[i] * 0;
			} else if(grade[i] == -1) {
				continue;
			}
			sum1 += s[i];
		}
		if(sum == 0) {
			printf("-1\n");
		} else
			printf("%.2lf\n",sum / sum1);
	}
	return 0;
}
/*测试样例1
3
3 92
5 80
2 60
*/

Ignatius is doing his homework now. The teacher gives him some articles and asks him to tell how many times each letter appears.

It’s really easy, isn’t it? So come on and AC ME.
Input
Each article consists of just one line, and all the letters are in lowercase. You just have to count the number of each letter, so do not pay attention to other characters. The length of article is at most 100000. Process to the end of file.

Note: the problem has multi-cases, and you may use “while(gets(buf)){…}” to process to the end of file.
Output
For each article, you have to tell how many times each letter appears. The output format is like “X:N”.

Output a blank line after each test case. More details in sample output.
Sample Input
hello, this is my first acm contest!
work hard for hdu acm.
Sample Output
a:1
b:0
c:2
d:0
e:2
f:1
g:0
h:2
i:3
j:0
k:0
l:2
m:2
n:1
o:2
p:0
q:0
r:1
s:4
t:4
u:0
v:0
w:0
x:0
y:1
z:0

a:2
b:0
c:1
d:2
e:0
f:1
g:0
h:2
i:0
j:0
k:1
l:0
m:1
n:0
o:2
p:0
q:0
r:3
s:0
t:0
u:1
v:0
w:1
x:0
y:0
z:0

还是基数排序思想
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
char c[100005];
int main() {
	while(gets(c)) {
		int b[27]= {0};
		int k=strlen(c);
		for(int i=0; i<k; i++)
			if(c[i]>='a'&&c[i]<='z')
				b[c[i]-'a']++;
		for(int i=0; i<26; i++)
			printf("%c:%d\n",i+'a',b[i]);
		puts("");
	}
	return 0;
}
/*测试样例1
hello, this is my first acm contest!
work hard for hdu acm.
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值