number of daffodils

 

Preliminary study on the number of daffodils

1. What is the number of daffodils

 A daffodil number is a 3-digit number whose sum of the powers of 3 of the digits in each digit is equal to itself (eg: 1^3 + 5^3 + 3^3 = 153).

2. The idea of ​​solving the number of daffodils

  According to the definition, the sum of the cubes of the required numbers should be obtained, and the original number should be compared with the sum to be obtained. If the values ​​are the same, it is the number of daffodils. The variable sum can be used to store the sum of the cubes of the digits to be counted.

  How to store you guys? Obviously, using the original number to continuously take the remainder of 10 can strip the original number from low to high in order.

 

1 while(ISshui/10!=0){
2         s[j++]=ISshui%10;
3         ISshui=ISshui/10; 
4     }
5     if(ISshui/10==0){
6         s[j++]=ISshui%10;;
7     }

 

  Through the above operations, at this time, each bit of the desired number has been stored in the s[n] array from low to high, and the cube operation is performed on each bit in turn and summed, and then the sum is compared with the original number.

3. Complete code (DEV-C++ runs through)

 

  In the final implementation, some optimizations were carried out. Instead of storing the required digits in the array, the cube operation was directly performed, and then stored in the sum , thus eliminating the need for loop summation and saving running time. The function pow(number, times) included in the header file math completes the cube operation, where number represents the number to be cubed and times represents several powers, which should be 3 here. Use while(scanf("%d %d",&m,&n)) for multiple sets of sample input . If multiple sets of sample input are not required, change it to normal scanf() .

  The code finally implements the input of two numbers m and n , and outputs all the daffodils between m and n . If there is no daffodils between the two numbers, it outputs no .

 

1 #include " stdio.h " 
2 #include " math.h " 
3  
4  // Determine whether it is the number of daffodils 
5  int SXH( int ISshui)
 6  {
 7      int sum= 0 ;             // Store the cubic sum of each bit and 
8      int m= ISshui;
 9      
10      while (ISshui/ 10 != 0 ){
 11          sum=sum+pow((ISshui% 10 ), 3 );
 12          ISshui=ISshui/10; 
13     }
14     if(ISshui/10==0){
15         sum=sum+pow((ISshui%10),3);
16     }
17     
18     if(sum!=m){
19         sum=0;
20     }
21     
22     return sum;
23 }
24 
25 int main(){
26     
27     int m,n,count=0;
28     int i,j=0;
29     while(scanf("%d %d",&m,&n)){
30         if(m==0&&n==0){
31             break;
32         }
33         for(i=m;i<=n;i++){
34             if(SXH(i)!=0){
35                 printf("%d ",i);
36                 count++;
37             }
38         }    
39     
40         if(count==0){
41             printf("no");
42         }
43         printf("\n");
44         j=0;
45         count=0;
46     }
47     
48     return 0;
49 } 

4. Code defects

  Although the above code has been simply optimized, the time complexity is still too large. The ultimate goal is to control its running time within 1 s, which has not been solved yet (ㄒoㄒ)/~~, I hope you can enlighten me.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324420640&siteId=291194637