思路:

除去前导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 内置功能 触发器介绍

    使用触发器可以在用户对表进行[增.删.改]操作时前后定义一些操作,注意:没有查询 创建触发器 create trigger 触发器的名字 之前(before)或者之后(after)  行为(inser ...

  2. Knight Moves(hdu1372 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)   ...

  3. jquery closest & parent比较

    .closest() .parents() 从当前元素开始 从父元素开始 沿 DOM 树向上遍历,直到找到已应用选择器的一个匹配为止. 沿 DOM 树向上遍历,直到文档的根元素为止,将每个祖先元素添加 ...

  4. 蒙特卡洛模拟(Monte Carlo simulation)

    1.蒙特卡罗模拟简介 蒙特卡罗模拟,也叫统计模拟,这个术语是二战时期美国物理学家Metropolis执行曼哈顿计划的过程中提出来的,其基本思想很早以前就被人们所发现和利用.早在17世纪,人们就知道用事 ...

  5. Groovy介绍

    关于 Groovy 这一节将学习 Groovy 的基础知识:它是什么,它与 Java 语言和 JVM 的关系,以及编写 Groovy 代码的一些要点. 一.什么是 Groovy? Groovy 是 J ...

  6. Integer类之equals与hashCode

    一.源码. 1.equals源码.可以看出,比较的是两者的类型相同且内容相同.即是否有相等的int类型的value值. ######################################## ...

  7. 33. Search in Rotated Sorted Array(二分查找)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  8. redis和memcached相关

    应该选择哪一种缓存机制 redis相较于memcached更加年轻,功能更加强大. 对小型静态数据进行缓存处理,最具代表性的例子就是HTML代码片段.使用memcached所消耗内存更少. 其他情况下 ...

  9. nginx之rewrite重写,反向代理,负载均衡

    rewrite重写(伪静态): 在地址栏输入xx.com/user-xxx.html, 实际上访问的就是xxx.com/user.php?id=xxx rewrite就这么简单 附上ecshop re ...

  10. HDU 4770

    这题说的是一在一个N*M的房间内,然后有些房间不能被灯光照亮,有一个灯可以转动方向,其他的灯只能在固定一个方向上,因为数据比较小,所以比较水,直接暴力的进行枚举就好了,但是还是 wa了很久,原因没认真 ...