呵呵 我是菜鸟,麻烦编个成品
设 a为一整数、若能使a^2=Ba成立,则称a为守形数,例如5^2=25,   25^2=625, 编写一个形参为一正整数,判断该正整数是否为守形数,并用该过程寻找1~1000内的守形数.

解决方案 »

  1.   

    #include <iostream.h>bool IsGuard(int x)
    {
        int y = x;
        int n = 10;
        while (y/=10) n *= 10;
        return (x*x - x) % n == 0;
    }void main()
    {
        for (int i = 1; i < 1000; i++)
        {
            if (IsGuard(i))
            {
                cout << i << endl;
            }
         }
    }
      

  2.   

    VC6编译通过,运行结果:1
    5
    6
    25
    76
    376
    625
    Press any key to continue
      

  3.   

    #include <iostream.h>bool IsGuard(int x)
    {
        int y = x;
        int n = 10;
        while (y/=10) n *= 10;
        return x*x > n && (x*x - x) % n == 0;
    }void main()
    {
        for (int i = 1; i < 1000; i++)
        {
            if (IsGuard(i))
            {
                cout << i << endl;
            }
         }
    }
    VC6 编译通过,运行结果如下:
    5
    6
    25
    76
    376
    625
    Press any key to continue