Analyzing the condition of the three-digit (c language)

This problem required to achieve a function, statistics given three-digit number in the interval have the same two perfect square numbers (e.g., 144,676) of.

Interface definition function:
int Search (n-int);

Wherein the parameters passed int n is a positive integer of three digits (the highest non-zero digits). Search function returns [101, n] satisfying the condition number of all of the number of the interval.

Referee test sample program:
#include <stdio.h>
#include <math.h>

int search( int n );

int main()
{
int number;

scanf("%d",&number);
printf("count=%d\n",search(number));
	
return 0;

}

/ * Your code will be embedded here * /

Input Sample:
500

Output Sample:
COUNT =. 6

As perfect square 2 * 2 = 4; 11 * 11 = 121 ...... 3 Here, 11 is a perfect square.

int search( int n )
{
    int d1, d2, d3, count = 0;
    int i, j;

    for (i = 101; i <= n; i++)
    {
        for (j = 11; j * j < i; j++)
            ;
        if (j * j == i)
        {
            d1 = i / 100;   //取出百位
            d2 = i / 10 % 10;  // 十位
            d3 = i % 10;   // 个位
            if (d1 == d2 && d1 == d3) // 三位数相同时跳过
                ;
            else if (d1 == d2 || d1 == d3 || d2 == d3) //两位数相同时
                count++;
        }
    }

    return count;
}
Published 24 original articles · won praise 0 · Views 156

Guess you like

Origin blog.csdn.net/qq_45624989/article/details/105082989