uva 10494 - If We Were a Child Again

If We Were a Child Again

Input: standard input
Output: standard output

Time Limit: 7 seconds

 

“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

高精度对低精度的除法和取余,模拟下小学时候的除法过程就行了。取余则调用已经实现的+-*/就行了。

中间结果要用long long才能过,坑爹的是忽略替换for(int i = 0, g = 0……)中的int结果WA了好多次。

套用模版时如果需要用到long long就全局替换掉int.注意替换后一些关键词也被替换了此时注意检查语法错误就行了

  1. /*
  2. 1.高精度加法
  3. 2.高精度减法
  4. 3.高精度乘法
  5. 4.高精度除以低精度
  6. 5.高精度对低精度的取余
  7.  
  8. 必要时可以将全局的long long替换成long long.除了main函数的返回值long long
  9. 用到除法和取余的时候可能需要把全局的long long替换成long long
  10. */
  11. #include <cstdio>
  12. #include <iostream>
  13. #include <cstring>
  14. #include <climits>
  15. #include <cassert>
  16. using namespace std;
  17.  
  18. #define maxn 30000
  19.  
  20. struct bign
  21. {
  22. long long len, s[maxn];
  23.  
  24. bign()
  25. {
  26. memset(s, , sizeof(s));
  27. len = ;
  28. }
  29.  
  30. bign(long long num)
  31. {
  32. *this = num;
  33. }
  34.  
  35. bign(const char* num)
  36. {
  37. *this = num;
  38. }
  39.  
  40. bign operator = (long long num)
  41. {
  42. char s[maxn];
  43. sprintf(s, "%d", num);
  44. *this = s;
  45. return *this;
  46. }
  47.  
  48. bign operator = (const char* num)
  49. {
  50. len = strlen(num);
  51. for (long long i = ; i < len; i++) s[i] = num[len-i-] - '';
  52. return *this;
  53. }
  54.  
  55. string str() const
  56. {
  57. string res = "";
  58. for (long long i = ; i < len; i++) res = (char)(s[i] + '') + res;
  59. if (res == "") res = "";
  60. return res;
  61. }
  62.  
  63. /*去除前导0*/
  64. void clean()
  65. {
  66. while(len > && !s[len-]) len--;
  67. }
  68.  
  69. /*高精度的加法*/
  70. bign operator + (const bign& b) const
  71. {
  72. bign c;
  73. c.len = ;
  74. for (long long i = , g = ; g || i < max(len, b.len); i++)
  75. {
  76. long long x = g;
  77. if (i < len) x += s[i];
  78. if (i < b.len) x += b.s[i];
  79. c.s[c.len++] = x % ;
  80. g = x / ;
  81. }
  82. return c;
  83. }
  84.  
  85. /*高精度的减法*/
  86. bign operator - (const bign& b)
  87. {
  88. bign c; c.len = ;
  89. for (long long i = , g = ; i < len; i++)
  90. {
  91. long long x = s[i] - g;
  92. if (i < b.len) x -= b.s[i];
  93. if (x >= )
  94. g = ;
  95. else
  96. {
  97. g = ;
  98. x += ;
  99. }
  100. c.s[c.len++] = x;
  101. }
  102. c.clean();
  103. return c;
  104. }
  105.  
  106. /*高精度的乘法*/
  107. bign operator * (const bign& b)
  108. {
  109. bign c; c.len = len + b.len;
  110. for (long long i = ; i < len; i++)
  111. for (long long j = ; j < b.len; j++)
  112. c.s[i+j] += s[i] * b.s[j];
  113. for (long long i = ; i < c.len-; i++)
  114. {
  115. c.s[i+] += c.s[i] / ;
  116. c.s[i] %= ;
  117. }
  118. c.clean();
  119. return c;
  120. }
  121.  
  122. /*高精度除以低精度*/ /*用到除法和取余的时候可能需要把全局的int替换成long long*/
  123. bign operator / (long long b) const
  124. {
  125. assert(b > );
  126. bign c;c.len = len;
  127. for (long long i = len-, g = ; i >= ; --i)
  128. {
  129. long long x = *g+s[i]; //这里可能会超过int 故用long long
  130.  
  131. c.s[i] = x/b; //这里可能会超过int
  132.  
  133. g = x-c.s[i]*b; //这里可能会超过int
  134. }
  135. c.clean();
  136. return c;
  137. }
  138.  
  139. /*高精度对低精度取余*/ /*用到除法和取余的时候可能需要把全局的int替换成long long*/
  140. bign operator % (long long b)
  141. {
  142. assert(b > );
  143. bign d = b;
  144. bign c = *this-*this/b*d;
  145. return c;
  146. }
  147.  
  148. bool operator < (const bign& b) const
  149. {
  150. if (len != b.len) return len < b.len;
  151. for (long long i = len-; i >= ; i--)
  152. if (s[i] != b.s[i]) return s[i] < b.s[i];
  153. return false;
  154. }
  155.  
  156. bool operator > (const bign& b) const
  157. {
  158. return b < *this;
  159. }
  160.  
  161. bool operator <= (const bign& b)
  162. {
  163. return !(b > *this);
  164. }
  165.  
  166. bool operator >= (const bign& b)
  167. {
  168. return !(b < *this);
  169. }
  170.  
  171. bool operator == (const bign& b)
  172. {
  173. return !(b < *this) && !(*this < b);
  174. }
  175.  
  176. bool operator != (const bign& b)
  177. {
  178. return (b < *this) || (*this < b);
  179. }
  180.  
  181. bign operator += (const bign& b)
  182. {
  183. *this = *this + b;
  184. return *this;
  185. }
  186. };
  187.  
  188. istream& operator >> (istream &in, bign& x)
  189. {
  190. string s;
  191. in >> s;
  192. x = s.c_str();
  193. return in;
  194. }
  195.  
  196. ostream& operator << (ostream &out, const bign& x)
  197. {
  198. out << x.str();
  199. return out;
  200. }
  201.  
  202. int main()
  203. {
  204. bign a;
  205. char b;
  206. long long c;
  207. while (cin >> a >> b >> c)
  208. {
  209. if (b == '/')
  210. cout << a/c << endl;
  211. else
  212. cout << a%c << endl;
  213. }
  214. }

uva 10494 - If We Were a Child Again 大数除法和取余的更多相关文章

  1. (高精度运算4.7.27)UVA 10494 If We Were a Child Again(大数除法&&大数取余)

    package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class UVA_10494 ...

  2. UVA - 10494 If We Were a Child Again

    用java写的大数基本操作,java要求的格式比较严谨. import java.util.*; import java.math.*; public class Main { public stat ...

  3. UVA 10494 (13.08.02)

    点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...

  4. UVA 11582 Colossal Fibonacci Numbers!(循环节打表+幂取模)

    题目链接:https://cn.vjudge.net/problem/UVA-11582 /* 问题 输入a,b,n(0<a,b<2^64(a and bwill not both be ...

  5. UVa 10213 How Many Pieces of Land ? (计算几何+大数)欧拉定理

    题意:一块圆形土地,在圆周上选n个点,然后两两连线,问把这块土地分成多少块? 析:这个题用的是欧拉公式,在平面图中,V-E+F=2,其中V是顶点数,E是边数,F是面数.对于这个题只要计算V和E就好. ...

  6. UVa 10213 How Many Pieces of Land ? (计算几何+大数)

    题意:一块圆形土地,在圆周上选n个点,然后两两连线,问把这块土地分成多少块? 析:这个题用的是欧拉公式,在平面图中,V-E+F=2,其中V是顶点数,E是边数,F是面数.对于这个题只要计算V和E就好. ...

  7. UVA+POJ中大数实现的题目,持续更新(JAVA实现)

    UVA10494:If We Were a Child Again 大数除法加取余 import java.util.Arrays; import java.util.Scanner; import ...

  8. TLCL

    参考阅读:http://billie66.github.io/TLCL/book/chap04.html 绝对路径 An absolute pathname begins with the root ...

  9. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

随机推荐

  1. HTML5学习笔记简明版(5):input的type超级类型

    HTML5为input的type类型添加了多种枚举值,用来表达不同的意思.同事具有验证功能,假设格式不正确,浏览器将原始提供错误提示,堪称超级牛X啊,详细例如以下: Keyword Data type ...

  2. 算法笔记_088:蓝桥杯练习 8-1因式分解(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 设计算法,用户输入合数,程序输出若个素数的乘积.例如,输入6,输出2*3.输入20,输出2*2*5. 样例 与上面的样例输入对应的输出. ...

  3. Android NDK学习记录(一)

    一.NDK环境在Mac中部署 1.准备eclipse,android sdk安装包,android ndk安装包(http://dl.google.com/android/ndk/android-nd ...

  4. js 判断是否是IE浏览器及ie版本

      方式一:只判断是否是ie浏览器 /** * 判断是否是IE浏览器,支持IE6-IE11 */ function isIE() { //ie? if (!!window.ActiveXObject ...

  5. 【DB2】NULLS LAST与NULLS FIRST

    DB2函数配合 select row_number() over(order by col desc nulls first/nulls last ) as rn,col from table1ord ...

  6. 乐鑫esp8266的串口通讯驱动源文件,nonos和rtos版本

    目录 一.前言: 二.esp8266的串口分布情况: 三.esp8266的串口通讯时候,应该怎么接线: 四.esp8266的NONOS非系统,串口编程: 五.esp8266的RTOS实时系统,串口编程 ...

  7. Javascript 判断网页横竖屏

    本篇文章由:http://xinpure.com/javascript-to-determine-page-anyway-screen/ Html5 流行至今,自适应的网站已经多如牛毛,但是横竖屏的切 ...

  8. input text 去掉标签下拉提示autocomplete

    autocomplete 属性 autocomplete 属性规定输入字段是否应该启用自动完成功能. 自动完成允许浏览器预测对字段的输入.当用户在字段开始键入时,浏览器基于之前键入过的值,应该显示出在 ...

  9. Android App补丁更新

    上一周比较忙,忙的不可开交,写的文章也就两篇,在此希望大家见谅.这周呢,突然闲下来了,有时间了,就重构了下代码,捣鼓点前卫的技术,沉淀沉淀.所以呢,今天就分享下这几天研究的东西. 移动互联网主打的就是 ...

  10. DDCX2018届校招内推笔试——算法工程师

    -------------------------------------------------------------------------[选择题]---------------------- ...