poj 1220 NUMBER BASE CONVERSION(短除法进制转换)
题目连接:1220 NUMBER BASE CONVERSION
题目大意:给出两个进制oldBase 和newBase, 以及以oldBase进制存在的数。要求将这个oldBase进制的数转换成newBase进制的数。
解题思路:短除法,只不过时直接利用了高精度的除法运算, 并且将以前默认的*10换成的*oldBase。
#include <stdio.h>
#include <string.h>
const int N = 1005;
int newBase, oldBase, n, cnt, num[N];
char str[N]; int getnum(char c) {
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'A' && c <= 'Z')
return c - 'A' + 10;
else
return c - 'a' + 36;
} char getchar(int c) {
if (c >= 0 && c <= 9)
return '0' + c;
else if (c >= 10 && c < 36)
return 'A' + c - 10;
else
return 'a' + c - 36;
} void change() {
memset(num, 0, sizeof(num));
n = strlen(str);
for (int i = 0; i < n; i++)
num[i] = getnum(str[i]);
} void solve() {
int flag = 1;
memset(str, 0, sizeof(str));
cnt = 0; while (flag) {
int t = 0, k;
flag = 0;
for (int i = 0; i < n; i++) {
k = num[i] + t * oldBase;
num[i] = k / newBase;
if (num[i]) flag = 1;
t = k % newBase;
}
str[cnt++] = getchar(t);
}
} int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d%d%s", &oldBase, &newBase, str); printf("%d %s\n", oldBase, str); change();
solve(); printf("%d ", newBase);
for (int i = cnt - 1; i >= 0; i--)
printf("%c", str[i]);
printf("\n");
if (cas) printf("\n");
}
return 0;
}
poj 1220 NUMBER BASE CONVERSION(短除法进制转换)的更多相关文章
- NUMBER BASE CONVERSION(进制转换)
Description Write a program to convert numbers in one base to numbers in a second base. There are 62 ...
- poj 1220 NUMBER BASE CONVERSION
NUMBER BASE CONVERSION Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5976 Accepted: ...
- POJ 1220 NUMBER BASE CONVERSION(较复杂的进制转换)
题目链接 题意 : 给你一个a进制的数串s,让你转化成b进制的输出. A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61,0到9还是原来的 ...
- (高精度运算4.7.26)POJ 1220 NUMBER BASE CONVERSION(高精度数的任意进制的转换——方法:ba1----->10进制----->ba2)
package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class POJ_1220_ ...
- PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) (进制转换,回文数)
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- POJ 3191 The Moronic Cowmpouter(进制转换)
题目链接 题意 : 将一个10进制整数转化为-2进制的数. 思路 :如果你将-2进制下的123转化为十进制是1*(-2)^2+2*(-2)^1+3*(-2)^0.所以十进制转化为-2进制就是一个逆过程 ...
- POJ 1220 大数字的进制转换,偷下懒,用java
题意为进制转换,Java的大数类就像是作弊 import java.math.BigInteger; import java.util.Scanner; public class Main { pub ...
- POJ 2305:Basic remains 进制转换
Basic remains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5221 Accepted: 2203 Des ...
- POJ 2635 The Embarrassed Cryptographer (千进制,素数筛,同余定理)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15767 A ...
随机推荐
- 读书与写论文的引导书——leo鉴书60
我是专科直接考的研究生.在论文写作方面基本能够算是初级.MBA毕业那会儿要写论文,在网上找了不少这方面的书,<论文与治学>是当中之中的一个. 这本那时为应景儿卖的书,成了我之后学习与工作的 ...
- Android原生APP内分享
Android原生APP内分享,可实现数据分享以及assets文件夹分享及私有文件分享 项目地址:https://github.com/json-pu/AndroidAppShare.git
- 【C语言】在两个数成对出现的数组中找到一个单独的数。
//在两个数成对出现的数组中找到一个单独的数.比如{1,2,3.3,1,4.2},即找出4 #include <stdio.h> int find(int arr[], int len) ...
- spring中bean的一些知识点
知识点1: 实例化bean的3种方法. 1. 使用类构造器 (90%用这个方法) 2. 使用静态工厂方法 3. 使用实例化工厂 知识点2: 看这段代码 Applicati ...
- spring mvc +cookie+拦截器功能 实现系统自动登陆
先看看我遇到的问题: @ResponseBody @RequestMapping("/logout") public Json logout(HttpSession session ...
- Kendo UI开发教程(23): 单页面应用(一)概述
Kendo单页面应用(Single-Page Application,缩写为SPA)定义了一组类用于简化Web应用(Rich Client)开发,最常见的单页面应用为Gmail应用,使用单页面可以给用 ...
- 论文阅读笔记 - Mesos: A Platform for Fine-Grained ResourceSharing in the Data Center
作者:刘旭晖 Raymond 转载请注明出处 Email:colorant at 163.com BLOG:http://blog.csdn.net/colorant/ 更多论文阅读笔记 http:/ ...
- 慎用MonthsBetweent和MonthSpan
●function MonthsBetween(const ANow, AThen: TDateTime): Integer; 描述: 根据两个给定的TDateTime类型的参数ANow和AThen, ...
- 通过崩溃地址找错误行数之Delphi版
通过崩溃地址找错误行数之Delphi版2009-5-11 17:42:35 来源: 转载 作者:网络 访问:360 次 被顶:2 次 字号:[大 中 小]核心提示:什么是 MAP 文件?简单地讲, M ...
- jquery用div模拟一个下拉列表框
原文 jquery用div模拟一个下拉列表框 今天分享一个用我自己用jquery写的,用div模拟下拉列表select,这个效果网上有很多,但是写一个有自己思路的代码效果,更有成就感,先看截图: 自我 ...