正文

C++吧第一界编程大赛(其它人算法) 2005-11-10 19:08:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/elva6401/6749.html

分享到:

网友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;
}

阅读(4529) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册