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;
}

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

最新文章

  1. 第四章 springboot + swagger(转载)
  2. C++ 类的静态成员及静态成员函数
  3. c++复习一:复数运算的简单实现。
  4. Socket的双网卡收发(C#)
  5. Mod 与 RequireJS/SeaJS 的那些事
  6. Eclipse用法和技巧二:自动生成Main方法1
  7. linux的基本命令
  8. jsp 按钮颜色
  9. XML文档读取-DOM
  10. Web安全概述
  11. Linux Shell脚本攻略学习总结:三
  12. 实战项目开发细节:C语言分离一个16进制数取出相应的位1或0
  13. linux安装lamp环境(linux+apache+mysql+php)
  14. go time模块
  15. android hal 诠释
  16. MY服务器架设
  17. Java和C#基本类库的区别
  18. 铁乐学python_day09_作业
  19. Linux 策略路由配置
  20. find命令中的print0和xargs -0

热门文章

  1. luogu P3356 火星探险问题
  2. red hat 7、centos7的root密码破译
  3. 利用kali自带的msfvenom工具生成远程控制软件(木马)
  4. Ajax请求回调地狱及解决方案(promise、async和await)
  5. java课堂第一次随机测试和课件课后动手动脑问题解决(2019-9-16 )
  6. MFC CListCtrl 显示bmp图片
  7. WebView使用详解(二)——WebViewClient与常用事件监听
  8. python 开启http服务并下载文件
  9. Linux CentOS7 VMware usermod命令、用户密码管理、mkpasswd命令
  10. selenium+chrome抓取淘宝宝贝-崔庆才思路