If We Were a Child Again(大数除法、求余)

2024-08-22 14:38
文章标签 大数 除法 child 求余

本文主要是介绍If We Were a Child Again(大数除法、求余),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Description

       

“Oooooooooooooooh!         

If I could do the easy mathematics like my school days!!         

I can guarantee, that I’d not make any mistake this time!!”         

Says a smart university student!!         

But his teacher even smarter – “Ok! I’d assign you such projects in your software lab. Don’t be so sad.”         

“Really!!” - the students feels happy. And he feels so happy that he cannot see the smile in his teacher’s face.         

          

         

The Problem

          

The first project for the poor student was to make a calculator that can just perform the basic arithmetic operations.         

          

But like many other university students he doesn’t like to do any project by himself. He just wants to collect programs from here and there. As you are a friend of him, he asks you to write the program. But, you are also intelligent enough to tackle this kind of people. You agreed to write only the (integer) division and mod (% in C/C++) operations for him.

           

Input

Input is a sequence of lines. Each line will contain an input number. One or more spaces. A sign (division or mod). Again spaces. And another input number. Both the input numbers are non-negative integer. The first one may be arbitrarily long. The second number n will be in the range (0 < n < 231).         

          

           
Output

A line for each input, each containing an integer. See the sample input and output. Output should not contain any extra space.

          

           
           
Sample Input

110 / 100

99 % 10

2147483647 / 2147483647

2147483646 % 2147483647

          

          

          

          

          
          
Sample Output

1

9

1

2147483646

 

         

         

         



#include <stdio.h>
#include <string.h>void chu (char *p,unsigned long long b);
void yu (char *p,unsigned long long b);int a[1000000];int main()
{static char str[1000000];unsigned long long b = 0,t = 0;char c = ' ';while (scanf ("%s",&str) != EOF){do{c = getchar ();}while (c != '/' && c != '%');scanf ("%llu",&b);if (c == '/'){chu (str,b);}else if (c == '%'){yu (str,b);}}return 0;
}void chu (char *p,unsigned long long b)
{int *p1 = a;unsigned long long tmp = b,shang = 0,yu = 0;unsigned long long quan = 1,i = 0;int tf = 1,pritf = 0;while (*p != '\0'){*p1 = (int)(*p - '0');p1++;p++;}*p1 = 10;p1 = a;while (*p1 != 10){yu = yu * 10;yu += *p1;shang = yu / b;yu %= b;if (shang != 0 && tf){printf ("%llu",shang);tf = 0;pritf = 1;}else{if (!tf)printf ("%llu",shang);}p1++;}if (!pritf)printf ("0");printf ("\n");
}void yu (char *p,unsigned long long b)
{int *p1 = a;unsigned long long tmp = b,shang = 0,yu = 0;unsigned long long quan = 1,i = 0;int tf = 1;while (*p != '\0'){*p1 = (int)(*p - '0');p1++;p++;}*p1 = 10;p1 = a;while (*p1 != 10){yu = yu * 10;yu += *p1;shang = yu / b;yu %= b;p1++;}printf ("%llu\n",yu);
}

这是第一次接触大数的问题,C中没有数据类型可以把这么长的数据储存,所以,需要用字符串或者整形数组来分段储存,在其运算过程中,也需要考虑分段计算的问题。
当所有大数考虑到了,那么就需要考虑特殊情况了,比如求余为本身,求商为0,类似的特殊情况。

这篇关于If We Were a Child Again(大数除法、求余)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1096556

相关文章

uva 10069 DP + 大数加法

代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <cl

Java验证辛钦大数定理

本实验通过程序模拟采集大量的样本数据来验证辛钦大数定理。   实验环境: 本实验采用Java语言编程,开发环境为Eclipse,图像生成使用JFreeChart类。   一,验证辛钦大数定理 由辛钦大数定理描述为: 辛钦大数定理(弱大数定理)  设随机变量序列 X1, X2, … 相互独立,服从同一分布,具有数学期望E(Xi) = μ, i = 1, 2, …, 则对于任意正数ε ,

高精度计算(代码加解析,洛谷p1601,p1303)除法待更新

目录 高精度加法 高精度减法 高精度乘法 高精度加法 我们知道在c++语言中任何数据类型都有一定的表示范围。当两个被加数很大时,正常加法不能得到精确解。在小学,我们做加法都采用竖式方法。那么我们也只需要按照加法进位的方式就能得到最终解。 8 5 6+ 2 5 5-------1 1 1 1 加法进位: c[i] = a[i] + b[i];if(c[i] >=

找第K大数(ACdream 1099)

瑶瑶的第K大 Time Limit: 4000/2000MS (Java/Others)  Memory Limit: 256000/128000KB (Java/Others) Submit  Statistic  Next Problem Problem Description 一天,萌萌的妹子--瑶瑶(tsyao)很无聊,就来找你玩。可是你们都不知道玩什么。。。

信息学奥赛初赛天天练-83-NOIP2014普及组-基础题2-输入设备、输出设备、操作系统、二进制、整数除法、while、do while循环

1 NOIP 2014 普及组 基础题2 4 以下哪一种设备属于输出设备( ) A 扫描仪 B 键盘 C 鼠标 D 打印机 5 下列对操作系统功能的描述最为完整的是( ) A 负责外设与主机之间的信息交换 B 负责诊断机器的故障 C 控制和管理计算机系统的各种硬件和软件资源的使用 D 将没有程序编译成目标程序 11 下列各无符号十进制整数中,能用八位二进制表示的数中最大的是( ) A 296

SDUT2876_走楼梯(大数)

走楼梯 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 小虎发现走楼梯的时候一次上一个台阶比较惬意,一次上两个台阶比较高效,一次上三个台阶就很累人。 小虎是一个即注重质量又注重高效的人,于是他就在上楼梯的时候每步就只跨上一个台阶或两个台阶, 现在小虎想知道他这样上n阶的楼梯一共有多少种走法,但

【高精度】-DLUTOJ-1176-大数乘法

题目链接:http://acm.dlut.edu.cn/problem.php?id=1176 题目描述:赤裸的大数乘法 解题思路: 突然想到自己没写过高精度乘法,就回咱们自己OJ上找出了这道题,赤裸的高精度乘法而已,没想到依然觉得不好写,准确说来是我从小到大算乘法的习惯使我产生了错觉:“ 想写大数乘法就得先写一个大数加法出来 ”。喂!我短路了半天才想明白,int 数组里可以存个两位数啊,再

nth-child 和 nth-of-type 的区别

先看案例: <ul class="demo"><p>p</p><li>one</li><li>two</li><div>div</div></ul> 区别: ele:nth-child: 选择的对象包含父元素下所有子元素ele, 如上 ul.demo 下的子元素包含 p,li,div; ele:nth-of-type: type指的是类型,选择的对象包含父元素下所有的子元素ele,并且必须是

大数问题 *

问题 N : A + B Problem II 时间限制:1 秒内存限制:32 兆特殊判题: 否 提交:58解决: 10 标签 输入输出练习 题目描述 I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.