Death to Binary?
来源:互联网 发布:js match 匹配字符 编辑:程序博客网 时间:2024/06/07 13:23
该题主要是。将求出的结果(N)换算为没有连续1的串,从高位到低位寻找,找到第一个小于等于N的位置即为最后字符的长度,继续向前遍历组成N,位置赋值为 1 ,就将该数(Fn(i))从N中减去,直至遍历完成。(注意:如果i小于零,说明该数为 0 )
#include<iostream>#include<cstring>#include<cstdio>using namespace std;typedef long long ll;ll fn[50]={1,2,3,5,8};void init(){ for(int i=5;i<50;i++) fn[i]=fn[i-1]+fn[i-2];}void out(ll num,string& str){ int i; for(i=41;i>=0;i--){ if(num>=fn[i]){ break; } } for(int j=i;j>=0;j--){ if(num>=fn[j]){ str+='1'; num-=fn[j]; }else{ str+='0'; } } if(i<0) str+='0';}int main(){ init(); string str1,str2; while(cin>>str1>>str2){ int len1=str1.length(); int len2=str2.length(); ll num1=0,num2=0,num3; for(int i=0;i<len1;i++) if(str1[len1-1-i]=='1') num1+=fn[i]; for(int i=0;i<len2;i++) if(str2[len2-1-i]=='1') num2+=fn[i]; num3=num1+num2; string str3; str1.clear();str2.clear(); out(num1,str1); out(num2,str2); out(num3,str3); cout<<" ";for(int i=0;i<str3.length()-str1.length();i++) cout<<' '; cout<<str1<<endl; cout<<"+ ";for(int i=0;i<str3.length()-str2.length();i++) cout<<' '; cout<<str2<<endl; cout<<" ";for(int i=0;i<str3.length();i++) cout<<'-'; cout<<endl; cout<<" "; cout<<str3<<endl; cout<<endl; } return 0;}
阅读全文
0 0
- Death to Binary?
- POJ 2116 Death to Binary?(模拟题)
- Death to Binary? 模拟+斐波那契进制数标准式
- death
- The Apple Watch Face That Counts Down to Your Death
- Gst-0.10 released and it's good time to be sentenced to death.
- Death Note
- Moving to Linux: Kiss the Blue Screen of Death Goodbye!读书笔记1
- Moving to Linux, Second Edition: Kiss the Blue Screen of Death Goodbye! (2nd Edition)
- Windows 7 64位版本的内存错误导致蓝屏死机(Blue Screen to Death)
- convert decimal to binary
- Decimal to Binary
- binary tree to list
- mdv to binary
- 5.2 Binary to String
- Binary to Decimal
- 省赛 Binary to Prime
- convert decimal number to binary
- 如何判断一个js对象是否一个DOM对象
- <设计模式可复用面向对象软件的基础> [1.3]、工厂方法(C#)
- pycharm修改快捷键
- Python 模块大全(很详细!)
- 子串的概率
- Death to Binary?
- 封装okhttp 简化
- 2017/10/13工作总结
- closure(闭包)、仿函数、std::function、bind、lambda
- [LeetCode]231. Power of Two-判断一个整数是否是2的幂次方
- IP地址
- okhttp的工作原理以及缓存机制
- chromiumFX 的使用
- 微信公众平台开发(83) 生成带参数二维码