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 ...
随机推荐
- 织梦dedecms 用交叉栏目时arclist标签调用不出内容文章的问题(纯转载)
本文转自:http://www.cnblogs.com/cnteam/articles/4056702.html 最近用了交叉栏目发现当为手动指定交叉栏目ID时用arclist标签不能调出相关文章最后 ...
- python 的 class
虽然python OOP 是可选的,但是尽可能使用OOP, 可以让你的代码更精简,容易理解. python 中大多数OOP故事, 都可简化为这个表达式: object.attribute 类的建立使用 ...
- Highcharts ajax获取json对象动态生成报表生成 .
http://blog.csdn.net/wsk7860/article/details/8751061 最近做个项目,项目经理想做一个统计报表,在网上查看些资料就选用Highchars 这里和大家分 ...
- Android Bitmap OOM处理
public int calculateInSampleSize(BitmapFactory.Options option, int rWidth, int rHeight) { ...
- Java 抓取网页内容
前两天想写一段自动提取微博状态的代码.据我所知,实现这个功能即可以用PHP写,也可以用Java写.我认为用Java写调试方便一点,PHP的脚本还要上传到服务器什么的. 代码很简单的,新建一个java. ...
- Windows Phone使用总结(长期更新)
[感受和经历] 1,型号,撸妹640XL: 2,经历,去银行办卡,当然各种潜规则要我装APP,然后小妹夺我手机要帮我安装,拿过去之后又还给我了--哈哈哈哈,不过乐极生悲,我以为能成功躲过去了,发现中国 ...
- Understanding postgresql.conf : log*
After loooong pause, adding next (well, second) post to the “series“. This time, I'd like to describ ...
- Sklearn库例子3:分类——岭回归分类(Ridge Regression )例子
为了解决数据的特征比样本点还多的情况,统计学家引入了岭回归. 岭回归通过施加一个惩罚系数的大小解决了一些普通最小二乘的问题.回归系数最大限度地减少了一个惩罚的误差平方和. 这里是一个复杂的参数,用来控 ...
- CSRF(Cross-site request forgery)跨站请求伪造
CSRF 背景与介绍 CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,它在 2007 年曾被列为互联网 20 大安全隐患之一.其他安全隐患,比如 ...
- Partial RenderPartial Action RenderAction 区别和用法
区别: 1.Partial 与 RenderPartial 两个方法性质基本一样,只是把一个静态用户控件给嵌入进来. 2.Partial 回传一堆html代码,直接写进到页面上@Html.Partia ...