思路:

除去前导0,注意两个1不能相邻(11->100),注意 0 *** 或者*** 0或者0 0情况

用string的reverse()很舒服

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
const int N=50;
const int MAX=45;
const int MOD=1000;
using namespace std;
ll fib[N];
void initfib(){
fib[0]=1;fib[1]=2;
for(int i=2;i<=MAX;i++){
fib[i]=fib[i-1]+fib[i-2];
}
}
void change(ll sum,string &c){
c.clear();
int j;
for(j=MAX;j>=0;j--){
if(sum>=fib[j]) break;
}
for(int i=j;i>=0;i--){
if(sum>=fib[i]){
sum-=fib[i];
c+='1';
}
else c+='0';
}
if(c.length()==0) c='0';
}
int main(){
initfib();
string a,b,c;
ll A,B,sum;
while(cin>>a>>b){
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
A=B=0;
for(int i=0;i<a.length();i++){
if(a[i]=='1') A+=fib[i];
}
ll cut=0,n;
n=a.length()-1;
while(1){
if(a[n]=='1') break;
if(a[n]=='0') cut++;
n--;
}
a=a.substr(0,a.length()-cut); //去掉a前导0
reverse(a.begin(),a.end()); for(int i=0;i<b.length();i++){
if(b[i]=='1') B+=fib[i];
}
cut=0;
n=b.length()-1;
while(1){
if(b[n]=='1') break;
if(b[n]=='0') cut++;
n--;
}
b=b.substr(0,b.length()-cut); //去掉b前导0
reverse(b.begin(),b.end()); sum=A+B;
change(A,a);
change(B,b);
change(sum,c); int mxlen=max(a.length(),max(b.length(),c.length()));
cout<<" ";
for(int i=0;i<mxlen-a.length();i++) cout<<" ";
cout<<a<<endl;
cout<<"+ ";
for(int i=0;i<mxlen-b.length();i++) cout<<" ";
cout<<b<<endl;
cout<<" ";
for(int i=0;i<mxlen;i++) cout<<"-";
cout<<endl;
cout<<" ";
for(int i=0;i<mxlen-c.length();i++) cout<<" ";
cout<<c<<endl<<endl;
}
return 0;
}

Death to Binary? (模拟)题解的更多相关文章

  1. poj 2116 Death to Binary? 模拟

    Death to Binary? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1707   Accepted: 529 D ...

  2. Death to Binary? 分析模拟

    /** 题目:Death to Binary? 链接:https://vjudge.net/contest/154246#problem/T 题意:略. 思路: 注意事项: 给的字符串存在前导0: 存 ...

  3. HGOI20181029模拟题解

    HGOI20181029模拟题解 /* sxn让我一定要谴责一下出题人和他的数据! */ problem: 给出十进制数a,b,然后令(R)10=(a)10*(b)10,给出c表示一个k进制数(1&l ...

  4. POJ2116 Death to Binary?

    /* POJ2116 Death to Binary? http://poj.org/problem?id=2116 齐肯多夫定理 */ #include <cstdio> #includ ...

  5. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  6. 【 P3952】 时间复杂度 大模拟题解

    题目链接 完全模拟 1.模拟结果 当我们的模拟程序执行结束时,直接执行模拟结果函数,用于比对我们的结果和数据给出的结果. bool yes(char a[],char b[]) { ;i<=;+ ...

  7. 【CF1436C】Binary Search 题解

    原题链接 题意简介 要求有多少种 n 的排列,能够通过二分法正确地找到放在 pos 处的数字 x. 答案对 1e9+7 取模.n<=1000. 采用的二分法如下图: 思路分析 首先,这个排列中有 ...

  8. HGOI20181031 模拟题解

    sol:第一题就DP?!然后写了O(n^2) dp再考虑优化!!!(尽量部分分带上!!!) 我写了正确的dp然后优化错了,具体的dp方法是考虑到对于右侧到左侧他是没有后效性的 所以定义f[i]为i及以 ...

  9. HGOI20181030 模拟题解

    problem:给定一个序列,问你能不能通过一次交换把他弄成有序 sol: 对于0%的数据,满足数列是一个排列,然后我就打了这档分(自己瞎造的!) 对于100%的数据,显然我们先对数列进行排序然后上下 ...

随机推荐

  1. mysql 内置功能 存储过程 创建有参存储过程

    对于存储过程,可以接收参数,其参数有三类: #in 仅用于传入参数用 #out 仅用于返回值用 #inout 既可以传入又可以当作返回值 传入参数使用格式 in 变量 数据类型 返回值使用格式 out ...

  2. c#按照指定长度切分字符串

    int pageSize=5; var array = new List<string>(); ----------方法1-------------------- var pageCoun ...

  3. POJ1014:Dividing(多重背包)

    http://poj.org/problem?id=1014 Description Marsha and Bill own a collection of marbles. They want to ...

  4. [LeetCode] 67. Add Binary_Easy tag: String

    Given two binary strings, return their sum (also a binary string). The input strings are both non-em ...

  5. scipy模块

  6. Vagrant配置虚拟机

    慕课上学习.需要安装 vagrant  VirtualBox .box文件和.iso文件一样都是镜像文件.可以在官网下载https://www.vagrantup.com/docs/ 点击boxs之后 ...

  7. STL学习笔记--特殊容器

    容器配接器 (1) stack 栈 后进先出(LIFO), 头文件#include<stack> template<class _Ty, class _Container = deq ...

  8. iOS UI基础-5.0 QQ框架(Storyboard)

    1.拉入TabBarController和4个Navigation 2.TabBarController关联Navigation 3.设置消息,拉入一个Button,设置背影 4.联系人,拉入一个Se ...

  9. memcache分布式布置方案

    利用hash算法 key->hash->取余%2 mod ->server=array()=>server[mod]算出是第几个memcache服务器

  10. 20154312 曾林 EXP7 网络欺诈防范

    目录 1.基础问题回答 ----1.1.通常在什么场景下容易受到DNS spoof攻击 ----1.2.在日常生活工作中如何防范以上两攻击方法 2.实践总结与体会 3.实践过程记录 ----3.1.简 ...