51Nod - 1385 凑数字

51Nod - 1385

如果是n位数,x1 x2 x3 ... xn

从1到n的所有数中位数n-1的数字一定含有

1111...11,2222...22,...,9999...99

对于0 考虑n位数1000...00 其中有n-1个0

那么n-1位数中0-9都应该有n-1个(ans += 10*(n-1))

考虑第一位数x1需要1, 2, 3, ..., x1(ans += x1)

但是如果第一位数大于第二位数 比如21

这时2最多出现一次,以为最高位出现时,

它的下一位不能出现2, 但是如果计算ans = 2 + 10*1

显然2出现了两次,在推导发现221,2221与21情况相同,均多计算了一次

所以在单独判断x1 x1 x1 ... x1 x2 ... xn (x1 > x2)

如果符合这种情况, 那么ans要减1

综上所扯,我们可以得出代码 T U T

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int maxn = 1e5 + 10;
 6 char str[maxn];
 7 int main()
 8 {
 9     while (~scanf("%s", str)) {
10         int len = strlen(str) - 1;
11         int ans = str[0] - '0' + 10 * len;
12         if (strncmp(str, str+1, len) > 0) --ans;
13         cout << ans << endl;
14     }
15     return 0;
16 }

 

posted @ 2018-10-19 20:18  向往天空的羽毛  阅读(167)  评论(0编辑  收藏  举报