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 ...
随机推荐
- cocos2d-x3.6 连连看连通画线
我的博客:http://blog.csdn.net/dawn_moon 网上看到非常多人写的连连看,都没有画连线的实现.事实上要话连线挺简单的.cocos2d-x 提供了一个非常方便的绘图形的类.Dr ...
- Win7安装IIS
非常明显的,我们做系统是用来给人用的,所以这就涉及到对系统的公布问题.仅仅有公布之后.别人才干通过訪问你的IP和port号来訪问你的程序. 而系统公布一般都是在IIS上面进行系统公布,所以我们就必需要 ...
- iOS导航条渐变透明
来源:HelloYeah 链接:http://www.jianshu.com/p/b8b70afeda81 下面这个界面有没有觉得很眼熟.打开你手里的App仔细观察,你会发现很多都有实现这个功能.比如 ...
- linux 工具: Top
linux TOP命令各参数详解[转载] http://www.cnblogs.com/sbaicl/articles/2752068.html
- STM32使用串口1配合DMA接收不定长数据,减轻CPU载荷
STM32使用串口1配合DMA接收不定长数据,减轻CPU载荷 http://www.openedv.com/thread-63849-1-1.html 实现思路:采 用STM32F103的串口1,并配 ...
- ASP.NET MVC 以Stream 下载文件
1.0以Stream 下载文件 nl.fileid = Int32.Parse(id); //服务器上对应的id Stream stream = Lawsuit.DownLoad(nl);//服务器 ...
- Chapter 8 工厂方法模式
工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是说,工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行.你想要加功能,本来是改工厂类的,而现在是修改 ...
- Week2(9月19日):增加一个CodeFirst的例子来说明
Part I:提问 =========================== 1.上堂课中我们使用了()数据库,它是()可部署的,只需要将相应的()文件添加到应用程序的()文件夹,就可以使用了,该数据 ...
- 模拟Struts2的AOP实现
在Struts2中有拦截器的概念,通过它的拦截器可以拦截Action.Struts2的拦截器是通过AOP来实现的,在Spring也有类似的概念.下面的我们先来比较一下Struts2和Spring中AO ...
- Nutch 是一个开源Java 实现的搜索引擎
Nutch 是一个开源Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具.包括全文搜索和Web爬虫. Nutch的创始人是Doug Cutting,他同时也是Lucene.Hado ...