uva 763 Fibinary Numbers
2014-03-18 15:47
113 查看
Fibinary Numbers |
numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence are:
Where each term is the sum of the two preceding terms (note that there is only one 1 in the sequence as defined here). Using this scheme, the sequence ``1010'' could be interpreted as
.
This representation is called a Fibinary number.
Note that there is not always a unique Fibinary representation of every number. For example the number 10 could be represented as either 8 + 2 (10010) or as 5 + 3 + 2 (1110). To make the Fibinary representations unique, larger Fibonacci terms
must always be used whenever possible (i.e. disallow 2 adjacent 1's). Applying this rule to the number 10, means that 10 would be represented as 8+2 (10010).
Input and Output
Write a program that takes two valid Fibinary numbers and prints the sum in Fibinary form. These numbers will have at most 100 digits.In case that two or more test cases had to be solved, it must be a blank line between two consecutive, both in input and output files.
Sample Input
10010 1 10000 1000 10000 10000
Sample Output
10100 100000 100100
Miguel Revilla
2000-12-30
题目大意:给你两串数字,已斐波那契数列表示,从右往左 第k 位 的 权值为 f[k] ,求两串数字的和,要求相邻两个数字不能都为1,用斐波那契数列表示。
解题思路:利用两个性质 (1) 2*a[k]=a[k+1]+a[k-2] ,(2) a[k]=a[k-1]+a[k-2],模拟位运算,第k位如果>=2 利用性质(1),如果相邻两位都为1,利用性质(2),直到做到满足要求为止。
#include <iostream> #include <string> #include <algorithm> using namespace std; string rev(string st){ int len=st.length(); for(int i=0;i<len/2;i++){ swap(st[i],st[len-i-1]); } return st; } void computing(string a,string b){ if(a.length()<b.length()) swap(a,b); a=rev(a);b=rev(b); for(int i=0;i<b.length();i++) a[i]=a[i]+b[i]-'0'; //cout<<a<<endl; bool flag=true; while(flag){ flag=false; for(int i=0;i<a.length();i++){ while(a[i]>='2'){ flag=true; a[i]-=2; if(i+1<a.length()) a[i+1]++; else a.push_back('1'); if(i-2>=0) a[i-2]++; else if(i-2==-1) a[i-1]++; } } for(int i=0;i<a.length();i++){ if(a[i]=='1' && i+1<a.length() ){ if(a[i+1]=='1'){ a[i]--; a[i+1]--; flag=true; if( i+2<a.length() ) a[i+2]++; else a.push_back('1'); } } } } a=rev(a); cout<<a<<endl; } int main(){ int casen=0; string a,b; while(cin>>a>>b){ if(casen>0) cout<<endl; computing(a,b); casen++; } return 0; }
相关文章推荐
- 【软件工程】软件开发方法综述
- 【转】ext4+delalloc造成单次写延迟增加的分析
- uva 763 Fibinary Numbers
- 蛋疼的 查询,
- 翻纸牌 高校俱乐部 英雄会 csdn
- linux下两次fclose运行时出错及解决方法
- ARM9网络环境搭建(有路由)
- 河南省第六届程序设计大赛--Card Trick
- NYOJ 613 免费馅饼
- jQuery插件开发
- 活动目录之组策略管理用户
- SQL2005学习(十二),使用日志文件恢复到故障点
- 到底什么是一个网站的建站流程,其实就是关于网站的大致构造吧
- Java发送HTTP的POST请求
- poj 1064 Cable master
- IIS 301 重定向 带参数链接
- JUC中的锁
- HDUOJ-----4506小明系列故事——师兄帮帮忙
- 在家学习 利器 记录每日点滴
- windows下svn的使用