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 ...
随机推荐
- iOS 判断有无网络连接
众所周知,我们在开发APP时,涉及网络连接的时候,都会想着提前判断一下当前的网络连接状态,如果没有网络,就不再请求url,省去不必要的步骤,所以,这个如何判断?其实很简单. 前提:工程添加:Syste ...
- CodeForces 486C Palindrome Transformation 贪心+抽象问题本质
题目:戳我 题意:给定长度为n的字符串,给定初始光标位置p,支持4种操作,left,right移动光标指向,up,down,改变当前光标指向的字符,输出最少的操作使得字符串为回文. 分析:只关注字符串 ...
- CodeForces 519B A and B and Compilation Errors【模拟】
题目意思还是蛮简单的,看 输入数据输出数据还是比较明显的 我用排序来写还是可以AC的 //#pragma comment(linker, "/STACK:16777216") // ...
- HDU 3923 Invoker 【裸Polya 定理】
参考了http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 的模板 对于每一种染色,都有一个等价群,例如旋转, ...
- oracle中if/else
oracle中if/else功能的实现的3种写法 1.标准sql规范 一.单个IF 1. if a=... then ......... end if; 2. if a=... then .... ...
- 由基于qml,c++的串口调试工具浅谈qml与c++混合编程
最近在做一个基于sim900 的串口通信工具,基于qml和c++来实现. 首先,对于串口,qt有自带的QSerialPort,可以实现同步,和异步通信,qt creator也有自带的例子,本例子是从其 ...
- 数据库中操作XML(openXML)
最近公司项目需要在数据库中操作XML,因此系统的学习了一下 一.openxml的格式 OPENXML( idoc int [ in] , XPathnvarchar [ in ] , [ flags ...
- 删除: warning C4996: 'sprintf': This function or variable may be unsafe. Consider 方法
可以使用的最简单的方法: 选项Project | Configuration Properties | C/C++ | Preprocessor | Preproc ...
- [译]URL和URI的区别
译者:华科小涛:http://www.cnblogs.com/hust-ghtao/: 初学http协议,就被这两个相似的术语搞蒙了,查了很多资料,总算搞清楚了.(找资料还是英文啊,靠谱...). 本 ...
- 解析stm32的时钟
STM32 时钟系统 http://blog.chinaunix.net/uid-24219701-id-4081961.html STM32的时钟系统 *** http://www.cnblo ...