POJ 1894 Alternative Scale of Notation
来源:互联网 发布:网络教育学费多少 编辑:程序博客网 时间:2024/06/03 20:21
给一个Base,一个大整数(100位),求这个大整数的Base进制的表示,其中,表示中不能出现0,但是可以出现Base。
如,60 = 1 * 3^3 + 3 * 3^2 + 1 * 3^1 + 3 * 3^0,所以,60(3) = 1313。
思路:
依然是,让大整数不断地对Base取模得到低位,再对Base做整数除法,不一样的是如果得到的模是0,那么低位会是Base,而不是0。
涉及到高精度减法,高精度除法,高精度取模。
code:
#include <cstdio>#include <cstring>#define N 105int b, ans[4*N], cnt;char data[N], tmp[N];int mod(){int ret = 0, len = strlen(data);for (int i = 0; i < len; ++ i)ret = (ret*10 + data[i]-'0') % b;return ret % b;};bool judge(){int len = strlen(data);if (data[0] != '1') return false;for (int i = 1; i < len-1; ++ i)if (data[i] != '0') return false;return true;}void sub(int x){int len = strlen(data);if (data[len-1]-'0' >= x)data[len-1] -= x;else if (judge()){for (int i = 0; i < len-2; ++ i)data[i] = '9';data[len-2] = data[len-1]+10-x;data[len-1] = '\0';}else{int y = len-2;data[len-1] += 10-x;while (data[y] == '0'){data[y] = '9';y --;}data[y] --;}}void div(){int x = 0, len = strlen(data), ret = 0, i = 0;if (data[0]-'0' < b){ret += data[0]-'0';i ++;}for (; i < len; ++ i, ++ x){ret = ret*10 + data[i]-'0';tmp[x] = ret/b + '0';;ret %= b;}tmp[x] = '\0';memcpy(data, tmp, sizeof(data));}int main(){scanf("%d", &b);scanf("%s", data);if (data[0] == '0') printf("\n");else{cnt = 0;while (true){ans[cnt] = mod();if (ans[cnt] == 0) ans[cnt] = b;sub(ans[cnt]);div();cnt ++;if (data[0] == '0' || strlen(data) == 0) break;}for (int i = cnt-1; i >= 0; -- i)printf("%d", ans[i]);printf("\n");}}
- POJ 1894 Alternative Scale of Notation
- POJ 1894 Alternative Scale of Notation
- POJ 1894 Alternative Scale of Notation .
- Alternative Scale of Notation(大数java)
- Storage of Scale and Precision
- POJ 1588 Reverse Roman Notation
- poj 1588(Reverse Roman Notation)
- POJ 3139 Balancing the Scale
- Scale-out of the Performance of SignalR
- The notes of Algorithms ---- Asymptotic Notation
- large-scale analysis of malware downloaders
- Visualization of Large-Scale Distributed Data
- POJ 1588 Reverse Roman Notation 笔记
- POJ 3139 Balancing the Scale 笔记
- Scale
- Scale
- Scale
- The origin of "Hungarian" notation for naming variables
- PowerDesigner连接MySQL,建立逆向工程图解
- 最短路—1008
- 基于WCF 的远程数据库服务访问技术
- VS2010中的调试技巧
- <welcome-file-list>404
- POJ 1894 Alternative Scale of Notation
- C++ dlopen mini HOWTO 中译版
- hdu1789
- POJ 1896 Code Formatting
- WCF数据库开发一般分下面几步:
- Ant + ivy的安装
- 收藏的网址
- Openwrt python,openwrt上使用Python
- POJ 1899 Farmer Bill's Problem