987654321 problem

题目大意:求n位数的平方的后几位结果是987654321的个数是多少。

分析:刚看到这道题的时候怀疑过有没有这样的数,于是暴力跑了一下,发现还真有,9位的数有8个,如下:

i=111111111, i*i=12345678987654321
i=119357639, i*i=14246245987654321
i=380642361, i*i=144888606987654321
i=388888889, i*i=151234567987654321
i=611111111, i*i=373456789987654321
i=619357639, i*i=383603884987654321
i=880642361, i*i=775530967987654321
i=888888889, i*i=790123456987654321

知道这个后就比较简单了,因为首位不能为0,所以10位数字的时候有9*8种,11位数字的时候,第10位很明显可以为0了,于是又9*10*8种,以此类推,每增长以为也就多乘上一个10....

代码如下:

===================================================================================================================

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

const int MAXN = 1e4+7;
const long long Mod = 1e9;
const long long res = 987654321;

int main()
{
    int n;

    scanf("%d", &n);

    if(n <= 8)
        printf("0
");
    else if(n == 9)
        printf("8
");
    else
    {
        printf("72");
        for(int i=11; i<=n; i++)
            printf("0");
        printf("
");
    }
}
原文地址:https://www.cnblogs.com/liuxin13/p/4810897.html