寒假算法摸鱼day1 多刷水题有益身心健康

版权声明:本文为博主原创文章,如需转载请联系博主。 https://blog.csdn.net/GregoryHanson/article/details/86618491

知识点整理

处理多组输入:

while(scanf("%d",&n)!=EOF){
    //code here
}

输入至0输入结束:

while(cin >> n, n != 0) {
    //code here      
}

T组数据:

while(T--){
    //code here
}

题目

A题

HDU - 2000 ASCII码排序

输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
Sample Input
qwe
asd
zxc
Sample Output
e q w
a d s
c x z

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<stdlib.h>
#include<memory.h>
using namespace std;

int main() {
	char a[4];
	while (scanf("%s",a)!=EOF) {
		sort(a, a + 3);
		cout << a[0] << " " << a[1] << " " << a[2] << endl;
	}
	return 0;
}

B题

HDU - 2005 第几天?

给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20
2006/3/12
Sample Output
20
71

水题,值得吐槽的是说好了给规范日期格式的,结果1月还是写的1而不是01。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<stdlib.h>
#include<memory.h>
using namespace std;

int daysPerMonth[2][12] = { { 31,28,31,30,31,30,31,31,30,31,30,31 },{31,29,31,30,31,30,31,31,30,31,30,31 } };

int main() {
	char str[20];
	while (scanf("%s", str) != EOF) {
		int data[3] = { 0,0,0 };
		int pos = 0;
		for (int i = 0; i < strlen(str); i++) {
			if (str[i] != '/') {
				data[pos] = data[pos] * 10 + (str[i] - '0');
			}
			else {
				pos++;
			}
		}
		if ((data[0] % 4 == 0 && data[0] % 100 != 0) || data[0] % 400 == 0) {
			pos = 1;
		}
		else {
			pos = 0;
		}
		int day = data[2];
		for (int i = 0; i < data[1] - 1; i++) {
			day += daysPerMonth[pos][i];
		}
		cout << day << endl;

	}
	return 0;
}

C题

HDU - 2029 Palindromes _easy version

“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。
Input
输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。
Output
如果一个字符串是回文串,则输出"yes",否则输出"no".
Sample Input
4
level
abcde
noon
haha
Sample Output
yes
no
yes
no

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<memory.h>
using namespace std;

int main() {
	string str;
	int T;
	cin >> T;
	while (T--) {
		int flag = 1;
		cin >> str;
		int begin = 0;
		int end = str.length() - 1;
		for (; begin < end; begin++, end--) {
			if (str[begin] != str[end]) {
				flag = 0;
			}
		}
		flag == 1 ? cout << "yes" << endl : cout << "no" << endl;
	}
	return 0;
}

D题

HDU - 2031 进制转换

输入一个十进制数N,将它转换成R进制数输出。
Input
输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。
Output
为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。
Sample Input
7 2
23 12
-4 3
Sample Output
111
1B
-11

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<memory.h>
using namespace std;

char str[20] = "0123456789ABCDEF";

int main() {
	int N;
	int R;
	while (scanf("%d %d", &N, &R) != EOF) {
		char ans[100];
		int i = 0;

		int absN = abs(N);
		while (absN / R != 0) {
			ans[i++] = str[absN%R];
			absN /= R;
		}
		ans[i] = str[absN];
		if (N < 0) {
			cout << "-";
		}
		for (int j = i; j >= 0; j--) {
			cout << ans[j];
		}
		cout << endl;
	}
	return 0;
}

E题

HDU - 1021 Fibonacci Again

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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<memory.h>
using namespace std;

int main() {
	int N;
	while (scanf("%d", &N) != EOF) {
		int a = 1, b = 2, temp = 0;
		for (int i = 0; i < N - 1; i++) {
			temp = (a + b) % 3;
			a = b;
			b = temp;
		}
		if (b % 3 == 0) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}
	return 0;
}

F题

HDU - 1033 Edge

For products that are wrapped in small packings it is necessary that the sheet of paper containing the directions for use is folded until its size becomes small enough. We assume that a sheet of paper is rectangular and only folded along lines parallel to its initially shorter edge. The act of folding along such a line, however, can be performed in two directions: either the surface on the top of the sheet is brought together, or the surface on its bottom. In both cases the two parts of the rectangle that are separated by the folding line are laid together neatly and we ignore any differences in thickness of the resulting folded sheet.
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
The input contains several test cases, each on a separate line. Each line contains a nonempty string of characters A and V describing the longer edge of the sheet. You may assume that the length of the string is less than 200. The input file terminates immediately after the last test case.
Output
For each test case generate a PostScript drawing of the edge with commands placed on separate lines. Start every drawing at the coordinates (300, 420) with the command “300 420 moveto”. The first turn occurs at (310, 420) using the command “310 420 lineto”. Continue with clockwise or counter-clockwise turns according to the input string, using a sequence of “x y lineto” commands with the appropriate coordinates. The turning points are separated at a distance of 10 units. Do not forget the end point of the edge and finish each test case by the commands stroke and showpage.
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

英文题,题面很啰嗦,意思就是从一个点出发,输入A就顺时针拐弯,V就逆时针拐弯,然后结合这个AV的输入把这个画图过程用英文句子表示出来。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<memory.h>
using namespace std;

int main() {
	char str[205];
	while (scanf("%s", str) != EOF) {
	int x = 300, y = 420;
	cout << x << " " << y << " moveto" << endl;
	x += 10;
	int state = 1;
	cout << x << " " << y << " lineto" << endl;
	for (int i = 0; i < strlen(str); ++i) {
		if (state == 1) {
			if (str[i] == 'V') { y += 10, state = 3; }
			else { y -= 10, state = 4; }
		}
		else if (state == 2) {
			if (str[i] == 'V') { y -= 10, state = 4; }
			else { y += 10, state = 3; }
		}
		else if (state == 3) {
			if (str[i] == 'V') { x -= 10, state = 2; }
			else { x += 10, state = 1; }
		}
		else if (state == 4) {
			if (str[i] == 'V') { x += 10, state = 1; }
			else { x -= 10, state = 2; }
		}
		cout << x << " " << y << " lineto" << endl;
	}
	cout << "stroke" << endl << "showpage" << endl;
	}
	return 0;
}

G题

CodeForces - 262A

Roma (a popular Russian name that means ‘Roman’) loves the Little Lvov Elephant’s lucky numbers.

Let us remind you that lucky numbers are positive integers whose decimal representation only contains lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Roma’s got n positive integers. He wonders, how many of those integers have not more than k lucky digits? Help him, write the program that solves the problem.
Input
The first line contains two integers n, k (1 ≤ n, k ≤ 100). The second line contains n integers ai (1 ≤ ai ≤ 109) — the numbers that Roma has.
The numbers in the lines are separated by single spaces.
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
3 4
1 2 4
Output
3
Input
3 2
447 44 77
Output
2
Note
In the first sample all numbers contain at most four lucky digits, so the answer is 3.
In the second sample number 447 doesn’t fit in, as it contains more than two lucky digits. All other numbers are fine, so the answer is 2.

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<memory.h>
using namespace std;

int main() {
	int n, k;
	cin >> n>>k;
	int count = 0;
	while (n--) {
		int a;
		cin >> a;
		int tempCount = 0;
		while (a != 0) {
			if (a % 10 == 4 || a % 10 == 7) {
				tempCount++;
			}
			a /= 10;
		}
		if (tempCount <= k) {
			count++;
		}
	}
	cout << count << endl;
	return 0;
}

H题

CodeForces - 255B Code Parsing

Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly’s algorithm works with string s, consisting of characters “x” and “y”, and uses two following operations at runtime:
1.Find two consecutive characters in the string, such that the first of them equals “y”, and the second one equals “x” and swap them. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string.
2.Find in the string two consecutive characters, such that the first of them equals “x” and the second one equals “y”. Remove these characters from the string. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string.
The input for the new algorithm is string s, and the algorithm works as follows:
1.If you can apply at least one of the described operations to the string, go to step 2 of the algorithm. Otherwise, stop executing the algorithm and print the current string.
2.If you can apply operation 1, then apply it. Otherwise, apply operation 2. After you apply the operation, go to step 1 of the algorithm.
Now Vitaly wonders, what is going to be printed as the result of the algorithm’s work, if the input receives string s.
Input
The first line contains a non-empty string s.
It is guaranteed that the string only consists of characters “x” and “y”. It is guaranteed that the string consists of at most 106 characters. It is guaranteed that as the result of the algorithm’s execution won’t be an empty string.
Output
In the only line print the string that is printed as the result of the algorithm’s work, if the input of the algorithm input receives string s.
Examples
Input
x
Output
x
Input
yxyxy
Output
y
Input
xxxxxy
Output
xxxx
Note
In the first test the algorithm will end after the first step of the algorithm, as it is impossible to apply any operation. Thus, the string won’t change.
In the second test the transformation will be like this:
1.string “yxyxy” transforms into string “xyyxy”;
2.string “xyyxy” transforms into string “xyxyy”;
3.string “xyxyy” transforms into string “xxyyy”;
4.string “xxyyy” transforms into string “xyy”;
5.string “xyy” transforms into string “y”.
As a result, we’ve got string “y”.
In the third test case only one transformation will take place: string “xxxxxy” transforms into string “xxxx”. Thus, the answer will be string “xxxx”.

这题还是有点坑的。模拟的话会超时,应该通过找规律解决问题。规律是:只要有一对xy存在,不管是什么顺序,都会消除。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<memory.h>
using namespace std;

char str[2000000];
//。。。。。。。。。。。。。。。。。。

int main() {
	cin >> str;
	int x = 0;
	int y = 0;
	for (int i = 0; i < strlen(str); i++) {
		str[i] == 'x' ? x++ : y++;
	}
	if (x > y) {
		for (int i = 0; i < x - y; i++) {
			cout << "x";
		}
	}
	else {
		for (int i = 0; i < y - x; i++) {
			cout << "y";
		}
	}
	cout << endl;
	return 0;
}

I题

CodeForces - 257B Playing Cubes

Petya and Vasya decided to play a little. They found n red cubes and m blue cubes. The game goes like that: the players take turns to choose a cube of some color (red or blue) and put it in a line from left to right (overall the line will have n + m cubes). Petya moves first. Petya’s task is to get as many pairs of neighbouring cubes of the same color as possible. Vasya’s task is to get as many pairs of neighbouring cubes of different colors as possible.
The number of Petya’s points in the game is the number of pairs of neighboring cubes of the same color in the line, the number of Vasya’s points in the game is the number of neighbouring cubes of the different color in the line. Your task is to calculate the score at the end of the game (Petya’s and Vasya’s points, correspondingly), if both boys are playing optimally well. To “play optimally well” first of all means to maximize the number of one’s points, and second — to minimize the number of the opponent’s points.
Input
The only line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the number of red and blue cubes, correspondingly.
Output
On a single line print two space-separated integers — the number of Petya’s and Vasya’s points correspondingly provided that both players play optimally well.
Examples
Input
3 1
Output
2 1
Input
2 4
Output
3 2
Note
In the first test sample the optimal strategy for Petya is to put the blue cube in the line. After that there will be only red cubes left, so by the end of the game the line of cubes from left to right will look as [blue, red, red, red]. So, Petya gets 2 points and Vasya gets 1 point.
If Petya would choose the red cube during his first move, then, provided that both boys play optimally well, Petya would get 1 point and Vasya would get 2 points.

找规律题。别想太多。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<memory.h>
using namespace std;

int main() {
	int m, n;
	cin >> m >> n;
	cout << max(m, n) - 1 << " " << min(m, n) << endl;
	return 0;
}

J题

CodeForces - 259A Little Elephant and Chess

The Little Elephant loves chess very much.

One day the Little Elephant and his friend decided to play chess.They’ve got the chess pieces but the board is a problem. They’ve got an 8 × 8 checkered board, each square is painted either black or
white. The Little Elephant and his friend know that a proper chessboard doesn’t have any side-adjacent cells with the same color and the upper left cell is white. To play chess, they want to make the board they have a proper chessboard. For that the friends can choose any row of the board and cyclically shift the cells of the chosen row, that is, put the last (rightmost) square on the first place in the row and shift the others one position to the right. You can run the described operation multiple times (or not run it at all).
For example, if the first line of the board looks like that “BBBBBBWW” (the white cells of the line are marked with character “W”, the black cells are marked with character “B”), then after one cyclic shift it will look like that “WBBBBBBW”.

Help the Little Elephant and his friend to find out whether they can use any number of the described operations to turn the board they have into a proper chessboard.
Input
The input consists of exactly eight lines. Each line contains exactly eight characters “W” or “B” without any spaces: the j-th character in the i-th line stands for the color of the j-th cell of the i-th row of the elephants’ board. Character “W” stands for the white color, character “B” stands for the black color.

Consider the rows of the board numbered from 1 to 8 from top tobottom, and the columns — from 1 to 8 from left to right. The given board can initially be a proper chessboard.

Output
In a single line print “YES” (without the quotes), if we can make the board a proper chessboard and “NO” (without the quotes) otherwise.

Examples
Input
WBWBWBWB
BWBWBWBW
BWBWBWBW
BWBWBWBW
WBWBWBWB
WBWBWBWB
BWBWBWBW
WBWBWBWB
Output
YES
Input
WBWBWBWB
WBWBWBWB
BBWBWWWB
BWBWBWBW
BWBWBWBW
BWBWBWWW
BWBWBWBW
BWBWBWBW
Output
NO
Note
In the first sample,you should shift the following lines one position to the right: the
3-rd, the 6-th, the 7-th and the 8-th.

In the second sample there is no way you can achieve the goal.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<memory.h>
using namespace std;

int main() {
	string str[8];
	for (int i = 0; i < 8; i++) {
		cin >> str[i];
	}
	int flag = 1;
	for (int i = 0; i < 8; i++) {
		for (int j = 0; j < 7; j++) {
			if (str[i][j] == str[i][j + 1]) {
				flag = 0;
			}
		}
	}
	flag == 1 ? cout << "YES" << endl : cout << "NO" << endl;
	return 0;
}

K题

CodeForces - 259B Little Elephant and Magic Square

Little Elephant loves magic squares very much.
A magic square is a 3 × 3 table, each cell contains some positive integer. At that the sums of integers in all rows, columns and diagonals of the table are equal. The figure below shows the magic square, the sum of integers in all its rows, columns and diagonals equals 15.
The Little Elephant remembered one magic square. He started writing this square on a piece of paper, but as he wrote, he forgot all three elements of the main diagonal of the magic square. Fortunately, the Little Elephant clearly remembered that all elements of the magic square did not exceed 105.
Help the Little Elephant, restore the original magic square, given the Elephant’s notes.
Input
The first three lines of the input contain the Little Elephant’s notes. The first line contains elements of the first row of the magic square. The second line contains the elements of the second row, the third line is for the third row. The main diagonal elements that have been forgotten by the Elephant are represented by zeroes.
It is guaranteed that the notes contain exactly three zeroes and they are all located on the main diagonal. It is guaranteed that all positive numbers in the table do not exceed 105.
Output
Print three lines, in each line print three integers — the Little Elephant’s magic square. If there are multiple magic squares, you are allowed to print any of them. Note that all numbers you print must be positive and not exceed 105.
It is guaranteed that there exists at least one magic square that meets the conditions.
Examples
Input
0 1 1
1 0 1
1 1 0
Output
1 1 1
1 1 1
1 1 1
Input
0 3 6
5 0 5
4 7 0
Output
6 3 6
5 5 5
4 7 4

gzp告诉我这题大多数人(codeforce)做法其实是暴力。惊呆了,本来还以为是段子。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stdlib.h>
#include<memory.h>
using namespace std;

int main() {
	int a[9];
	for (int i = 0; i < 9; i++) {
		cin >> a[i];
	}
	a[0] = (2 * a[6] + a[7] - a[1]) / 2;
	a[4] = a[0] + a[1] + a[2] - a[3] - a[5];
	a[8] = a[0] + a[1] + a[2] - a[6] - a[7];
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout << a[i * 3 + j]<<" ";
		}
		cout << endl;
	}
	return 0;
}

吐槽:今天题量有点大,虽然都很简单。rank榜看起来绿了一片还行,但是参与度还是太低了,40多人就30个不到登录了,还有好几个人就写了两三题不到。希望他们可以过几天可以补过来吧(不存在的

lx关于多组输入的总结链接:https://blog.csdn.net/qq_40265493/article/details/86610711?tdsourcetag=s_pctim_aiomsg

猜你喜欢

转载自blog.csdn.net/GregoryHanson/article/details/86618491