poj1906解题报告
2010-08-11 20:02
381 查看
题目大意: 求3的k次幂集合{1.3.9.27.81......}中和排在第n位的子集,从小到大输出.....
Sample Input
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; }
相关文章推荐
- poj解题报告——2234
- poj 2449 Remmarguts' Date 第k短路 A*+spfa 解题报告
- POJ 1016(自总结数 字符串处理) 解题报告
- POJ 1046(颜色映射 简单数学) 解题报告
- poj1753解题报告(2):BFS
- POJ 1214 解题报告
- LA-3883 & POJ-3518 Prime Gap 解题报告
- poj 3228 Gold Transportation 最小生成树+带权并查集 解题报告
- poj解题报告——2348
- POJ-1995-Raising Modulo Numbers 解题报告
- nlogn求逆序数 POJ 2299解题报告
- POJ 1936(字符串匹配) 解题报告
- POj 1105解题报告
- POJ 3414 解题报告!
- POJ 1003解题报告
- poj 1007 DNA Sorting 解题报告
- 【转】POJ题目分类 - 实时更新我所有的解题报告链接
- POJ-3087 Shuffle'm Up 解题报告(陈小宾)
- POJ_1321 棋盘问题解题报告
- POJ 1007 DNA Sorting [解题报告] Java