// PowerMultiSequence_3.cpp : Defines the entry point for the console application.
//
//
#include "stdafx.h"
#include<math.h>
void swap(double& a,double& b)
{
if(&a==&b)
return;
double tmp=a;
a=b;
b=tmp;
}
int main(int argc, char* argv[])
{
printf("Input n:\n");
double n;
scanf("%lf",&n);
getchar();
//double xH=log(n)/log(2);
//double yH=log(n)/log(3);
unsigned int count=0;
double nums[5000];
double value;
/*
for(int x=0;x<=xH;x++)
for(int y=0;y<=yH;y++)
if( (value=pow(2,x)*pow(3,y)) <= n)
{
count++;
nums[count]=value;
}
*/
double pow2x=1;
double pow3y=1;
for(pow2x=1;pow2x<=n;pow2x*=2)
for(pow3y=1;pow3y<=n;pow3y*=3)
if( (value=pow2x*pow3y) <= n)
{
count++;
nums[count]=value;
}
//下面进行排序
unsigned int lowerIndex;
unsigned int j;
for(unsigned int i=1;i<count;i++)
{
lowerIndex=i;
for(j=i+1;j<=count;j++)
if(nums[lowerIndex]>nums[j])
{
lowerIndex=j;
}
swap(nums[lowerIndex],nums[i]);
}
printf("幂序列中小于%lf的项数:%d\n",n,count);
printf("Input m:");
unsigned int m;
scanf("%d",&m);
getchar();
if(m<=count)
{
int p2=0;
int p3=0;
double tmp_num=nums[m];
/*
while(fmod(tmp_num,2)==0)
{
p2++;
tmp_num/=2;
}
while(fmod(tmp_num,3)==0)
{
p3++;
tmp_num/=3;
}
*/
while(fabs(fmod(tmp_num,2))<1e-6)
{
p2++;
tmp_num/=2;
}
while(fabs(fmod(tmp_num,3))<1e-6)
{
p3++;
tmp_num/=3;
}
printf("从小到大第:%d项:%.0lf=2^%d*3^%d\n",m,nums[m],p2,p3);
}
else
printf("所输入的m大于序列的项数\n");
return 0;
}
PowerMultiSequence_3
最新推荐文章于 2020-08-31 23:17:44 发布