Basic remains
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5221   Accepted: 2203

Description

Given a base b and two non-negative base b integers p and m, compute p mod m and print the result as a base b integer. p mod m is defined as the smallest non-negative integer k such that p = a*m + k for some integer a.

Input

Input consists of a number of cases. Each case is represented by a line containing three unsigned integers. The first, b, is a decimal number between 2 and 10. The second, p, contains up to 1000 digits between 0 and b-1. The third, m, contains up to 9 digits
between 0 and b-1. The last case is followed by a line containing 0.

Output

For each test case, print a line giving p mod m as a base-b integer.

Sample Input

2 1100 101
10 123456789123456789123456789 1000
0

Sample Output

10
789

给出一个base进制的数,base的范围好在在2到10之间。然后给出在base进制下的p与m,求在base进制下的p%m。

来回的进制转换。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int base;
string num;
string m; int change(string n)
{
int i;
int sum = 0;
int len = n.length(); for (i = 0; i < len; i++)
{
sum = sum*base + n[i] - '0';
}
return sum;
} void res(int mod)
{
int i, n, a[500];
int len = num.length();
int res = 0; for (i = 0; i < len; i++)
{
res = (res*base + num[i] - '0') % mod;
}
//转换成base进制
n = 0;
if (res != 0)
{
while (res != 0)
{
a[n++] = res % base;
res = res / base;
}
for (i = n - 1; i >= 0; i--)
{
printf("%d", a[i]);
}
}
else
{
printf("0");
}
printf("\n");
} int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int mod;
while (cin >> base)
{
if (base == 0)
break;
cin >> num >> m;
mod = change(m); res(mod);
}
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 2305:Basic remains 进制转换的更多相关文章

  1. poj2305-Basic remains(进制转换 + 大整数取模)

    进制转换 + 大整数取模一,题意: 在b进制下,求p%m,再装换成b进制输出. 其中p为b进制大数1000位以内,m为b进制数9位以内二,思路: 1,以字符串的形式输入p,m; 2,转换:字符串-&g ...

  2. POJ 2305 Basic remains(进制转换)

    题目链接:http://poj.org/problem?id=2305 ime Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5326 ...

  3. poj 2305(指定进制,大数取模)

    题意:输入一个进制b,在输入两个基于b进制的大整数 x,y ,求x%y的b进制结果. http://162.105.81.212/JudgeOnline/problem?id=2305 函数: Str ...

  4. POJ 1220 大数字的进制转换,偷下懒,用java

    题意为进制转换,Java的大数类就像是作弊 import java.math.BigInteger; import java.util.Scanner; public class Main { pub ...

  5. poj1220 (高精度任意进制转换)

    http://poj.org/problem?id=1220 高精度任意进制转换 代码是从discuss里找到的,据说是maigo神牛写的. 超精简!! 我自己第一写的时候,还把n进制先转成10进制, ...

  6. SQL Server 进制转换函数

    一.背景 前段时间群里的朋友问了一个问题:“在查询时增加一个递增序列,如:0x00000001,即每一个都是36进位(0—9,A--Z),0x0000000Z后面将是0x00000010,生成一个像下 ...

  7. [No000071]C# 进制转换(二进制、十六进制、十进制互转)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. JS中的进制转换以及作用

    js的进制转换, 分为2进制,8进制,10进制,16进制之间的相互转换, 我们直接利用 对象.toString()即可实现: //10进制转为16进制 ().toString() // =>&q ...

  9. 结合stack数据结构,实现不同进制转换的算法

    #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...

随机推荐

  1. treap(堆树)

    # 2018-09-27 17:35:58 我实现的这个treap不能算是堆.有问题 最近对堆这种结构有点感兴趣,然后想用指针的方式实现一个堆而不是利用数组这种结构,于是自己想到了一个用二叉树结构实现 ...

  2. Django 执行 manage 命令方式

    本人使用的Pycharm作为开发工具,可以在顶部菜单栏的Tools->Run manage.py Task直接打开manager 命令控制台 打开后在底部会有命令窗口: 或者,也可以在Pytho ...

  3. <audio>音频标签

    <audio ref="audio" @canplay="ready" @error="error"  @timeupdate=&qu ...

  4. FFmpeg笔记--vcodec和-c:v,-acodec和-c:a的区别?

    在看ffmpeg命令的时候经常会看到有些地方使用--vcodec指定视频解码器,而有些地方使用-c:v指定视频解码器,那这两个有没有区别呢? ffmpeg的官方文档: -vcodec codec (o ...

  5. dropdownlist select的用法

    <tr>        <td></td>        <td>@Html.DropDownList("ddlSex",@Mode ...

  6. 「ZJOI2006」物流运输

    题目 [内存限制:$256MiB$][时间限制:$1000ms$] [标准输入输出][题目类型:传统][评测方式:文本比较] [题目描述] 物流公司要把一批货物从码头 A 运到码头 B.由于货物量比较 ...

  7. sqlplus 登陆使用

    select * from dept; input order by dname;  追加文本命令  del  n  删除语句 celar buffer ; 清除缓冲区的命令 conn sys as ...

  8. 【jmeter】NO-GUI模式运行的过程及报错解决方法(转)

    1.什么都不需要设置(若非得想配置点什么的话,那请看备注) 2.将脚本抛到Linux里 3.执行 jmeter -n -t /opt/day7_buildbug.jmx -l result.jtl - ...

  9. docker的概念

    Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Lin ...

  10. 新闻网大数据实时分析可视化系统项目——2、linux环境准备与设置

    1.Linux系统常规设置 1)设置ip地址 使用界面修改ip比较方便,如果Linux没有安装操作界面,需要使用命令:vi /etc/sysconfig/network-scripts/ifcfg-e ...