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的更多相关文章

  1. SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]

    题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...

  2. SPOJ BALNUM Balanced Numbers (数位dp)

    题目:http://www.spoj.com/problems/BALNUM/en/ 题意:找出区间[A, B]内所有奇数字出现次数为偶数,偶数字出现次数为计数的数的个数. 分析: 明显的数位dp题, ...

  3. SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)

    Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...

  4. SPOJ - BALNUM - Balanced Numbers(数位DP)

    链接: https://vjudge.net/problem/SPOJ-BALNUM 题意: Balanced numbers have been used by mathematicians for ...

  5. spoj 10606 Balanced Numbers 数位dp

    题目链接 一个数称为平衡数, 满足他各个数位里面的数, 奇数出现偶数次, 偶数出现奇数次, 求一个范围内的平衡数个数. 用三进制压缩, 一个数没有出现用0表示, 出现奇数次用1表示, 出现偶数次用2表 ...

  6. [创意标题] spoj 11354 Amusing numbers

    意甲冠军: 给k(1<=k<=10^15),先询问k 大只包含数字5和6的数目是多少 实例 1那是,5 ,3那是,55 .4那是,56 思考: 首先,我们可以找到.有许多2这是头号,有两个 ...

  7. UVa 10473 - Simple Base Conversion

    题目大意:十进制与十六进制之间的相互转换. #include <cstdio> int main() { #ifdef LOCAL freopen("in", &quo ...

  8. [数位dp] spoj 10738 Ra-One Numbers

    题意:给定x.y.为[x,y]之间有多少个数的偶数位和减去奇数位和等于一. 个位是第一位. 样例: 10=1-0=1 所以10是这种数 思路:数位dp[i][sum][ok] i位和为sum 是否含有 ...

  9. SPOJ BALNUM Balanced Numbers(数位DP+状态压缩)题解

    思路: 把0~9的状态用3进制表示,数据量3^10 代码: #include<cstdio> #include<map> #include<set> #includ ...

随机推荐

  1. 利用powerdesigner反向数据库结构,生成ER图

    参考月下狼~图腾~:<利用powerdesigner反向数据库结构,生成ER图> https://www.zybuluo.com/Jpz/note/123582 首先新建一个"P ...

  2. ZOJ 1024 Calendar Game

    原题链接 题目大意:2001年11月4日是一个黄道吉日,有两个人做游戏,看谁先数到这个日子.可以往后数一天,比如2号到3号,30号到31号,也可以往后数一个月.给出一个起始的日子,求能否保证先开始游戏 ...

  3. C++ Primer : 第十二章 : 动态内存之shared_ptr类

    在C++中,动态内存是的管理是通过一对运算符来完成的:new  ,在动态内存中为对象分配空间并返回一个指向该对象的指针,delete接受一个动态对象的指针,销毁该对象,并释放该对象关联的内存. 动态内 ...

  4. leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. poj1417 带权并查集+0/1背包

    题意:有一个岛上住着一些神和魔,并且已知神和魔的数量,现在已知神总是说真话,魔总是说假话,有 n 个询问,问某个神或魔(身份未知),问题是问某个是神还是魔,根据他们的回答,问是否能够确定哪些是神哪些是 ...

  6. Python 基础语法(四)

    Python 基础语法(四) --------------------------------------------接 Python 基础语法(三)------------------------- ...

  7. C++ 实用的小程序

    1. 打开test_ids.txt 将里面的东西添加"1_",然后另存为test_ids_repaired.txt #include <iostream> #inclu ...

  8. php安全的改进以及检测文件是否被篡改

    可以使用svn的check for modifications//查看更改 至于缓存的php模板文件 可以查找以下执行命令的函数是否存在phpinfo,eval,exec,passthru,shell ...

  9. mysql启动与关闭(手动与自动)

    手动管理mysql的启动与关闭 [root@mysql ~]# service mysql start --手动启动mysqlStarting MySQL. SUCCESS![root@mysql ~ ...

  10. Softmax回归(使用tensorflow)

    # coding:utf8 import numpy as np import cPickle import os import tensorflow as tf class SoftMax: def ...