poj 2116 Death to Binary? 模拟
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1707 | Accepted: 529 |
Description
For example 1101001Fib = F0 + F3 + F5 + F6 = 1 + 5 + 13 + 21 = 40.
You may observe that every integer can be expressed in this base,
but not necessarily in a unique way - for example 40 can be also
expressed as 10001001Fib. However, for any integer there is a
unique representation that does not contain two adjacent digits 1 - we
call this representation canonical. For example 10001001Fib is a canonical Fibonacci representation of 40.
To prove that this representation of numbers is superior to the
others, ACM have decided to create a computer that will compute in
Fibonacci base. Your task is to create a program that takes two numbers
in Fibonacci base (not necessarily in the canonical representation) and
adds them together.
Input
input consists of several instances, each of them consisting of a single
line. Each line of the input contains two numbers X and Y in Fibonacci
base separated by a single space. Each of the numbers has at most 40
digits. The end of input is not marked in any special way.
Output
The first line contains the number X in the canonical
representation, possibly padded from left by spaces. The second line
starts with a plus sign followed by the number Y in the canonical
representation, possibly padded from left by spaces. The third line
starts by two spaces followed by a string of minus signs of the same
length as the result of the addition. The fourth line starts by two
spaces immediately followed by the canonical representation of X + Y.
Both X and Y are padded from left by spaces so that the least
significant digits of X, Y and X + Y are in the same column of the
output. The output for each instance is followed by an empty line.
Sample Input
11101 1101
1 1
Sample Output
100101
+ 10001
-------
1001000 1
+ 1
--
10
Source
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define ll long long
#define esp 1e-13
const int N=1e4+,M=1e6+,inf=1e9+,mod=;
string s1,s2,s3;
ll a[N];
void init()
{
a[]=;
a[]=;
for(int i=;i<=;i++)
a[i]=a[i-]+a[i-];
}
ll getnum(string aa)
{
int x=aa.size();
ll sum=;
for(int i=;i<x;i++)
if(aa[i]=='')
sum+=a[i];
return sum;
}
void check(ll x,string &str)
{
int i;
for(i=;i>=;i--)
if(x>=a[i])
break;
for(int t=i;t>=;t--)
if(x>=a[t])
{
str+='';
x-=a[t];
}
else
str+='';
if(i<)
str+='';
}
int main()
{
int x,y,i,z,t;
init();
while(cin>>s1>>s2)
{ reverse(s1.begin(),s1.end());
reverse(s2.begin(),s2.end());
ll num1=getnum(s1);
ll num2=getnum(s2);
ll num3=num1+num2;
s1.clear();
s2.clear();
s3.clear();
check(num1,s1);
check(num2,s2);
check(num3,s3);
printf(" ");for(i=;i<s3.size()-s1.size();i++)printf(" ");cout<<s1<<endl;
printf("+ ");for(i=;i<s3.size()-s2.size();i++)printf(" ");cout<<s2<<endl;
printf(" ");for(i=;i<s3.size();i++)printf("-");cout<<endl;
printf(" ");cout<<s3<<endl;
cout<<endl;
}
return ;
}
poj 2116 Death to Binary? 模拟的更多相关文章
- Death to Binary? 分析模拟
/** 题目:Death to Binary? 链接:https://vjudge.net/contest/154246#problem/T 题意:略. 思路: 注意事项: 给的字符串存在前导0: 存 ...
- POJ2116 Death to Binary?
/* POJ2116 Death to Binary? http://poj.org/problem?id=2116 齐肯多夫定理 */ #include <cstdio> #includ ...
- Death to Binary? (模拟)题解
思路: 除去前导0,注意两个1不能相邻(11->100),注意 0 *** 或者*** 0或者0 0情况 用string的reverse()很舒服 代码: #include<cstdio& ...
- poj 1008:Maya Calendar(模拟题,玛雅日历转换)
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64795 Accepted: 19978 D ...
- POJ 1027 The Same Game(模拟)
题目链接 题意 : 一个10×15的格子,有三种颜色的球,颜色相同且在同一片内的球叫做cluster(具体解释就是,两个球颜色相同且一个球可以通过上下左右到达另一个球,则这两个球属于同一个cluste ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- poj 2632 Crashing Robots(模拟)
链接:poj 2632 题意:在n*m的房间有num个机器,它们的坐标和方向已知,现给定一些指令及机器k运行的次数, L代表机器方向向左旋转90°,R代表机器方向向右旋转90°,F表示前进,每次前进一 ...
- poj 1028 Web Navigation(模拟)
题目链接:http://poj.org/problem? id=1028 Description Standard web browsers contain features to move back ...
- POJ 3087 Shuffle'm Up (模拟+map)
题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块 ...
随机推荐
- 【BZOJ4825】[Hnoi2017]单旋 线段树+set
[BZOJ4825][Hnoi2017]单旋 Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能 ...
- EasyNVR浏览器无插件直播在Linux系统下将录像文件与EasyNVR可执行文件分离运行的方案
问题背景 在工控机上运行EasyNVR,WEB访问出现设备在线,但是视频没有快照和无法正常直播: 问题原因分析 通过上工控机发现是由于磁盘空间被占满导致的软件运行收到影响. 解决问题分析 由于录像文件 ...
- 记一次服务器inodes数报警的事件
# df -i 执行以上命令,发现/上的 inodes 占用率为81%,于是开始处理. 首先找出哪个目录底下文件数最多: # cd / # for i in $(ls);do echo ${i} &a ...
- Java 自带的加密类MessageDigest类(加密MD5和SHA)
Java 自带的加密类MessageDigest类(加密MD5和SHA) - X-rapido的专栏 - CSDN博客 https://blog.csdn.net/xiaokui_wingfly/ar ...
- JSP页面获取下来框select选中项的值和文本的方法
<select id="username" name=""> <option value="1">jyy< ...
- C 和 C++ 的标准库分别有自己的 locale 操作方法,C 标准库的 locale 设定函数是 setlocale(),而 C++ 标准库有 locale 类和流对象的 imbue() 方法(gcc使用zh_CN.GBK,或者zh_CN.UTF-8,VC++使用Chinese_People's Republic of China.936或者65001.)
转自:http://zyxhome.org/wp/cc-prog-lang/c-stdlib-setlocale-usage-note/ [在此向原文作者说声谢谢!若有读者看到文章转载时请写该转载地址 ...
- if控制器+循环控制器+计数器,控制接口分支
但是我不想这么做,接口只想写一次,让循环控制器和if控制器去判断接口,执行我想要的分支.这里遇到了一个问题,if控制器通过什么去判断接下来的分支?我引入了一个计数器的概念.起始值为0,每次循环加1,将 ...
- 洗牌算法Fisher-Yates以及C语言随机数的产生
前些天在蘑菇街的面试中碰到一道洗牌的算法题,拿出来和大家分享一下! 原题是:54张有序的牌,如何无序的发给3个人? 这个题是运用经典的洗牌算法完成.首先介绍一种经典的洗牌算法--Fisher-Yate ...
- Python实现下载进度
# encoding:utf-8 import urllib import os def Schedule(a,b,c): ''''' a:已经下载的数据块 b:数据块的大小 c:远程文件的大小 '' ...
- 024-Spring Boot 应用的打包和部署
一.概述 二.手工打包[不推荐] 打包命令:maven clean package 打包并导出依赖:maven clean package dependency:copy-dependencies 1 ...