# 2019年蓝桥杯每周一练题目练习

PC/UVa IDs: 110101/100
Popularity: A
Success rate: low Level: 1

https://vjudge.net/problem/UVA-100
[问题描述]

22，11，34，17，52，26，13，40，20，10，5，16，8，4，2，1

[输入]

[输出]

[样例输入]

1 10
100 200
201 210
900 1000


[样例输出]

1 10 20
100 200 125
201 210 89
900 1000 174


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int arr[10000000];
bool cmp(int a,int b){
return a > b;
}
void fun(int a,int b){
int k = 0,temp = 0,j = 0;
memset(arr,0,sizeof(arr));
for(int i = a;i <= b;i++){
k = i;
temp = 0;
while(k != 1){
if(k == 1){
break;
}
if(k % 2 == 0){
k = k / 2;
}else{
k = k * 3 + 1;
}
temp++;
}
arr[j++] = temp + 1;
}
sort(arr,arr + j,cmp);
}
int main(){
long long n,m,l = 0;
while(~scanf("%lld%lld",&n,&m)){
fun(n,m);
cout << n << " " << m << " " << arr[0] << endl;
}
return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int maxx;
void fun(int a,int b){
int k,temp;
maxx = 0;
for(int i = min(a, b);i <= max(a, b);i++){
k = i;
temp = 1;
while(k != 1){
if(k % 2 == 0){
k = k / 2;
}else{
k = k * 3 + 1;
}
temp++;
}
maxx = max(temp,maxx);
}
}
int main(){
long long n,m,l = 0;
while(~scanf("%lld%lld",&n,&m)){
fun(n,m);
cout << n << " " << m << " " << maxx << endl;
}
return 0;
}


Minesweeper （扫雷）

PC/UVa IDs: 110102/10189,

Popularity: A,

Success rate: high Level: 1

[问题描述]

Have you ever played Minesweeper? It’s a cute little game which comes within a certain Operating

System which name we can’t really remember. Well, the goal of the game is to find where are all the mines within a M × N field. To help you, the game shows a number in a square which tells you how many mines there are adjacent to that square. For instance, supose the following 4 × 4 field with 2 mines (which are represented by an ‘*’ character):

*…

.*…

If we would represent the same field placing the hint numbers described above, we would end up

with:

*100

2210

1*10

1110

As you may have already noticed, each square may have at most 8 adjacent squares.

[输入]

The input will consist of an arbitrary number of fields. The first line of each field contains two integers

n and m (0 < n, m ≤ 100) which stands for the number of lines and columns of the field respectively.

The next n lines contains exactly m characters and represent the field.

Each safe square is represented by an ‘.’ character (without the quotes) and each mine square

is represented by an ‘*’ character (also without the quotes). The first field line where n = m = 0

represents the end of input and should not be processed.

[输出]

[样例输入]

4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0


[样例输出]

Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char mapp[500][500];
int n,m;
int cross[10] = {0,0,-1,-1,-1,0,1,1,1};
int stra[10] = {0,1,1,0,-1,-1,-1,0,1};
int main(){
cin >> n >> m;
while(scanf("%d%d",&n,&m) != EOF && n != 0 && m != 0){
memset(mapp,0,sizeof(mapp));
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
cin >> mapp[i][j];
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
int count = 0;
if(mapp[i][j] == '*'){
continue;
}
if(mapp[i][j] != '*'){
for(int k = 0;k <= 8;k++){
int pre = i + cross[k];
int pry = j + stra[k];
if(mapp[pre][pry] == '*'){
count++;
}
}
}
mapp[i][j] = count + '0';
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
cout << mapp[i][j];
}
cout << endl;
}
}
return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char mapp[500][500];
int n,m;
int cross[10] = {0,0,-1,-1,-1,0,1,1,1};
int stra[10] = {0,1,1,0,-1,-1,-1,0,1};
int main(){
int l = 1;
cin >> n >> m;
while(scanf("%d%d",&n,&m) != EOF && n != 0 && m != 0){
memset(mapp,0,sizeof(mapp));
if(l != 1){
cout << endl;
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
cin >> mapp[i][j];
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
int count = 0;
if(mapp[i][j] == '*'){
continue;
}
if(mapp[i][j] != '*'){
for(int k = 0;k <= 8;k++){
int pre = i + cross[k];
int pry = j + stra[k];
if(mapp[pre][pry] == '*'){
count++;
}
}
}
mapp[i][j] = count + '0';
}
}
printf("Field #%d:\n",l++);
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
cout << mapp[i][j];
}
cout << endl;
}
}
return 0;
}


3.每周一题之3
[问题描述]
X星球居民小区的楼房全是一样的，并且按矩阵样式排列。其楼房的编号为1,2,3…

1 2 3 4 5 6
12 11 10 9 8 7
13 14 15 …

[样例输入]
6 8 2
[样例输出]
4
[样例输入]
4 7 20
[样例输出]
5

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int arr[1000][1000];
int main(){
int n,m,k,w = 1,temp;
cin >> k >> m >> n;//6 8 2
for(int i = 1;i <= 1000;i++){
if(i % 2 == 1){
int j;
for(j = 1;j <= k;j++){
///if(j % k != 0){
arr[i][j] = w;
w = w + 1;
}
//temp = w;
}else{
for(int p = k;p > 0;p--){
arr[i][p] = w;
w += 1;
}
}
}
int startx,starty,endx,endy;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= k;j++){
if(arr[i][j] == m){
startx = i;
starty = j;
}
if(arr[i][j] == n){
endx = i;
endy = j;
}
}
}
cout << abs((startx - endx) + (starty - endy));
return 0;
}

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main(){
int w, m, n;
int i, j, k = 1;
int mi, mj, ni, nj;
cin >> w >> m >> n;
int arr[1001][w];
for (i = 0; i < 1001; i++){
if ((i + 1) % 2 != 0){
for (j = 0; j < w; j++){
arr[i][j] = k + j;
}
k = arr[i][j - 1];
}else{
int f = 1;
for (j = w - 1; j >= 0; j--, f++){
arr[i][j] = k + f;
}
k = arr[i][0] + 1;
}
}
for (i = 0; i < 1001; i++){
for (j = 0; j < w; j++){
if (arr[i][j] == n){
ni = i;
nj = j;
}
if (arr[i][j] == m){
mi = i;
mj = j;
}
}
}
int a = abs(mi - ni);
int b = abs(mj - nj);
printf("%d\n",a + b);
return 0;
}



4.每周一题之4高斯日记

[问题描述]

1）不能用日期API，因为日期API都是从1970-1-1开始计算的。
2）所以我们模拟翻日历的过程来算日期，具体看代码注释。

1777-4-30是第一天，这年过完，是第x天，

x=1+31+30+31+31+30+31+30+31=155+91=246

1778年365天

1779年365天

1780年366天

  ……


1799年前六个月共181天，还差16天

public class W4 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int x = sc.nextInt();

int y = 1777;

int m = 4;

int d = 30;

for (int i = 2; i <= x; i++) {

d++;

if (m == 2 && ((isLeap(y) && d == 30)||(!isLeap(y) && d == 29))) {
//闰年的2月的30日等于3月1日，非闰年的2月29日相当于3月1日

m = 3;

d = 1;

} else if (m==12&&d==32) { //12月32日相当于下一年的1月1日
y++;
m=1;
d=1;
}else if (d == 32 && (m == 1 || m == 3 || m == 5 ||
m == 7 || m == 8 || m == 10) ) {
//大月的32日等于次月的1日
m++;
d = 1;
} else if (d == 31 && (m == 4 || m == 6 || m == 9 || m == 11)) {
//小月的31日等于次月的1日
m++;
d = 1;
}
}
System.out.println(y+"-"+m+"-"+d);
}

public static boolean isLeap(long year) {
return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0);
}

}


PC/UVa IDs: 110402/120,
Popularity: B, Success rate:high Level: 2

https://vjudge.net/problem/UVA-120
[问题描述]（老师已经为宝宝们翻译好啦）
Stacks and Queues are often considered the bread andbutter of data structures and find use in architecture, parsing,operating systems, and discrete event simulation.Stacks are also important in thetheory of formal languages.

This problem involves both butter and sustenance inthe form of pancakes rather than bread in addition to a finicky server who flips pancakesaccording to a unique, but complete set of rules.

Given a stack of pancakes, you are to write a programthat indicates how the stack can be sorted so that the largest pancake is on the bottom and thesmallest pancake is on the top.

The size of a pancake is given by the pancake’sdiameter.

All pancakes in a stack have different diameters.

Sorting a stack is done by a sequence of pancake“flips”.

A flip consists of inserting a spatula between twopancakes in a stack and flipping (reversing) all the pancakes on thespatula (reversing the sub-stack).

A flip is specified by giving the position of thepancake on the bottom of the sub-stack to be flipped (relative to the whole stack).

The pancake on the bottom of the whole stack hasposition 1
and the pancake on the top of a stack of n pancakeshas position n.

A stack is specified by giving the diameter of eachpancake in the stack in the order in which the pancakes appear.

For example, consider the three stacks of pancakesbelow (in which pancake 8 is the top-most pancake of the left stack):

8 7 2
4 6 5
6 4 8
7 8 4
5 5 6
2 2 7
The stack on the left can be transformed to the stackin the middle via flip(3). The middle stack can be transformed into the right stack via the commandflip(1).

Input（输入）
The input consists of a sequence of stacks ofpancakes. Each stack will consist of between 1 and 30 pancakes and each pancake will have an integerdiameter between 1 and 100. The input is terminated by end-of-file. Each stack is given as a single lineof input with the top pancake on a stack appearing first on a line, the bottom pancake appearing last,and all pancakes separated by a space.

Output（输出）
For each stack of pancakes, the output should echo theoriginal stack on one line, followed by some sequence of flips that results in the stack ofpancakes being sorted so that the largest diameter pancake is on the bottom and the smallest on top. For eachstack the sequence of flips should be terminated by a ‘0’ (indicating no more flips necessary). Once astack is sorted, no more flips should be made.

Sample Input
1 2 3 4 5
5 4 3 2 1
5 1 2 3 4
Sample Output
1 2 3 4 5
0
5 4 3 2 1
1 0
5 1 2 3 4
1 2 0

#include <iostream>
#include <deque>
#include <algorithm>
#include <sstream>
using namespace std;
int main() {
for(string s; getline(cin,s),cout << s << endl ; cout << 0 << endl ){
deque<int> que;
stringstream str(s);
for(int temp; str >> temp; que.push_front(temp));
for(deque<int>::iterator i = que.begin(); i != que.end(); ++i){
deque<int>::iterator imax = max_element(i,que.end());
if(imax != i){
reverse(imax, que.end());
if(que.end() - imax - 1){
cout << que.end() - imax - 1 << " ";
}
reverse(i, que.end());
cout << i - que.begin() + 1<< " ";
}
}
}
return 0;
}