Advertisement
llvlleo1810

Tìm số Amstrong trong khoảng cho trước

Apr 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int n, m;
  6.  
  7. void input() {
  8.  cin >> n >> m;
  9. }
  10.  
  11. int socs(int x) {
  12.  int dem = 0;
  13.  while(x > 0) {
  14.   x = x/10;
  15.   dem++;
  16.  }
  17.  return dem;
  18. }
  19.  
  20.  
  21. bool isAmstrong(int x) {
  22.  int tmp = x;
  23.  int soMu = socs(x);
  24.  int tong = 0;
  25.  while(x > 0) {
  26.   int t = x %10;
  27.   int mut =pow(t, soMu);
  28.   tong += mut;
  29.   x/=10;
  30.  }
  31.  if(tong == tmp) return true;
  32.  return false;
  33. }
  34.  
  35. void findAmstrong() {
  36.  int dem = 0;
  37.  if(n < m) {
  38.   for(int i = n; i<=m; i++) {
  39.    if(isAmstrong(i)) {
  40.     dem++;
  41.     cout << i << " ";
  42.    }
  43.   }
  44.  } else {
  45.   for(int i = m; i <= n; i++) {
  46.    if(isAmstrong(i)) {
  47.     dem++;
  48.     cout << i << " ";
  49.    }
  50.   }
  51.  }
  52.  if(dem == 0)
  53.   cout << "0";
  54. }
  55.  
  56. int main() {
  57.  
  58.  input();
  59.  findAmstrong();
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement