AtCoder Beginner Contest 161 D Lunlun Number playing table + finding rules + structure

AtCoder Beginner Contest 161 The   number of contestants is 9927   fast, see all questions 5 minutes after the start of the contest

AtCoder Beginner Contest 161 D Lunlun Number playing table + finding rules + structure

See https://blog.csdn.net/mrcrack/article/details/104454762 for the general catalog

Online evaluation address https://atcoder.jp/contests/abc161/tasks/abc161_d

This kind of problem, manual simulation, corresponds to thousands of data, or metering, it is not easy to simulate data errors.

The table code is as follows

#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std;
int a[15];
int judge(LL x){
	int i,top=0;
	while(x){
		a[++top]=x%10;
		x/=10;
	}
	for(i=2;i<=top;i++)
		if(abs(a[i]-a[i-1])>1)return 0;
	return 1;
}
int main(){
	int cnt=0;
	LL i,n;
	scanf("%lld",&n);
	for(i=1;i<=n;i++)
		if(judge(i))cnt++,printf("%lld,",i);
	printf("\ncnt=%d\n",cnt);
	return 0;
}

Compile and run the table code, enter 9999, and get the following output data

1,2,3,4,5,6,7,8,9,
10,11,12,
21,22,23,
32,33,34,
43,44,45,
54,55,56,
65,66,67,
76,77,78,
87,88,89,
98,99,
100,101,
110,111,112,
121,122,123,
210,211,212,
221,222,223,
232,233,234,
321,322,323,
332,333,334,
343,344,345,
432,433,434,
443,444,445,
454,455,456,
543,544,545,
554,555,556,
565,566,567,
654,655,656,
665,666,667,
676,677,678,
765,766,767,
776,777,778,
787,788,789,
876,877,878,
887,888,889,
898,899,
987,988,989,
998,999,
1000,1001,
1010,1011,1012,
1100,1101,
1110,1111,1112,
1121,1122,1123,
1210,1211,1212,
1221,1222,1223,
1232,1233,1234,
2100,2101,
2110,2111,2112,
2121,2122,2123,
2210,2211,2212,
2221,2222,2223,
2232,2233,2234,
2321,2322,2323,
2332,2333,2334,
2343,2344,2345,
3210,3211,3212,
3221,3222,3223,
3232,3233,3234,
3321,3322,3323,
3332,3333,3334,
3343,3344,3345,
3432,3433,3434,
3443,3444,3445,
3454,3455,3456,
4321,4322,4323,
4332,4333,4334,
4343,4344,4345,
4432,4433,4434,
4443,4444,4445,
4454,4455,4456,
4543,4544,4545,
4554,4555,4556,
4565,4566,4567,
5432,5433,5434,
5443,5444,5445,
5454,5455,5456,
5543,5544,5545,
5554,5555,5556,
5565,5566,5567,
5654,5655,5656,
5665,5666,5667,
5676,5677,5678,
6543,6544,6545,
6554,6555,6556,
6565,6566,6567,
6654,6655,6656,
6665,6666,6667,
6676,6677,6678,
6765,6766,6767,
6776,6777,6778,
6787,6788,6789,
7654,7655,7656,
7665,7666,7667,
7676,7677,7678,
7765,7766,7767,
7776,7777,7778,
7787,7788,7789,
7876,7877,7878,
7887,7888,7889,
7898,7899,
8765,8766,8767,
8776,8777,8778,
8787,8788,8789,
8876,8877,8878,
8887,8888,8889,
8898,8899,
8987,8988,8989,
8998,8999,
9876,9877,9878,
9887,9888,9889,
9898,9899,
9987,9988,9989,
9998,9999,
cnt=327

Pick out special data for research

98,99,
100,101,
898,899,
998,999,
1000,1001,
1100,1101,
2100,2101,
7898,7899,
8898,8899,
8998,8999,
9898,9899,
9998,9999,

Take 8, 9, 10 as an example to show the data generation process

8生成的2位数有87,88,89
9生成的2位数有98,99
10生成的2位数有100,101

其它数据生成过程,不外乎上述三种过程。

After being familiar with the data generation process, construct 100,000 sets of data that meet the intent of the question and select the data to be tested.

AC code is as follows

#include <stdio.h>
#define maxn 100010
#define LL long long
LL rd[maxn]={1,2,3,4,5,6,7,8,9};
int main(){
	int i,id,k;
	scanf("%d",&k);
	i=9,id=0;
	while(i<k){
		if(rd[id]%10==0){//10生成的2位数有100,101
			rd[i]=rd[id]*10;
			rd[i+1]=rd[id]*10+1;
			i+=2;
		}else if(rd[id]%10==9){//9生成的2位数有98,99
			rd[i]=rd[id]*10+8;
			rd[i+1]=rd[id]*10+9;
			i+=2;
		}else{//8生成的2位数有87,88,89
			rd[i]=rd[id]*10+rd[id]%10-1;
			rd[i+1]=rd[id]*10+rd[id]%10;
			rd[i+2]=rd[id]*10+rd[id]%10+1;
			i+=3;
		}
		id++;
	}
	printf("%lld\n",rd[k-1]);
	return 0;
}

 

Published 660 original articles · praised 562 · 480,000 views

Guess you like

Origin blog.csdn.net/mrcrack/article/details/105322122