网友222.70.181.* #include<iostream> using namespace std; bool IsUglyNum( int nNum, int* pNum, int nCur ); int CalUglyNum( int nIndex ) { if( nIndex > 4 ) { int *pNum; int nCur = 3; pNum = new int[ nIndex ]; int nNum = 6; pNum[ 0 ] = 2; pNum[ 1 ] = 3; pNum[ 2 ] = 4; pNum[ 3 ] = 5; bool bContinue = true; for( int nCount = 4; nCount < nIndex; ) { while( bContinue ) { if( IsUglyNum( nNum, pNum, nCur ) ) { pNum[ ++nCur ] = nNum; nCount++; nNum++; bContinue = false; } else nNum++; } bContinue = true; } delete []pNum; return --nNum; } else { switch( nIndex ) { case 1: return 2; case 2: return 3; case 3: return 4; case 4: return 5; default: return 0; } } } bool IsUglyNum( int nNum, int* pNum, int nCur ) { if( nCur < 0 ) return false; if( !( nNum % pNum[ nCur ] ) ) { nNum /= pNum[ nCur ]; if( nNum == 1 ) return true; else return IsUglyNum( nNum, pNum, nCur ); } else { return IsUglyNum( nNum, pNum, nCur - 1 ); } } int main() { int nIndex; cout << "please input a number between 1 to 200: "; cin >> nIndex; if( nIndex < 1 || nIndex > 200 ) cout << "out of range!" << endl; else cout << "the ugly number is: " << CalUglyNum( nIndex-1); return 1; } 网友 dead_fox#include<iostream.h> #include<stdlib.h> bool fon(int n); int main() { int m,j=0,k=0; cout<<"请输入1~200间的一个正整数"<<endl; cin>>m; if(m<1||m>200) { cout<<"输入错误!"<<endl; exit(0); } while(k<m) { j++; if(fon(j)) k++; } cout<<"第 "<<m<<" 个Ugly数是: "<<j<<endl<<endl; system("pause"); return 0; } bool fon(int n) { if(n==1) return 1; while(!(n%2)) { n/=2; if(n==1) return 1; } while(!(n%3)) { n/=3; if(n==1) return 1; } while(!(n%5)) { n/=5; if(n==1) return 1; } return 0; } //不知道为什么我不能登陆——dead_fox 网友 222.173.180.* int main() { long num=0,i=0,j=0,k=0; printf("please input number(<=200):\n"); scanf("%d",&num); while(i++,j!=num) { k=i; while(k%2?false:k>>=1); while(k%3?false:k/=3); while(k%5?false:k/=5); if(k==1) j++; } printf("\n%d\n",--i); return 0; }

评论