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. 基于Qt 5.9.8,编译Qtxlsx

    1.源码下载地址:https://github.com/dbzhang800/QtXlsxWriter 2.下载并安装编译时需要的工具 Perl:https://www.perl.org/get.ht ...

  2. [ 剑指Offer ] Week2 学习笔记

    打印从1到最大的n位数 题解:由于未知n的大小,需要考虑大数问题.在这样的情况下,逐位地将字符串转换为数字输出,不会有溢出的可能.使用全排列的方式列出所有数字,省去了需要考虑进位的可能. 初始化数组, ...

  3. C语言程序设计-现代方法(笔记3)

    第十三章 字符串 1.字符串字面量(13.1) 字符串字面量:用一对双引号括起来的字符序列.字符串字面量可以像字符常量一样包含转义字序列. 在字符串字面量中小心使用八进制和十六进制的转义序列. 字符串 ...

  4. 卸载sql server 2008

    一.    SQL2008卸载. 1.从控制面板卸载 1)点击计算机右下角“开始”,点击“控制面板” 2)点击“卸载程序”. 3)在程序列表中找到“Microsoft SQL Server 2008” ...

  5. jmeter分布式linux负载机,windows主控机

    1.将参数化文件上传到linux服务器,放在linux上jmeter的bin路径下 2.设置server.rmi.ssl.disable=true 分别修改主控机和负载机的jmeter.propert ...

  6. Spring注解@ConfigurationPropertie

    @ConfigurationPropertie作用 参考的博客 springboot中@ConfigurationProperties注解的工作原理 @ConfigurationProperties是 ...

  7. HTML有2种路径的写法:绝对路径和相对路径

    HTML有2种路径的写法:绝对路径和相对路径 2016年11月30日 17:51:20 Bolon0708 阅读数 21775   版权声明:本文为博主原创文章,未经博主允许不得转载. https:/ ...

  8. GIMP

    1. 认识GIMP 2. GIMP与Photoshop的对比 3. GIMP官方手册教程 4. 2本GIMP的外文书下载 5. 2个外部入门教程 6. 其他相关软件 1. 认识GIMP GIMP是可用 ...

  9. 在linux环境中如何删除文件

    使用rm -rf 目录名字 命令即可 -r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思 eg 删除文件夹实例:rm -rf /var/log/httpd/acc ...

  10. boost::program_options 解析命令行参数

    源码: #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int a ...