SPOJ #429 Simple Numbers Conversion
This is simply a human work simulation - exactly reproducing how you do it by hand. Nothing special. You'd better put each step on a paper to make everything clear. Reference: http://blog.csdn.net/rappy/article/details/1737671
My code passed all 6 test cases. Yay..
// #include <iostream>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std; char GetDigitChar(int val)
{
if(val >= && val <= ) return val + '';
else if(val >= && val < ) return val - + 'A';
return '\0';
} int GetDigitVal(char c)
{
c = toupper(c);
if( c >= '' && c <= '') return c - '';
else if(c >= 'A' && c <= 'Z') return c - 'A' + ;
return ;
} void num_conv(string &str, int r, int s)
{
int strLen = str.length(); // Convert string to val first
vector<int> origVal;
origVal.reserve(strLen);
for(int i = ; i < strLen; i ++)
{
origVal.push_back(GetDigitVal(str[i]));
} // Go
vector<char> ret; int currStartInx = ;
bool bAllDone = false;
while(currStartInx < strLen && !bAllDone)
{
// cout << "Start from " << currStartInx << endl;
for(int i = currStartInx; i < strLen;i++)
{
// cout << "\t Curr Digit: " << origVal[i] << endl;
int quo = origVal[i] / s;
int rem = origVal[i] % s;
// cout << "\t Quo: " << quo << " Rem: " << rem << endl; origVal[i] = quo;
// The digit to record
if(i == strLen - )
{
ret.push_back(GetDigitChar(rem));
// cout << "!" << GetDigitChar(rem) << endl;
bAllDone = (currStartInx == (strLen - ) && quo == )? true: false;
break;
} // Add remainer to next digit
if(rem > )
{
// cout << "\tAdding rem " << r * rem << " = ";
origVal[i+] += r * rem;
// cout << origVal[i+1] << endl;
} if(i == currStartInx)
{
currStartInx += quo == ? : ;
}
}// for
}// while // Output
for(int i = ret.size() - ; i >=; i --)
{
cout << ret[i];
}
cout << endl;
} int main()
{
int runcnt = ;
cin >> runcnt;
while(runcnt--)
{
string strnum;
int r, s;
cin >> strnum >> r >> s;
num_conv(strnum, r, s);
}
return ;
}
SPOJ #429 Simple Numbers Conversion的更多相关文章
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- SPOJ BALNUM Balanced Numbers (数位dp)
题目:http://www.spoj.com/problems/BALNUM/en/ 题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数. 分析: 明显的数位dp题, ...
- SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)
Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...
- SPOJ - BALNUM - Balanced Numbers(数位DP)
链接: https://vjudge.net/problem/SPOJ-BALNUM 题意: Balanced numbers have been used by mathematicians for ...
- spoj 10606 Balanced Numbers 数位dp
题目链接 一个数称为平衡数, 满足他各个数位里面的数, 奇数出现偶数次, 偶数出现奇数次, 求一个范围内的平衡数个数. 用三进制压缩, 一个数没有出现用0表示, 出现奇数次用1表示, 出现偶数次用2表 ...
- [创意标题] spoj 11354 Amusing numbers
意甲冠军: 给k(1<=k<=10^15),先询问k 大只包含数字5和6的数目是多少 实例 1那是,5 ,3那是,55 .4那是,56 思考: 首先,我们可以找到.有许多2这是头号,有两个 ...
- UVa 10473 - Simple Base Conversion
题目大意:十进制与十六进制之间的相互转换. #include <cstdio> int main() { #ifdef LOCAL freopen("in", &quo ...
- [数位dp] spoj 10738 Ra-One Numbers
题意:给定x.y.为[x,y]之间有多少个数的偶数位和减去奇数位和等于一. 个位是第一位. 样例: 10=1-0=1 所以10是这种数 思路:数位dp[i][sum][ok] i位和为sum 是否含有 ...
- SPOJ BALNUM Balanced Numbers(数位DP+状态压缩)题解
思路: 把0~9的状态用3进制表示,数据量3^10 代码: #include<cstdio> #include<map> #include<set> #includ ...
随机推荐
- 文件IO操作
前言 本文介绍使用java进行简单的文件IO操作. 操作步骤 - 读文件 1. 定义一个Scanner对象 2. 调用该对象的input函数族进行文件读取 (参见下面代码) 3. 关闭输入流 说明:其 ...
- Install Oracle Java JDK/JRE 7u55 on Fedora 20/19, CentOS/RHEL 6.5/5.10
What’s new in Sun/Oracle Java 7 VM Compressed 64-bit object pointers Garbage-First GC (G1) JSR 292: ...
- CSS+DIV常用命名
常用的符合CSS+DIV规则的命名 页头:header 登录条:loginBar 标志:logo 侧栏:sideBar 广告:banner 导航:nav 子导航:subNav 菜单:menu 子菜单: ...
- C#各类型大小
- 前后台Json的转换
jsp:JSON.stringify(params):params:表示数组 servlet:Store store = (Store) JSONObject.toBean(JSONObject.fr ...
- mysql优化参数thread_cache_size
thread_cache_size功能在mysql数据库配置文件中是非常重要的一项功能了,如果对thread_cache_size优化做得好我们可以让服务器跑得非常快,设置不好就会发现很小访问量就非常 ...
- CentOS配置LAMP环境
环境:CentOS 6.5 配置防火墙,开启80端口.3306端口 # Firewall configuration written by system-config-firewall # Manua ...
- jQuery之Deferred对象的使用
详见:http://www.imooc.com/code/8907 JavaScript的执行流程是分为"同步"与"异步" 传统的异步操作会在操作完成之后,使用 ...
- springMvc源码学习之:利用springMVC随时随地获取HttpServletRequest等对象
一.准备工作: 在web.xml中添加 <listener> <listener-class> org.springframework.web.context.request. ...
- POI Workbook接口和HSSFWorkbook对象和XSSFWorkbook对象操作相应excel版本
由于HSSFWorkbook只能操作excel2003一下版本,XSSFWorkbook只能操作excel2007以上版本,所以利用Workbook接口创建对应的对象操作excel来处理兼容性 @Te ...