AtCoder Beginner Contest 161 D 题解 [abc161 d]

Original title link: Luogu link ; AtCoder link

Ideas

Each time according to the previous one, calculate the next one as TA-1/TA/TA+1, put it queuein, and finally output the KKInteger popped K times.

Precautions

  • No need to long longknow WA!
  • The previous digit is 0 0When 0, the next digit cannot be− 1 -11 ! (Special judgement)
  • The previous digit was 9 9At 9 o'clock the next digit cannot be10 101 0 ! (Also special judgment)

Code

#include <cstdio>
#include <queue>
using namespace std;

typedef long long LL;

int main(int argc, char** argv)
{
    
    
	int k;
	scanf("%d", &k);
	queue<LL> q;
	for(int i=1; i<10; i++) q.push(i);
	while(true)
	{
    
    
		LL x = q.front(); q.pop();
		if(--k == 0)
		{
    
    
			printf("%lld\n", x);
			return 0;
		}
		int r = x % 10LL;
		x *= 10LL;
		x += r;
		if(r > 0) q.push(x - 1);
		q.push(x);
		if(r < 9) q.push(x + 1);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/write_1m_lines/article/details/105326334