Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <cmath>
- using namespace std;
- //armstrong
- 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
- cout<<"\t";
- cout<<"x="<<x<<" sum="<<sum<<endl;
- if(sum+(digNum-a)*pow(9.0f,digNum)>x+pow(10.0f,digNum+1)-1 ){
- return false;
- }
- if(a==digNum){ //moze spelniac->wypisz liczbe
- cout<<" x="<<x<<endl;
- system("pause");
- return true;
- }
- for(int i=1;i<=9;++i){
- cout<<"adding "<<i<<endl;
- if( !get(digNum,a+1,x+i*pow(10.0f,digNum-a),sum+pow((float)i,digNum) ) ){ //tam juz sie nie da
- break;
- }
- }
- return true;
- }
- int main(int argc, char *argv[])
- {
- //bire cyfre i spr czy mozliwe by w tym przedziale spelniala zaleznosc, jesli tak to biore nastepna dowolna tworzac liczbe.
- cout<<"armstrong"<<endl;
- int digNum=2; //od 2 zaczynamy
- while(true)
- {
- if(digNum*pow(9.0f,digNum)<pow(10.0f,digNum) ){
- cout<<"stopped at "<<digNum<<endl;
- break;
- }
- get(digNum,0,0,0);
- digNum++;
- }
- system("PAUSE");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement