Solution of 11970 - Lucky Numbers

Problem Description
source:https://uva.onlinejudge.org/external/119/p11970.html

Every person has its own numbers that he considers lucky. Usually the numbers are fixed like 3 or 7 and do not depend on anything. This lucky number model seems to be very primitive for John, so he decided to upgrade it for his own use. Maybe more complex model will bring more luck to him? John has added a dependency for lucky numbers on specific integer N (for example N can be ordinal number of day in year or some other meaning). For each N John considers some number X lucky if and only if fraction X/√( N − X) value is integer and greater than zero. 

Input 

The number of tests T (T ≤ 100) is given on the first line. T lines follow, each of them contains one integer N (1 ≤ N ≤ 109 ) described above. 


Output 

For each test case output a single line ‘Case T: S’. Where T is the test case number (starting from 1) and S is increasing sequence of numbers considered lucky by John for specified N. Please refer to the sample output for clarity. 

Sample Input 

 3
16
109
33

Sample Output 

Case 1: 12 15
Case 2: 108
Case 3: 24 32


Solution:
#include<stdio.h>
#include<math.h>

 int main()
 {
     long long int t, n, x, i, j;
     long double day;
     while(scanf("%lld",&t)==1)
     {
         for(i=1;i<=t;i++)
         {
             scanf("%lld",&n);
              printf("Case %lld:",i);
             for(x=(long long int)sqrt(n);x>=1;x--)
             {
                 j=n-(x*x);
                 if(j%x==0 && j>0)
                    {
                        printf(" %lld",j);
                    }
             }

            printf("\n");
         }
     }
     return 0;
 }
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience