icpc 银川 I. Base62 任意进制的转换 短除法
Problem Description
As we already know, base64 is a common binary-to-text encoding scheme. Here we define a special series of positional systems that represent numbers using a base (a.k.a. radix) of 2 to 62. The symbols ‘0’ – ‘9’ represent zero to nine, and ‘A’ – ‘Z’ represent ten to thirty-five,and ‘a’ – ‘z’ represent thirty-six to sixty-one. Now you need to convert some integer z in base x into base y.
Input
The input contains three integers x; y (2 ≤ x; y ≤ 62) and z (0 ≤ z < x^120), where the integer z is given in base x.
Output
Output the integer z in base x.
Sample Input
16 2 FB
Sample Output
11111011
Analysis of ideas
利用短除法 poj1220原题
Accepted code
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define cin(a) scanf("%d",&a)
#define ll long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 1100;
int getnum(char ch) //字符转数字
{
if(ch <= '9') return ch-'0';
else if(ch <= 'Z') return 10+ch-'A';
else return 36+ch-'a';
}
char getch(int num) //数字转字符
{
if(num <= 9) return num+'0';
else if(num <= 35) return num-10+'A';
else return num-36+'a';
}
int n,m;
char str1[maxn],str2[maxn];
int t[maxn],ans[maxn];
void solve()
{
int len = strlen(str1);
for(int i = 0; i < len; i++) //先把字符串变成数组,高位->低位
t[i] = getnum(str1[i]);
int j = 0,k = 0;
while(j < len)
{
for(int i = j; i < len-1; i++) //除以m,把余数加到下一位
{
t[i+1] += t[i] % m * n;
t[i] /= m;
}
ans[k++] = t[len-1]%m; //个位数余m
t[len-1] /= m;
while(j < len && !t[j]) j++; //最高位是0,j++
}
for(int i = 0; i < k; i++) //逆序变成字符串
{
str2[i] = getch(ans[k-i-1]);
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t;
cin(t);
while(t--)
{
mem(ans,0),mem(str2,0); //wa
cin>>n>>m;
cin>>str1;
solve();
cout<<n<<' '<<str1<<endl;
cout<<m<<' '<<str2<<endl<<endl;
}
}
icpc 银川 I. Base62 任意进制的转换 短除法的更多相关文章
- C#十进制与任意进制的转换
/// <summary> /// 将十进制转换为指定的进制 /// </summary> /// <param name="Val">十进制值 ...
- python的十进制与任意进制的转换
将任意进制转换成十进制 ", 8)) # 表示把8进制的54转换成十进制数并输出结果. # 8可以是2.8,10,16等进制数 将十进制转换成任意进制 def f(n,x): #n为待转换的 ...
- java 的任意进制间转换(很方便)
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = n ...
- java 的任意进制间转换
直接上代码: public class Main { public static void main(String[] args) { // TODO Auto-generated method st ...
- (高精度运算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_ ...
- C语言之任意进制的转换
我们都知道转换进制是一个让人比较头疼的事情,下面我的代码不是最好的,也就仅仅是一个思路而已,至少我认为使用栈来进行进制转换是比较合适的一种方法,好了,不多叙述了. #include<stdio. ...
- C#实现整型数据字任意编码任意进制的转换和逆转换
实现如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespa ...
- Python版任意进制转换
def decimalToAny(num,n): baseStr = {10:"a",11:"b",12:"c",13:"d&qu ...
- itoa()、atoi()、任意进制转换
头文件:<stdlib.h> itoa --功能:将任意类型的数字转换为字符串.在<stdlib.h>中与之有相反功能的函数是atoi. atoi----功 能: 将字符串转换 ...
随机推荐
- 十八、Nand Flash驱动和Nor Flash驱动
在读者学习本章之前,最好了解Nand Flash读写过程和操作,可以参考:Nand Flash裸机操作. 一开始想在本章写eMMC框架和设备驱动,但是没有找到关于eMMC设备驱动具体写法,所以本章仍继 ...
- Tomcat HTTP connector和AJP connector
Tomcat服务器通过Connector连接器组件与客户程序建立连接,“连接器”表示接收请求并返回响应的端点.即Connector组件负责接收客户的请求,以及把Tomcat服务器的响应结果发送给客户. ...
- 音视频入门-10-使用libyuv对YUV数据进行缩放、旋转、镜像、裁剪、混合
* 音视频入门文章目录 * libyuv libyuv 是 Google 开源的实现各种 YUV 与 RGB 之间相互转换.旋转.缩放等的库.它是跨平台的,可在 Windows.Linux.Mac.A ...
- 在论坛中出现的比较难的sql问题:26(动态行专列+合并字符串、补足行数)
原文:在论坛中出现的比较难的sql问题:26(动态行专列+合并字符串.补足行数) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所 ...
- PHP即时实时输出内容
一般情况下,PHP都是将整个页面全部执行完成后,才会把要输出的内容发送回客户端. for ($i = 0; $i < 10; $i++) { echo $i; sleep(1); } 这段代码会 ...
- MySQL绿色版mysql-5.7.17-winx64简洁安装教程
1.解压MySQL绿色版,复制my-default.ini,修改名称为my.ini 2. 以下为my.ini文件 # For advice on how to change settings plea ...
- springmvc+mybatis的增删改查入门
先到官网了解mybatis的语法:https://mybatis.org/mybatis-3/zh/sqlmap-xml.html 前端用了thymeleaf和vue.js,效果图和demo地址:ht ...
- python简单页面爬虫入门 BeautifulSoup实现
本文可快速搭建爬虫环境,并实现简单页面解析 1.安装 python 下载地址:https://www.python.org/downloads/ 选择对应版本,常用版本有2.7.3.4 安装后,将安装 ...
- windows系统Android-sdk的下载与环境变量配置
最近一段时间在做app的开发,作为前端而言,开发app并不像android工程师那样熟悉android开发:无论是使用cordova.js或者react-native开发都需要配置android开发环 ...
- 【前端开发】ES6知识点系统化梳理笔记
>ES6扩展: #Map和Set是es6标准新增的数据类型 ##Map是key-value(关键字-值),Map允许修改value,不允许修改key,Map支持下标操作 var m = new ...