Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cmath>
  4. using namespace std;
  5. //armstrong
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. bool get(const int digNum,int a,int x,int sum){ //dignum-ile cyfr, a-ile od konca(ile cyfr ma utworzona liczba),x-aktualna suma, sum-suma cyfr^n
  18. cout<<"\t";
  19. cout<<"x="<<x<<" sum="<<sum<<endl;
  20. if(sum+(digNum-a)*pow(9.0f,digNum)>x+pow(10.0f,digNum+1)-1 ){
  21.  
  22. return false;
  23. }
  24. if(a==digNum){ //moze spelniac->wypisz liczbe
  25. cout<<" x="<<x<<endl;
  26. system("pause");
  27. return true;
  28. }
  29. for(int i=1;i<=9;++i){
  30. cout<<"adding "<<i<<endl;
  31. if( !get(digNum,a+1,x+i*pow(10.0f,digNum-a),sum+pow((float)i,digNum) ) ){ //tam juz sie nie da
  32. break;
  33. }
  34. }
  35. return true;
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39. //bire cyfre i spr czy mozliwe by w tym przedziale spelniala zaleznosc, jesli tak to biore nastepna dowolna tworzac liczbe.
  40. cout<<"armstrong"<<endl;
  41. int digNum=2; //od 2 zaczynamy
  42. while(true)
  43. {
  44. if(digNum*pow(9.0f,digNum)<pow(10.0f,digNum) ){
  45. cout<<"stopped at "<<digNum<<endl;
  46. break;
  47. }
  48. get(digNum,0,0,0);
  49. digNum++;
  50. }
  51. system("PAUSE");
  52. return EXIT_SUCCESS;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement