(1) set the status array p of 10 elements, and record the occurrence of the numbers 0-9 in 6-digit and 3-digit. All array elements are assigned a value of 1, indicating that the numbers 0-9 have not been used.
(2) for each number of attempts, 3 digits x, 6 digits = x*x, and each digit is taken as the subscript of the array. If the corresponding element is 1, the digit will appear for the first time,
Assign a value of 0 to the corresponding element to indicate that the number has appeared once. Otherwise, if the corresponding element is 0, it means that there are duplicate numbers, ending the attempt.
(3) when 9 elements in the state array p are 0, the solution of the problem is found. However, it is necessary to scan the array p once to determine the solution. To avoid this step,
Set a counter K, record the number of different numbers in the process of taking each digit of X, x*x, and find the solution when k=9.
1 #include<stdio.h> 2 int main() 3 { 4 long x; 5 int p[10]; 6 int i,t,k; 7 int num=0; 8 int n,f,y,m; 9 for(x=100;x<=999;x++) 10 { 11 for(i=0;i<=9;i++) 12 p[i]=1; 13 y=x*x; //y Represents 6 digits 14 f=y; //take y Temporarily stored in f in 15 n=x; //n Represents 3 digits 16 if(f<=999999) 17 { 18 k=0; 19 for(i=1;i<=6;i++) 20 { 21 t=n%10; 22 n=n/10; 23 m=f%10; 24 f=f/10; 25 if(m==t) 26 { 27 k=0; 28 break; 29 } 30 if(i<4) 31 if(p[t]==1) 32 { 33 p[t]=0; 34 k++; 35 } 36 else 37 { 38 k=0; 39 break; 40 } 41 if(p[m]==1) 42 { 43 p[m]=0; 44 k++; 45 } 46 else 47 { 48 k=0; 49 break; 50 } 51 } 52 if(k==9) 53 { 54 num=num+1; 55 printf("%ld\t%d\n",x,y); 56 } 57 } 58 else 59 continue; 60 } 61 printf("\n"); 62 printf("%d\n",num); 63 return 0; 64 }