思路:

除去前导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. sql批量修改wordpress网站的文章发布状态

    wordpress批量导入文章的时候,有些文章的状态可能会缺失,例如“mis scheduled”.draft.future等几种状态,如何用sql批量修改wordpress网站的文章发布状态呢? 点 ...

  2. HTML方法

    HTTP 方法:GET 对比 POST 两种最常用的 HTTP 方法是:GET 和 POST. 什么是 HTTP ? 超文本传输协议(HTTP)的设计目的是保证客户端与服务器之间的通信. HTTP 的 ...

  3. PAT 1074 Reversing Linked List[链表][一般]

    1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...

  4. CentOS6.5安装RHadoop

    1.首先安装依赖包(各个节点都要安装) [root@Hadoop-NN-01 ~]$ yum install gcc-gfortran #否则报”configure: error: No F77 co ...

  5. 在Win7系统下, 使用VS2015 打开带有日文注释程序出现乱码的解决方案

    在Win7系统下, 使用VS2015 打开带有日文注释程序出现乱码的解决方案 下载: apploc.msi (下载地址:http://microsoft-applocale.software.info ...

  6. testng入门教程3用TestNG执行case的顺序

    本教程介绍了TestNG中执行程序的方法,这意味着该方法被称为第一和一个接着.下面是执行程序的TestNG测试API的方法的例子. 创建一个Java类文件名TestngAnnotation.java在 ...

  7. Redis演示及使用场景

    概述 Redis是一个开源的.使用C语言编写的.支持网络交互的.可基于内存也可持久化的Key-Value(字典, Remote Dictionary Server,远程字典服务器)数据库. 客户端:h ...

  8. .net 调用API并解析Json数据方法

    using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Syst ...

  9. web前端面试小结(1)

    两天大概面试了4家,有电面也有F2F,现将面试中的问题大概汇总下,一方面了解自己的不足,一方面用来勉励自己后面面试加油! 答案网上都有,就不一一写在这里了,后面有时间会把下面的问题分别拉出来详述. 1 ...

  10. Linux系统——MySQL基础(二)

    # MySQL数据库完全备份与恢复## 数据库备份的分类1. 从物理与逻辑的角度,备份可以分为物理备份和逻辑备份.(1)物理备份:对数据库操作系统的物理文件(数据文件.日志文件)的备份.物理备份又可分 ...