思路:

除去前导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. Python开发【笔记】:什么是RESTful框架

    RESTful框架 前言:  一句话总结:用URL定位资源,用HTTP描述操作 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式 ...

  2. ARP报文

    硬件类型:指明了发送方想知道的硬件接口类型,以太网的值为1: 协议类型:指明了发送方提供的高层协议类型,IP为0x0800(16进制): 硬件地址长度和协议长度:指明了硬件地址和高层协议地址的长度,这 ...

  3. python 全局变量引用与修改

    一.引用 使用到的全局变量只是作为引用,不在函数中修改它的值的话,不需要加global关键字.如: #! /usr/bin/python a = 1 b = [2, 3] def func(): if ...

  4. Java——文件操作字符流和字节流的区别

    转:http://blog.csdn.net/joephoenix/articles/2283165.aspx java的IO流分两种流 字节流 InputStream OutputStream 字符 ...

  5. 【Python】【亲测好用】安装第三方包报错:AttributeError:'module' object has no attribute 'main'

    安装/卸载第三包可能出现如下问题及相应解决办法: 在pycharm编辑中,使用anconda2更新.卸载第三方包时,出现如下错误: AttributeError:'module' object has ...

  6. OLTP与OLAP

    当今的数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical Processing ...

  7. 总结)Nginx/LVS/HAProxy负载均衡软件的优缺点详解

    总结)Nginx/LVS/HAProxy负载均衡软件的优缺点详解 PS:Nginx/LVS/HAProxy是目前使用最广泛的三种负载均衡软件,本人都在多个项目中实施过,参考了一些资料,结合自己的一些使 ...

  8. [LeetCode] questions conclustion_BFS, DFS

    BFS, DFS 的题目总结. Directed graph: Directed Graph Loop detection and if not have, path to print all pat ...

  9. 梯度消失与梯度爆炸 ==> 如何选择随机初始权重

    梯度消失与梯度爆炸 当训练神经网络时,导数或坡度有时会变得非常大或非常小,甚至以指数方式变小,这加大了训练的难度 这里忽略了常数项b.为了让z不会过大或者过小,思路是让w与n有关,且n越大,w应该越小 ...

  10. Twitter OA prepare: Flipping a bit

    You are given a binary array with N elements: d[0], d[1], ... d[N - 1]. You can perform AT MOST one ...