寒假尾声组队做了点其他学校的寒假作业题,水题居多。
发下我做的一些题的题解,会把有价值的题目放在前面
先发个公共头文件
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cmath>
#include <vector>
#include <deque>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define clr(x) memset(x,0,sizeof(x))
#define pn printf("\n")
#define ll long long
#define pr(x) cout<<#x<<"="<<x<<"$";
然后是题解
Description
The traveller needs to go from the cell with number M to the cell with number N. The traveller is able to enter the cell through cell edges only, he can not travel from cell to cell through vertices. The number of edges the traveller passes makes the length of the traveller's route.
Write the program to determine the length of the shortest route connecting cells with numbers N and M.
Input
Output
Sample Input
6 12
Sample Output
3
本题我的做法比较有趣:对于两个格子,计算出它在第几行、将图顺时针旋转120度后它在倒数第几行,将图逆时针旋转120度后它在倒数第几行,三个参数分别为p,q,r,容易发现按照规则移动一步,这三个参数恰有一个参数有变化,且变化1,于是计算出两个格子的参数后分别做差取绝对值即可。
ll p1, q1, r1, p2, q2, r2;
int main()
{
ll m, n;
while(scanf("%lld%lld", &m, &n) == 2){
ll x1 = ceil(sqrt(m));
ll x2 = ceil(sqrt(n));
ll Y1 = m - (x1-1)*(x1-1), Y2 = n - (x2-1)*(x2-1);
p1 = x1; q1 = (Y1+1)/2; r1 = (2*x1-Y1+1)/2;
p2 = x2; q2 = (Y2+1)/2; r2 = (2*x2-Y2+1)/2;
//printf("%lld %lld %lld\n%lld %lld %lld\n", p1, q1, r1, p2, q2, r2);
printf("%lld\n", abs(p1-p2)+abs(q1-q2)+abs(r1-r2));
}
return 0;
}
Description
Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs
one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively.
By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.
Input
Output
Sample Input
1 ABCD EFGH even ABCI EFJK up ABIJ EFGH even
Sample Output
K is the counterfeit coin and it is light.
思路就是如果平则秤上的球都是清白的,如果不平则不在秤上的球都是清白的,给重的那一方和轻的那一方没有清白标记的打上标记
最后还有标记的输出即可
结果WA了
我漏了一个关键问题,如果一个球被打上可能重的标记之后又在秤的偏轻一端出现,则这个球是清白的
加上这一点就能A了
int vis[20]; //-1 :unknown, 0 : clean 1 : light 2 : heavy
char s1[20], s2[20], s3[20];
int main()
{
int tt; cin>>tt;
rep(kk, tt){
memset(vis, -1, sizeof(vis));
rep(j, 3){
scanf("%s%s%s", s1, s2, s3);
if (s3[0] == 'e'){
rep(i, strlen(s1)) vis[s1[i]-'A'] = 0;
rep(i, strlen(s2)) vis[s2[i]-'A'] = 0;
}else{
set<int> mset;
rep(i, strlen(s1)) mset.insert(s1[i] - 'A');
rep(i, strlen(s2)) mset.insert(s2[i] - 'A');
rep(i, 12) if (mset.count(i) == 0) vis[i] = 0;
rep(i, strlen(s1)) {
int val = s3[0] == 'u' ? 2 : 1;;
if (vis[s1[i]-'A'] == -1)
vis[s1[i]-'A'] = val;
if (vis[s1[i]-'A'] != 0 && vis[s1[i]-'A'] != val) vis[s1[i]-'A'] = 0; //
}
rep(i, strlen(s2)) {
int val = s3[0] == 'u' ? 1 : 2;
if (vis[s2[i]-'A'] == -1)
vis[s2[i]-'A'] = val;
if (vis[s2[i]-'A'] != 0 && vis[s2[i]-'A'] != val) vis[s2[i]-'A'] = 0; //
}
}
//rep(i, 12) printf("%d %d\n", i, vis[i]);
}
int ans = -1, dir = -1;
rep(i, 12) if (vis[i] != 0) {
ans = i; dir = vis[i];
}
printf("%c is the counterfeit coin and it is %s. \n", ans + 'A', dir == 1 ? "light" : "heavy");
}
return 0;
}
D - 不容易系列之(3)―― LELE的RPG难题
Description
有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法.
以上就是著名的RPG难题.
如果你是Cole,我想你一定会想尽办法帮助LELE解决这个问题的;如果不是,看在众多漂亮的痛不欲生的Cole女的面子上,你也不会袖手旁观吧?
Input
Output
Sample Input
1 2
Sample Output
3 6
这题还是不错的,我找规律推完公式才找到关键。
如果不要求最后一位和第一位不同的话,答案就是3*2^(n-1),现在要求的话,我们只需扣除第n位和第1位相同的情况,这个情况有多少种?
其实很简单,就是n-1位下的标准答案,通过这个完成递推。
代码就不贴了。
Description
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy.
Input
Output
Sample Input
3 4 0
Sample Output
5 30
这题还是不错的。约瑟夫问题一般是用模拟解决的,但这题用到了一个递推公式来计算被踢掉的人的序号,
设f(i)为第i+1轮被踢掉的人的序号(从0起算),则f(i) = (f(i-1) + m - 1) % (n - i)
f(0) = m - 1;
以n = 7, m = 5为例
f(0) = 4; f(1) = 2; f(2) = 1; f(3) = 1; f(4) = 2; f(5) = 0; f(6) = 0;
这个很明显不对啊
事实上,f(i)代表的是去掉之前被踢掉的人重新编号之后的序号
比如前三轮过后,情况是 1 4 6 7, 2刚被踢掉,下一个被踢的应该是4
而f(3) = 1, 1 4 6 7,重编号即0 1 2 3,1代表4
4被踢后,剩下1 6 7,下一个是7,f(4) = 2,也符合
个人认为这不是一个很有用的公式
但在这道题里非常有用,直接枚举m,按照这个公式进行递推,前k轮 值一旦小于k就代表这个值不对
按照这个思路进行打表即可.
Description
Input
Output
Sample Input
2 1 124866 3 124866 111111 987651
Sample Output
1 8
这题我是用暴力过的,即从小到大枚举答案然后测试,测试的时候使用bitset就能过,用普通数组记录就可能T
const int maxn = 310;
int a[maxn];
bitset<1000010> vis;
int main()
{
int kk; cin>>kk;
rep(tt, kk){
int n; cin>>n;
rep(i, n) scanf("%d", &a[i]);
int ans = n;
while(1){
int flag = 1;
vis.reset();
rep(i, n){
int x = a[i] % ans;
if (vis[x] == 1) {flag = 0; break;}
vis[x] = 1;
}
if (flag) break;
++ans;
}
printf("%d\n", ans);
}
return 0;
}
C - Edge
Description
After several such folding steps have been performed we may unfold the sheet again and take a look at its longer edge holding the sheet so that it appears as a one-dimensional curve, actually a concatenation of line segments. If we move along this curve in a fixed direction we can classify every place where the sheet was folded as either type A meaning a clockwise turn or type V meaning a counter-clockwise turn. Given such a sequence of classifications, produce a drawing of the longer edge of the sheet assuming 90 degree turns at equidistant places.
Input
Output
You may display such drawings with the gv PostScript interpreter, optionally after a conversion using the ps2ps utility.
Sample Input
V AVV
Sample Output
300 420 moveto 310 420 lineto 310 430 lineto stroke showpage 300 420 moveto 310 420 lineto 310 410 lineto 320 410 lineto 320 420 lineto stroke showpage
这题看上去各种高大上有没有?什么gv PostScript之类的,事实上每组数据的第一行和最后两行都是套话,中间的输入长度+1行才有价值,这些行表示一些连笔的线段,线段的方向由输入控制.
int dx[] = {10,0,-10,0};
int dy[] = {0,-10,0,10};
char s[300];
int main()
{
while(scanf("%s", s) == 1){
int now = 0, x = 300, y = 420;
printf("300 420 moveto\n");
int len = strlen(s);
x += dx[now]; y += dy[now];
printf("%d %d lineto\n", x, y);
rep(i, len){
if (s[i] == 'A') now = (now + 1) % 4;
else now = now == 0 ? 3 : now - 1;
x += dx[now]; y += dy[now];
printf("%d %d lineto\n", x, y);
}
printf("stroke\nshowpage\n");
}
return 0;
}
需要注意的是:A和B的每一位数字由对应的英文单词给出.
Input
Output
Sample Input
one + two = three four + five six = zero seven + eight nine = zero + zero =
Sample Output
3 90 96
map<string, int> mmap;
int num[10];
int main()
{
mmap["zero"] = 0;
mmap["one"] = 1;
mmap["two"] = 2;
mmap["three"] = 3;
mmap["four"] = 4;
mmap["five"] = 5;
mmap["six"] = 6;
mmap["seven"] = 7;
mmap["eight"] = 8;
mmap["nine"] = 9;
char s[100];
int now = 0;
while(scanf("%s", s) == 1){
if (s[0] == '+') now ^= 1;
else if (s[0] == '='){
int ans = num[0] + num[1];
//printf("%d %d\n", num[0], num[1]);
if (ans)printf("%d\n", ans); else break;
num[0] = num[1] = 0;
now ^= 1;
}else{
rep(i, strlen(s)) if (s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
int tmp = mmap[s];
num[now] = num[now] * 10 + tmp;
}
}
return 0;
}
Description
"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
Output
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
本想搞个map什么的,其实求个中位数就行了
Description
"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?
Input
Output
Sample Input
6 4 11 8
Sample Output
1 2 3 5 6 4 1 2 3 4 5 6 7 9 8 11 10
善用C++的话是个水题
Description
For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
Input
Output
Sample Input
24 39 0
Sample Output
6 3
这题就是求输入的数加起来模9,如果是0的话改成9就行了。
int getint(){
int ans = 0; char c;
while( (c = getchar()) != '\n'){
if (c <= '9' && c >= '0') ans = (ans + c - '0' - 1) % 9 + 1;
}
return ans;
}
int main()
{
int sum;
while( (sum = getint()) != 0){
printf("%d\n", sum);
}
return 0;
}
Description
Ring radii are at 3", 6", 9", 12" and 15" (the Bullseye has a diameter of 6"). A game of Simple Darts between two players is played as follows. The first player throws 3 darts at the board. A score is computed by adding up the point values of each region that a dart lands in. The darts are removed. The second player throws 3 darts at the board; the score for player two is computed the same way as it is for player one. The player with the higher score wins.
For this problem, you are to write a program that computes the scores for two players, and determine who, if anyone, wins the game. If a dart lands exactly on a ring (region boundary), the higher point value is awarded. Any dart outside the outer ring receives no points. For the purposes of this problem, you can assume that a dart has an infinitely fine point and can not land paritially on a ring; it is either on the ring or it is not on the ring. Standard double precision floating point operations will be should be used.
Input
Output
SCORE: N to M, PLAYER P WINS.
Or:
SCORE: N to M, TIE.
N is player one's score, and M is player two's score. P is either 1 or 2 depending on which player wins. All values are non-negative integers.
Formula
Recall: r 2 = x 2 + y 2 where r is the radius, and (x, y) are the coordinates of a point on the circle.
Sample Input
-9 0 0 -4.5 -2 2 9 0 0 4.5 2 -2 -19.0 19.0 0 0 0 0 3 3 6 6 12 12 -100 0 0 0 0 0 0 0 0 0 0 0
Sample Output
SCORE: 240 to 240, TIE. SCORE: 200 to 140, PLAYER 1 WINS.
Description
If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theorem on Arithmetic Progressions, which had been conjectured by Johann Carl Friedrich Gauss (1777 - 1855) and was proved by Johann Peter Gustav Lejeune Dirichlet (1805 - 1859) in 1837.
For example, the arithmetic sequence beginning with 2 and increasing by 3, i.e.,
2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, ... ,
contains infinitely many prime numbers
2, 5, 11, 17, 23, 29, 41, 47, 53, 59, 71, 83, 89, ... .
Your mission, should you decide to accept it, is to write a program to find the nth prime number in this arithmetic sequence for given positive integers a, d, and n.
Input
The input is a sequence of datasets. A dataset is a line containing three positive integers a, d, and n separated by a space. a and d are relatively prime. You may assume a <= 9307, d <= 346, and n <= 210.
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
Output
The output should be composed of as many lines as the number of the input datasets. Each line should contain a single integer and should never contain extra characters.
The output integer corresponding to a dataset a, d, n should be the nth prime number among those contained in the arithmetic sequence beginning with a and increasing by d.
FYI, it is known that the result is always less than 106 (one million) under this input condition.
Sample Input
367 186 151 179 10 203 271 37 39 103 230 1 27 104 185 253 50 85 1 1 1 9075 337 210 307 24 79 331 221 177 259 170 40 269 58 102 0 0 0
Sample Output
92809 6709 12037 103 93523 14503 2 899429 5107 412717 22699 25673
Description
Bessie is training for her next race by running on a path that includes hills so that she will be prepared for any terrain. She has planned a straight path and wants to run as far as she can -- but she must be back to the farm within M seconds (1 ≤ M ≤ 10,000,000).
The entire path she has chosen is T units (1 ≤ T ≤ 100,000) in length and consists of equal-length portions that are uphill, flat, or downhill. The input data describes path segment i with a single character Si that is u, f, or d, indicating respectively uphill, flat, or downhill.
Bessie takes U seconds (1 ≤ U ≤ 100) to run one unit of uphill path, F (1 ≤ F ≤ 100) seconds for a unit of flat path, and D (1 ≤ D ≤ 100) seconds for a unit of downhill path. Note that, when returning home, uphill paths become downhill paths and downhill paths become uphill paths.
Find the farthest distance Bessie can get from the farm and still make it back in time.
Input
* Line 1: Five space-separated integers: M, T,U, F, and D
* Lines 2..T+1: Line i+1 describes path segment i with a single letter: Si
Output
* Line 1: A single integer that is the farthest distance (number of units) that Bessie can get from the farm and make it back in time.
Sample Input
13 5 3 2 1 u f u d f
Sample Output
3
Description
For example, the number 2991 has the sum of (decimal) digits 2+9+9+1 = 21. Since 2991 = 1*1728 + 8*144 + 9*12 + 3, its duodecimal representation is 1893 12, and these digits also sum up to 21. But in hexadecimal 2991 is BAF 16, and 11+10+15 = 36, so 2991 should be rejected by your program.
The next number (2992), however, has digits that sum to 22 in all three representations (including BB0 16), so 2992 should be on the listed output. (We don't want decimal numbers with fewer than four digits -- excluding leading zeroes -- so that 2992 is the first correct answer.)
Input
Output
Sample Input
There is no input for this problem
Sample Output
2992 2993 2994 2995 2996 2997 2998 2999 ...
转换进制即可