Binary mod and divide(模拟+大数)
2024-09-03 05:57:47
描述
Most people know that the binary operations. Do you know the binary mod and divide?
Now give the Binary number N and a integer number M ,Can you tell me the answer of N%(2^M) and N/(2^M)?
输入
Input contains multiple test cases.
The first line of each test case contains an binary number N no more than 128 bits and an integer M (1 <= M <= 64).
when N=0&&M=0 ,test is over.
输出
output the answer the N%(2^M) and N/(2^M).
样例输入
111 2
1111 2
0 0
样例输出
mod=3, divide=1
mod=3, divide=3
题目大意是: 给一个不超过128位的二进制数N,和一个整数M,求N%(2^M)和N/(2^M).
1.把2^M次转化成二进制,即1后面M个0;N的最小位开始数M位,把N分成两部分,左边为倍数,右边为余数。
eg:1010101101,5 .即倍数的二进制为10101,余数的二进制为01101.
2.计算的话,2^128次longlong存不下,所以用数组存了。
代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
char a[];
void add(int l,int r,int b[])
{
for(int i=l;i<r;i++)
{
for(int j=;j<;j++)
b[j]*=;
b[]+=a[i]-'';
for(int j=;j<;j++)
{
if(b[j]>=)
{
b[j+]+=b[j]/;
b[j]%=;
}
}
}
}
int main()
{
int m,i,l;
int b[],c[];
while(scanf("%s%d",a,&m))
{
if(!m&&a[]=='') break;
memset(b,,sizeof(b));
memset(c,,sizeof(c));
l=strlen(a);
add(,l-m,b);
add(max(,l-m),l,c);
35 printf("mod=");
for(i=;i>=;i--)
if(c[i]) break;
if(i==-) printf("");
for(;i>=;i--)
printf("%d",c[i]);
41 printf(", divide=");
for(i=;i>=;i--)
if(b[i]) break;
if(i==-) printf("");
for(;i>=;i--)
printf("%d",b[i]);
putchar();
}
}
最新文章
- 剑指offer四:链表中倒数第k个结点
- jQuery选择器大全
- Java接口回调
- Hibernate 分页时 Long 无法转化成Integer类型 异常
- Vim篇
- vba 快速定位当前EXCEL最后一栏
- document.write 向文档中写内容,包括文本、脚本、元素之类的,但是它在什么时候执行不会覆盖当前页面内容尼?
- AspNet Core 初步认识
- redis常见命令使用
- RocketMQ三主三从二命名服务平滑版本升级实操
- PostMessage发送WM_CLOSE消息,实现关闭任意程序
- Spark-shell错误:Missing Python executable &#39;python&#39;, defaulting to ...
- Codeforces 570 - A/B/C/D/E - (Done)
- jsonArray返回
- 用ActiveX 创建自己的comboBox 控件(二)
- #Leetcode# 788. Rotated Digits
- 20155117王震宇 2016-2017-2 《Java程序设计》第六周学习总结
- uniGUI试用笔记(三)
- button中文字垂直居中
- Java用户输入数值,做简单的猜数字游戏,导入基础的工具包util