3 1 50 500
0 1 15HintFrom 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499", so the answer is 15.
这个题被longlong坑爽了……没改完,wa了若干次
跟前一个题太像了,不细说了哈……主要是因为之前浏览器抽了一次,写的东西没有了,不想再打了……
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; long t; long long n; long long f[23][3]; ///f[i][0] i位的数字 没有不符合条件的数 ///f[i][1] i位的数字 没有不符合条件的数但最高位是9 ///f[i][1] i位的数字 有不符合条件的数 void prepare() { memset(f, 0, sizeof(f)); f[0][0] = 1; for (long i = 1; i <= 20; i++) { f[i][0] = f[i - 1][0] * 10LL - f[i - 1][1]; f[i][1] = f[i - 1][0]; f[i][2] = f[i - 1][2] * 10LL + f[i - 1][1]; } } long long calc(long long n) { long len = 0; long long bit[30] = {0}; long long tmp = n; while (n) { bit[++len] = n % 10; n /= 10; } long long re = 0; bool b = false; for (long i = len; i > 0; i--) { re += f[i - 1][2] * bit[i]; if (b) re += f[i - 1][0] * bit[i]; if ((!b) && (bit[i] > 4)) re += f[i - 1][1]; if (bit[i] == 9 && bit[i + 1] == 4) b = true; } return re; } int main() { scanf("%d", &t); prepare(); while (t--) { cin >> n; cout << calc(n + 1) << endl; } return 0; }