思路:

除去前导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. 设置elasticsearch一次最大数量查询

    PUT my_index/_settings?preserve_existing=true{ "max_result_window": "2000000000" ...

  2. 【Pyton】【小甲鱼】类和对象

    一.类 定义一个类,例子如下: class Turtle: #定义一个名为Turtle的类,Python中类型约定以大写字母开头 #属性 color='green' weight=10 legs=4 ...

  3. Linux文件目录介绍及文件颜色区别

    文件颜色代表含义: 蓝色表示目录: 绿色表示可执行文件: 红色表示压缩文件: 浅蓝色表示链接文件: 白色表示其他文件: 黄色是设备文件,包括block, char, fifo. 常见目录解释 Linu ...

  4. python 基础 集合

    集合 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 交集 并集 差集 子集  对称差集 list ...

  5. 001-读书笔记-企业IT架构转型之道-阿里巴巴中台战略思想与架构实战-第一章 阿里巴巴集团中台战略引发的思考

    1.1.阿里中台发展 组件中台可能问题:组织间业务协作.业务核心能力的沉淀.组织KPI考核等 1.2.企业信息中心发展的症结 1.烟囱式系统建设模式 独立构建独立维护 缺点:1.重复功能建设和维护带来 ...

  6. openstack 部署笔记--neutron计算节点

    控制节点 # vim /etc/neutron/neutron.conf [DEFAULT] # ... transport_url = rabbit://openstack:root@control ...

  7. 从Maven仓库中导出jar包

    从Maven仓库中导出jar包:进入工程pom.xml 所在的目录下,输入以下命令:mvn dependency:copy-dependencies -DoutputDirectory=lib更简单的 ...

  8. [LeetCode] 261. Graph Valid Tree _ Medium tag: BFS

    Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), w ...

  9. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  10. jxl(Java Excel API) 使用方法 【1】

    //   构建Workbook对象, 只读Workbook对象  //   直接从本地文档创建Workbook  //   从输入流创建Workbook Workbook workbook = nul ...