Fibinary Numbers
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30506#problem/V
题意:从右向左,每一个位数,分别表示一个fibonacci数,1表示有,0表示没有;求两个数的和,同样按照这种形式存储
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<ctime>
#include<deque>
#include<stack>
#include<bitset>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<numeric>
#include<sstream>
#include<utility>
#include<iostream>
#include<algorithm>
#include<functional> using namespace std ;
const int maxn = 110 ;
char f1[ maxn + 10 ] , f2[ maxn + 10] , f[ maxn + 10 ] ; void Swap( char a[ ] )
{
int len = strlen( a ) ;
char temp ;
for( int i = 0 ; i < len / 2 ; ++ i )
{
temp = a[ i ] ;
a[ i ] = a[ len - i - 1 ] ;
a[ len - i - 1 ] = temp ;
}
} int main()
{
int Case = 0 ;
while( scanf( "%s%s" , f1 , f2 ) != EOF )
{
int len1 = strlen( f1 ) ;
int len2 = strlen( f2 ) ;
int len = len1 > len2 ? len1 : len2 ;
Swap( f1 ) ;
Swap( f2 ) ;
Case++ ;
if( Case != 1 )
{
printf( "\n" ) ;
}
memset( f , '0' , sizeof( f ) ) ;
for( int i = len1 ; i < maxn + 10 ; ++i )
{
f1[ i ] = '0' ;
}
for( int j = len2 ; j < maxn + 10; ++j )
{
f2[ j ] = '0' ;
}
for( int i = 0 ; i < len ; ++i )
{
f[ i ] = f1[ i ] + f2[ i ] - '0' ;
}
for( int i = 0 ; i < maxn ; ++i )
{
if( f[ i ] == '2' )
{
f[ i + 1 ] ++ ;
f[ i ] = '0' ;
if( i == 1 )
{
f[ i - 1 ]++ ;
i -= 2 ;
}
else if( i > 1 )
{
f[ i - 2 ]++ ;
i -= 3 ;
}
}
}
for( int i = maxn ; i >= 1 ; --i )
{
if( f[ i ] == '1' && f[ i - 1 ] == '1' )
{
f[ i ] = '0' ;
f[ i - 1 ] = '0' ;
f[ ++i ]++ ;
i += 2 ;
}
}
int i ;
for( i = maxn ; i >= 0 && f[ i ] == '0' ; --i );
{
if( i == -1 )
{
printf( "0" ) ;
}
else
{
for( ; i >= 0 ; -- i )
{
printf( "%c" , f[ i ] ) ;
} }
printf( "\n" ) ;
}
}
return 0;
}
Fibinary Numbers的更多相关文章
- UVA 763 Fibinary Numbers
题意讲某个二进制按照规则每一位对应斐波那契数生成新的数字,然后2个数字求和.再求由该规则生成的二进制串.并且要求尽量用更大项的fib数(题目提示不能由连续的1就是2个连续的1(11)不如100更优) ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
随机推荐
- Android:源码环境下移植第三方的apk内置到ROM(System Image)中
1. 首先在vendor目录下新建一个the3rdapk的目录,将需要内置的apk丢进去,目录名自己随意定. 2. 在 build/target/product/common.mk最后面,在$(cal ...
- OpenStack导入镜像后Launch不起来的几个问题
Dashboard上显示state为error 没有其他报错 用nova list找到虚拟机的id 然后nova show,可以得到fault详细信息 也可以查看下面两个log /var/log/no ...
- html里面自定义弹出窗口
网页上默认的提示框或对话框一般比较丑,可以利用div遮盖层来自定义对话框 1.定义一个按钮或者链接(项目里面是通过点击一个图片) <img src="images/zz.gif&quo ...
- ThinkPHP的验证码刷新显示和验证码显示不出来的原因
1.应当这样<imp src='验证码路径' onclick="this.src='验证码路径?'+Math.random()">;如果后面不加Math.random( ...
- IOS7 适配以及向下兼容问题
1.所有的UIViewController加如下方法. - (void) viewDidLayoutSubviews { if ([[[UIDevice currentDevi ...
- CentOS Linux使用crontab运行定时任务详解
参考博文: (总结)CentOS Linux使用crontab运行定时任务详解
- Spring+EhCache缓存实例(详细讲解+源码下载)(转)
一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...
- zzuli Camellia的难题(暴力)
1784: Camellia的难题 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 67 Solved: 14SubmitStatusWeb Boar ...
- HDU SPFA算法 Invitation Cards
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1535 分析: 题意:求1点到其它点的最短距离之和+其它点到1点的最短距离之和 前面一部分直接用SPFA ...
- 当webview遇到了Slidingmenu,webView出现卡白,解决方案
先介绍一下什么是SlidingMenu: Sliding Menu的是一种比较新的设置界面或配置界面效果,在主界面左滑或者右滑出现设置界面,能方便的进行各种操作.如Evernote.Google+.F ...