Advertisement
llvlleo1810

Tìm Số Hoàn Hảo trong khoảng cho trước

Apr 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int n1,n2,i,j=0,a[1000],tong,k;
  6.     scanf("%d %d",&n1,&n2);
  7.     if (n1>n2)
  8.     {
  9.         for (k=n2;k<=n1;k++)//xet cac so hoan hao tu k =1 den n
  10.         {
  11.             tong=0;//reset cac bien ve 0
  12.             j=0;
  13.             for (i=1;i<k;i++)
  14.             {
  15.                 if (k%i==0)//tim uoc cua k
  16.                 {
  17.                     a[j]=i;//sau do gan vao chuoi a
  18.                     j++;
  19.                 }
  20.             }
  21.             for (i=0;i<j;i++)//bien j=so uoc cua k vi j++ theo uoc
  22.                 tong+=a[i];//tong cua cac uoc
  23.             if (tong==k)
  24.                 printf("%d ",tong);
  25.         }
  26.     }
  27.     else if (n1<n2)
  28.     {
  29.         for (k=n1;k<=n2;k++)//xet cac so hoan hao tu k =1 den n
  30.         {
  31.             tong=0;//reset cac bien ve 0
  32.             j=0;
  33.             for (i=1;i<k;i++)
  34.             {
  35.                 if (k%i==0)//tim uoc cua k
  36.                 {
  37.                     a[j]=i;//sau do gan vao chuoi a
  38.                     j++;
  39.                 }
  40.             }
  41.             for (i=0;i<j;i++)//bien j=so uoc cua k vi j++ theo uoc
  42.                 tong+=a[i];//tong cua cac uoc
  43.             if (tong==k)
  44.                 printf("%d ",tong);
  45.         }
  46.     }
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement