水题合集~任务要求
最小公倍数
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 234 Accepted Submission(s) : 58
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
给定两个正整数,计算这两个数的最小公倍数。
Input
输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.
Output
对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。
Sample Input
10 14
Sample Output
70
_____________________________________________________________
思路:没什么好说的,gcd求出最大公约数,然后两数相乘除以最大公约数即可。
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int gcd(int x,int y) { return y?gcd(y,x%y):x; } int main() { int a,b,c; while(scanf("%d%d",&a,&b)!=EOF) { c=a*b/gcd(a,b); cout<<c<<endl; } return 0; }
人见人爱A^B
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 110 Accepted Submission(s) : 59
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
求A^B的最后三位数表示的整数。
说明:A^B的含义是“A的B次方”
说明:A^B的含义是“A的B次方”
Input
输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。
Output
对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。
Sample Input
2 3 12 6 6789 10000 0 0
Sample Output
8 984 1
思路:求的是后面的三个整数,那么我们只要求后面三位数不断相乘的结果就可以了。
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int main() { int a,b,c; while(scanf("%d%d",&a,&b)!=EOF && a!=0 && b!=0) { a%=1000; c=a; for(int i=1;i<b;i++){ a*=c; a%=1000; } cout<<a<<endl; } return 0; }
Rightmost Digit
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 249 Accepted Submission(s) : 55
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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).
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.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
思路:超大次方求余,很明显的快速幂。
#include<algorithm> #include<iostream> #include<cstdio> #include<cmath> #include<set> using namespace std; int main() { long long T, n, temp, ans, a, b; cin >> T; while (T--) { cin >> n; a = b = n; ans = 1; while (b) { if (b & 1) { ans = (ans*a) % 10; b--; } b /= 2; a = a * a % 10; } cout << ans << endl; } return 0; }
Climbing Worm
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 88 Accepted Submission(s) : 55
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u inches every minute, but then has to rest a minute before climbing again. During the rest, it slips down d inches. The process of climbing and resting then repeats. How long before the worm climbs out of the well? We'll always count a portion of a minute as a whole minute and if the worm just reaches the top of the well at the end of its climbing, we'll assume the worm makes it out.
Input
There will be multiple problem instances. Each line will contain 3 positive integers n, u and d. These give the values mentioned in the paragraph above. Furthermore, you may assume d < u and n < 100. A value of n = 0 indicates end of output.
Output
Each input instance should generate a single integer on a line, indicating the number of minutes it takes for the worm to climb out of the well.
Sample Input
10 2 1 20 3 1 0 0 0
Sample Output
17 19
思路:模拟题,没什么说的,按题目流程写就好了,注意下有可能第一秒马上就爬出去的情况就行了。
#include<stdio.h> int main() { int n,u,d,t; while(scanf("%d%d%d",&n,&u,&d)!=EOF && n){ t=1; n-=u; if(n<=0) printf("%d\n",t); else{ do{ n=n+d-u; t+=2; }while(n>0); printf("%d\n",t); } } }
Balloon Comes!
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 149 Accepted Submission(s) : 47
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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!
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
思路:根据符号判断加减乘除,几个if搞定,注意下除法的类型和输出格式就行了。
#include<algorithm> #include<iostream> #include<cstdio> #include<cmath> #include<set> using namespace std; int main() { int T; char c; int a, b; cin >> T; while (T--) { cin >> c >> a >> b; if (c == '+') { cout << a + b << endl; } else if (c == '-') { cout << a - b << endl; } else if (c == '*') { cout << a * b << endl; } else if (c == '/') { if (a%b) { printf("%.2lf\n", (double)a / (double)b); } else { cout << a / b << endl; } } } return 0;
Fibonacci Again
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 95 Accepted Submission(s) : 44
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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.
Print the word "no" if not.
Sample Input
0 1 2 3 4 5
Sample Output
no no yes no no no
思路:问能不能被3整除,规律题,打表就能找到。
#include<iostream> int main() { int n; while (scanf("%d", &n) != EOF) { if (n % 8 == 2 || n % 8 == 6) printf("yes\n"); else printf("no\n"); } return 0; }
Number Sequence
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 117 Accepted Submission(s) : 41
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output
For each test case, print the value of f(n) on a single line.
Sample Input
1 1 3 1 2 10 0 0 0
Sample Output
2 5
思路:还是规律题,因为mod 7 而且由两个子数相乘,所以是49一个循环。
#include<algorithm> #include<iostream> #include<cstdio> #include<cmath> #include<set> using namespace std; #define mod 7 int main() { int A,B,n; int ans[50]; int f_n_1,f_n_2,z; while(scanf("%d%d%d",&A,&B,&n)&& A && B && n){ if(n==1 || n==2){ cout<<1<<endl; } else{ ans[1]=ans[2]=1; for(int i=3;i<50;i++){ ans[i]=(A*ans[i-1]%mod+B*ans[i-2]%mod)%mod; } cout<<ans[n%49]<<endl; } } return 0; }
sort
Time Limit : 6000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 205 Accepted Submission(s) : 28
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
给你n个整数,请按从大到小的顺序输出其中前m大的数。
Input
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。
Output
对每组测试数据按从大到小的顺序输出前m大的数。
Sample Input
5 3 3 -35 92 213 -644
Sample Output
213 92 3
思路:语言题,注意下数据大小,应该用scanf和printf
#include<algorithm> #include<iostream> #include<cstdio> #include<set> using namespace std; int n, m; int num[1000005]; int cmp(int &a, int &b) { return a > b; } int main() { while (scanf("%d%d", &n, &m) != EOF) { for (int i = 1; i <= n; i++) { scanf("%d", &num[i]); } sort(num + 1, num + n + 1, cmp); cout << num[1]; for (int i = 2; i < m; i++) { cout << ' ' << num[i]; } if (m > 1) { cout << ' ' << num[m]; } cout << endl; } return 0; }
吃糖果
Time Limit : 6000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 165 Accepted Submission(s) : 35
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一种,这样;可是Gardon不知道是否存在一种吃糖果的顺序使得他能把所有糖果都吃完?请你写个程序帮忙计算一下。
Input
第一行有一个整数T,接下来T组数据,每组数据占2行,第一行是一个整数N(0<N<=1000000),第二行是N个数,表示N种糖果的数目Mi(0<Mi<=1000000)。
Output
对于每组数据,输出一行,包含一个"Yes"或者"No"。
Sample Input
2 3 4 1 1 5 5 4 3 2 1
Sample Output
No Yes
思路:鸽巢原理。简单来说就是 除了最多的那种糖果的其他糖果的总和>=最多的那种糖果-1 就满足条件
#include<algorithm> #include<iostream> #include<cstdio> #include<set> using namespace std; int T,n; long long Max,sum; int num[1000005]; int main() { cin>>T; while(T--){ cin>>n; Max=sum=0; for(int i=1;i<=n;i++){ scanf("%d",&num[i]); sum+=num[i]; if(Max<num[i]){ Max=num[i]; } } sum-=Max; Max--; if(sum>=Max){ cout<<"Yes"<<endl; } else{ cout<<"No"<<endl; } } return 0; }
Sum Problem
Time Limit : 1000/500ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 97 Accepted Submission(s) : 42
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).
In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.
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<iostream> #include<cstdio> #include<algorithm> using namespace std; int main() { int T; int sum; while(scanf("%d",&T)!=EOF) { sum=0; for(int i=1;i<=T;i++){ sum+=i; } cout<<sum<<endl<<endl; } return 0; }
Elevator
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 74 Accepted Submission(s) : 38
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
Input
There are multiple test cases. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100. A test case with N = 0 denotes the end of input. This test case is not to be processed.
Output
Print the total time on a single line for each test case.
Sample Input
1 2 3 2 3 1 0
Sample Output
17 41
思路:模拟题,按流程来就好了,没什么坑点。
#include<iostream> using namespace std; int main() { int t; int ans; int now, bef; while (scanf("%d",&t)!=EOF && t) { bef = ans = 0; while (t--) { scanf("%d", &now); if (now > bef) { ans += (now - bef) * 6 + 5; bef = now; } else if (now == bef) { ans += 5; bef = now; } else { ans+= (bef - now) * 4 + 5; bef = now; } } cout << ans << endl; } return 0; }
七夕节
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 108 Accepted Submission(s) : 32
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!"
人们纷纷来到告示前,都想知道谁才是自己的另一半.告示如下:
<center> </center>
数字N的因子就是所有比N小又能被N整除的所有正整数,如12的因子有1,2,3,4,6.
你想知道你的另一半吗?
人们纷纷来到告示前,都想知道谁才是自己的另一半.告示如下:
<center> </center>
数字N的因子就是所有比N小又能被N整除的所有正整数,如12的因子有1,2,3,4,6.
你想知道你的另一半吗?
Input
输入数据的第一行是一个数字T(1<=T<=500000),它表明测试数据的组数.然后是T组测试数据,每组测试数据只有一个数字N(1<=N<=500000).
Output
对于每组测试数据,请输出一个代表输入数据N的另一半的编号.
Sample Input
3 2 10 20
Sample Output
1 8 22
思路:建立一个数组,默认全为1,从2开始到把它的所有倍数都加上自身,即可得出答案。
#include<algorithm> #include<iostream> #include<cstdio> #include<cmath> #include<set> using namespace std; #define Max 500000 int T,n; int ans[Max+5]; int main() { for(int i=1;i<=Max;i++){ for(int j=i<<1;j<=Max;j+=i){ ans[j]+=i; } } cin>>T; while(T--){ cin>>n; cout<<ans[n]<<endl; } return 0; }