AtCoder Beginner Contest 161 D 题解 [abc161 d]

原题链接:洛谷链接AtCoder链接

思路

每次根据上一位,计算下一位为TA-1/TA/TA+1,放入queue中,最后输出第 K K K次弹出的整数。

注意事项

  • 不用long longWA
  • 上一位为 0 0 0时下一位不能为 − 1 -1 1!(要特判)
  • 上一位为 9 9 9时下一位不能为 10 10 10!(也要特判)

代码

#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;
}

猜你喜欢

转载自blog.csdn.net/write_1m_lines/article/details/105326334
今日推荐