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 ...
随机推荐
- POJ 1692 Crossed Matchings(DP)
Description There are two rows of positive integer numbers. We can draw one line segment between any ...
- GitHub学习笔记
安装 Ubuntu上安装Git sudo apt-get install git Windows上安装Git msysgit是Windows版的Git.从http://msysgit.github.i ...
- linux进程解析--进程切换
为了控制进程的执行,linux内核必须有能力挂起正在cpu上运行的进程,换入想要切换的进程,也就是恢复以前某个挂起的进程,这就是linux的进程切换. 1进程切换的时机 一般来说,进程切换都是发生在 ...
- Android之TextView------LINK的点击事件
package com.TextHtml; import android.app.Activity; import android.content.Context; import android.os ...
- Git简介及安装和简单配置
首先需要清楚的是Git和GitHub的区别. Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了 ...
- centos6.5 mysql安装+远程访问+备份恢复+基本操作+卸载
参考博文: Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置 MySQL修改root密码的多种方法 MySQL的备份与还原 解决mysql导入还原时乱码的问题 ...
- MessageBox不能前置显示的问题
在MFC的开发中,经常会遇到一些莫名奇妙的问题,可能是经验不足的原因吧. 进入正题....在手头的项目中,用MFC做的界面应用.在某一天突然发现程序界面不能进行响应,经过反复的调试后发现:Messag ...
- Java--日期的使用
Date 类: 最基础的日期时间类,返回一个相对日期的毫秒数.精确到毫秒,但不支持日期的国际化和分时区显示. Calender类: 相对于Date更加强大的时间类,是抽象类,提供了常规的日期修改功能和 ...
- 在windows下安装mysql5.6.24版本
链接地址:http://jingyan.baidu.com/article/90bc8fc8615c99f653640cc4.html 工具/原料 官网下载安装包 系统需装net 4.0及以上运行库 ...
- iphone开发,模拟器生成文件路径
原文链接:http://blog.csdn.net/gf771115/article/details/7722023 Finder---位置----apple(用户名)---application s ...