CodeForces

/*
  法一:
  这是我最初的做法,制表,但是...这种方法其实,完全想明白,规避所有漏洞,并不是那么简单,而且即便想明白了,思路其实也不是清晰直接的...下次还是不要这样做了
  
  又及,之前没想到先找这组 Lucky Number的顺次规律,而是...一个一个数去枚举求解,甚至,我一开始,还是把枚举的数作为数组下标的,而不是将目前已有的Lucky Numbr个数做下标,枚举的数作为数组元素值
  
  现在想来,当时真是太傻了,难怪老是WA,就像我这写题...对时效都不怎么敏感,没遇到TLE已经算是万幸了!...其实本来用枚举的数做下标时,dev上已经TLE了,只是我没交上去判而已...
  
  以及,以后做题时不要老想着暴力破解那种蛮力做法,先想想能不能使巧劲,多想想:还有没有更好的方法?还能不能再降低些复杂度等等,多想想再真正动手去敲!!!(毕竟做ACM的血泪教训就是:第一眼看完想到的方法,往往都是不能AC的,往往都超时)
*/



#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
const int M = 2e9;
typedef long long LL;
LL a[N];

int main()
{
	memset(a, 0, sizeof(a));
/*	for (LL i = 1; i <= N; i++)
	{
		int sum = 0, not0 = 0, tp;
		LL j = i;
		while (j)
		{
			tp = j % 10;
			sum++;
			if (tp) not0++;
			if (not0 > 1) break;
			j = j / 10;
		}
		if (not0 == 1) a[cnt++] = i;
	}*/
	a[0] = 1; int cnt = 1;
	for (LL i = 1, j = 1; i <= M ; )
	{
		while (j < 10 * i)
		{
			j += i;
			a[cnt++] = j;
		}
		i *= 10; 
		j = i;
	}
//	for (int i = 0; i < cnt; i++) cout << a[i] << " ";
	int n;
	while (cin >> n)
	{
		for (int i = 0; i < cnt; i++)
		{
			if (a[i] > n)
			{
				cout << a[i] - n << endl;
				break;
			}
		}
	}

	return 0;
}



/*法二
这种方法的思路很值得学习
log10(n)得到的,是n除了最高位外,还有多少位
将这个数字传入pow函数,可得到n的基数,即n的最高位改为1,其他位改为0的数,先记作tp
n/tp*tp,相当于最高位不变,其他位置0,再加上tp,相当于最高位加1
至于为什么这样处理,可以参考Lucky Number序列,发现它们就是满足这样的规律
*/
#include <bits/stdc++.h>
using namespace std;
int pow(int n)
{
	int ans = 1;
	while (n--) ans *= 10;
	return ans;
} 

int main()
{
	int n;
	cin >> n;
	if (n < 10)
	{
		cout << 1 << endl; return 0;
	}
	int high = log10(n);
	int ans = n / pow(high) * pow(high) + pow(high);
	cout << ans - n << endl;
	return 0;
}


/*
法三
和法一思路基本一致,算出除最高位以外的位数,暂记t1,这就是新Lucky Year从个位开始,向左数的连续0的个数
分离出最高位,下一个Lucky Year,最高位是n的最高位+1

比较有区别的地方是,得到最高位的方式和法一不同,具体就自己再比较和感受吧
*/

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin >> n;
	
	int tp = n, dig = 0, ans = 1;
//	while (tp >= 10) 和下句效果相同 
	while (tp / 10)
	{
		dig++;
		tp /= 10;
	}
	tp++;
//	for (int i = 0; i < dig; i++) 和下句效果相同 
	while (dig--) ans *= 10;
	cout << tp * ans - n << endl;
	return 0;
}


//其实思路都是大同小异,也不好叫法四了,最多叫写法四 T^T
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin >> n;
	
	int tp = n, dig = 0, ans = 1, i, j;
	for (i = 0; tp / 10; i++)
		tp /= 10;
		
	if (i == 0)
	{
		cout << 1 << endl;
		return 0;
	}
	for (j = 1; j <= i; j++) ans *= 10;
	ans *= (tp + 1);
	cout << ans - n << endl;
	return 0;
}


原文地址:https://www.cnblogs.com/mofushaohua/p/7789511.html