PowerMultiSequence_3

来源:互联网 发布:为什么移动数据连不上 编辑:程序博客网 时间:2024/05/19 22:57
// 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);}elseprintf("所输入的m大于序列的项数\n");return 0;}