描述

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

最新文章

  1. 剑指offer四:链表中倒数第k个结点
  2. jQuery选择器大全
  3. Java接口回调
  4. Hibernate 分页时 Long 无法转化成Integer类型 异常
  5. Vim篇
  6. vba 快速定位当前EXCEL最后一栏
  7. document.write 向文档中写内容,包括文本、脚本、元素之类的,但是它在什么时候执行不会覆盖当前页面内容尼?
  8. AspNet Core 初步认识
  9. redis常见命令使用
  10. RocketMQ三主三从二命名服务平滑版本升级实操
  11. PostMessage发送WM_CLOSE消息,实现关闭任意程序
  12. Spark-shell错误:Missing Python executable &#39;python&#39;, defaulting to ...
  13. Codeforces 570 - A/B/C/D/E - (Done)
  14. jsonArray返回
  15. 用ActiveX 创建自己的comboBox 控件(二)
  16. #Leetcode# 788. Rotated Digits
  17. 20155117王震宇 2016-2017-2 《Java程序设计》第六周学习总结
  18. uniGUI试用笔记(三)
  19. button中文字垂直居中
  20. Java用户输入数值,做简单的猜数字游戏,导入基础的工具包util

热门文章

  1. border 0px和border none的区别
  2. PHP的知识点总结1
  3. 【前端】Html5浏览器缓存 sessionStorage 与 localStorage
  4. webform简单空间以及数据库访问
  5. How to proxy a web site by apache2 in Ubuntu
  6. 全文索引Elasticsearch,Solr,Lucene
  7. checkbox设置复选框的只读效果不让用户勾选
  8. ubuntu4.04服务器添加虚拟主机
  9. 51nod 1097 拼成最小的数
  10. SQLite – GLOB子句