UVA 10706 Number Sequence(模拟)

首先,输入的i很大,如果想直接不断跑1 12 123这样上去直接找必定是超时的。

可以发现,一个数x,他12345……x一共有多少位我们是可以直接求出来的。所以我们每次直接减去这么多个数字即可。所以我们从1开始枚举x,每次i减去他的长度,直到i小于0,就说明答案就在12345……x中,直接暴力枚举即可得到答案。

那么这个x可能到多大呢?我们可以估算一下,假设x=1e5,直接用等差数列求和公式,那么sum至少是5e9 + 1e5。所以x最大不可能大于1e5,T只有15,这是完全够用的复杂度了。

代码如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;

int Count(int s) {
	int ans = s, tmp = 10;
	while(s >= tmp) {
		ans += s - tmp + 1;
		tmp *= 10;
	}
	return ans;
}

int Count2(int x) {
	int cnt = 0;
	while(x) {
		x /= 10;
		cnt++;
	}
	return cnt;
}

stack <int> sta;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif
	int T = 10000;
	int ans, a;
//	int t[10005];
//	for(int i = 0; i < 10005; i++)
//		t[i] = i + 1;
	scanf("%d", &T); 
	for(int k = 0; k < T; k++) {
		scanf("%d", &a);
//		a = t[k];
//		printf("%d\n", a);
		while(!sta.empty())
			sta.pop();
		int s = 1, cnt;
		while(a > 0) {
			cnt = Count(s);
			a -= cnt;
			s++;
		}
		a += cnt, s--;
		for(int i = 1; ; i++) {
			if(a <= 0) {
				i--;
				a += Count2(i);
				while(i) {
					sta.push(i % 10);
					i /= 10;
				}
				
				while(a--) {
					ans = sta.top();
					sta.pop();	
				}
				break;
			}
			a -= Count2(i);
		}
		printf("%d\n", ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值