find the nth digit HDU - 1597 二分 思维

题解

打表计算1+2+3+…+N的所有和 二分查找到第一个小于N的那个和 N减去他
接下来就是9个一循环 N = (N - 1) % 9 + 1得到是哪个数字

AC代码

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int INF = 0x3f3f3f3f;

int main()
{
#ifdef LOCAL
	freopen("C:/input.txt", "r", stdin);
#endif
	vector<ll> vec;
	for (int i = 1000000; i >= 0; i--)
		vec.push_back((1LL + i) * i / 2);
	int K;
	cin >> K;
	for (int ti = 0; ti < K; ti++)
	{
		ll N;
		scanf("%lld", &N);
		ll k = upper_bound(vec.begin(), vec.end(), N, greater<ll>()) - vec.begin();
		N -= vec[k];
		N = (N - 1) % 9 + 1;
		printf("%lld\n", N);
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/CaprYang/article/details/85226145