POJ 2305:Basic remains 进制转换
2024-10-08 14:14:03
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.
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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
最新文章
- 第四章 springboot + swagger(转载)
- C++ 类的静态成员及静态成员函数
- c++复习一:复数运算的简单实现。
- Socket的双网卡收发(C#)
- Mod 与 RequireJS/SeaJS 的那些事
- Eclipse用法和技巧二:自动生成Main方法1
- linux的基本命令
- jsp 按钮颜色
- XML文档读取-DOM
- Web安全概述
- Linux Shell脚本攻略学习总结:三
- 实战项目开发细节:C语言分离一个16进制数取出相应的位1或0
- linux安装lamp环境(linux+apache+mysql+php)
- go time模块
- android hal 诠释
- MY服务器架设
- Java和C#基本类库的区别
- 铁乐学python_day09_作业
- Linux 策略路由配置
- find命令中的print0和xargs -0
热门文章
- luogu P3356 火星探险问题
- red hat 7、centos7的root密码破译
- 利用kali自带的msfvenom工具生成远程控制软件(木马)
- Ajax请求回调地狱及解决方案(promise、async和await)
- java课堂第一次随机测试和课件课后动手动脑问题解决(2019-9-16 )
- MFC CListCtrl 显示bmp图片
- WebView使用详解(二)——WebViewClient与常用事件监听
- python 开启http服务并下载文件
- Linux CentOS7 VMware usermod命令、用户密码管理、mkpasswd命令
- selenium+chrome抓取淘宝宝贝-崔庆才思路