您的位置:首页 > 其它

poj1906解题报告

2010-08-11 20:02 381 查看
题目大意: 求3的k次幂集合{1.3.9.27.81......}中和排在第n位的子集,从小到大输出.....





Sample Input
1
7
14
783
1125900981634049

Sample Output{ }
{ 3, 9 }
{ 1, 9, 27 }
{ 3, 9, 27, 6561, 19683 }
{ 59049, 3486784401, 205891132094649, 717897987691852588770249 }

以前看的二进制解题思想派上了用场,将n-1转换为2进制,然后把输出等于1的对应的二进制的每个项就是结果,因为n小于19位,以前看见过_int64这个数据类型,可以保存19位的整数,但不知道为什么我用这个数据类型提交就是提示编译出错,咋改都不行(马勒隔壁的),最后不得不用高精度了...害我多写了N行代码,,,最后0MSA了....
 
#include<iostream>
#include<stdio.h>
using namespace std;
char out[1000]="1";
void cheng3(int ji)
{
	if(ji==0)
		return;
	while(ji--)
	{
		int i=strlen(out)-1,jinwei=0;
		while(i>=0)
		{
			int k=(out[i]-48)*3+jinwei;
			out[i]=k%10+48;
			jinwei=k/10;
			i--;
		}
		if(jinwei!=0)
		{
			for(i=strlen(out)+1;i>0;i--)
				out[i]=out[i-1];
			out[0]=jinwei+48;
		}
	}
}
int main()
{
	int num[100];char a[100],result[100],zhongjie[100];
	while(scanf("%s",a) && strcmp(a,"0"))
	{
		cout<<"{ ";
		int i,j;
		i=strlen(a)-1;
		while(i>=0)
		{
			if(a[i]=='0')
				a[i]='9';
			else
			{	a[i]--;	break;	}
			i--;
		}
		if(a[0]=='0')
			for(i=0;i<strlen(a);i++)
				a[i]=a[i+1];

		int num1,s=1;
		while(a[0]!='/0')
		{
			num1=0;
			for(i=0,j=0;i<strlen(a);i++)
			{
				num1=10*num1+a[i]-48;
				result[j++]=num1/2+48;
				num1=num1%2;
			}
			result[j]='/0';
			for(i=0;result[i]=='0';i++);
			strcpy(zhongjie,&result[i]);
			strcpy(a,zhongjie);
			num[(s++)]=num1;
		}
		i=s;
		
		for(j=1;j<i;j++) 
			if(num[j]==1)
			{
				cheng3(j-1);
				cout<<out;
				j++;
				break;
			}
			for(;j<i;j++)
				if(num[j]==1)
				{	strcpy(out,"1");	cheng3(j-1);	cout<<", "<<out;}
				cout<<" }"<<endl;
				strcpy(out,"1");
	}
	return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: