uva 10494 - If We Were a Child Again 大数除法和取余
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). |
||
OutputA line for each input, each containing an integer. See the sample input and output. Output should not contain any extra space. |
||
Sample Input110 / 100 99 % 10 2147483647 / 2147483647 2147483646 % 2147483647 |
||
Sample Output1 9 1 2147483646 |
||
高精度对低精度的除法和取余,模拟下小学时候的除法过程就行了。取余则调用已经实现的+-*/就行了。
中间结果要用long long才能过,坑爹的是忽略替换for(int i = 0, g = 0……)中的int结果WA了好多次。
套用模版时如果需要用到long long就全局替换掉int.注意替换后一些关键词也被替换了此时注意检查语法错误就行了
/*
1.高精度加法
2.高精度减法
3.高精度乘法
4.高精度除以低精度
5.高精度对低精度的取余 必要时可以将全局的long long替换成long long.除了main函数的返回值long long
用到除法和取余的时候可能需要把全局的long long替换成long long
*/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <climits>
#include <cassert>
using namespace std; #define maxn 30000 struct bign
{
long long len, s[maxn]; bign()
{
memset(s, , sizeof(s));
len = ;
} bign(long long num)
{
*this = num;
} bign(const char* num)
{
*this = num;
} bign operator = (long long num)
{
char s[maxn];
sprintf(s, "%d", num);
*this = s;
return *this;
} bign operator = (const char* num)
{
len = strlen(num);
for (long long i = ; i < len; i++) s[i] = num[len-i-] - '';
return *this;
} string str() const
{
string res = "";
for (long long i = ; i < len; i++) res = (char)(s[i] + '') + res;
if (res == "") res = "";
return res;
} /*去除前导0*/
void clean()
{
while(len > && !s[len-]) len--;
} /*高精度的加法*/
bign operator + (const bign& b) const
{
bign c;
c.len = ;
for (long long i = , g = ; g || i < max(len, b.len); i++)
{
long long x = g;
if (i < len) x += s[i];
if (i < b.len) x += b.s[i];
c.s[c.len++] = x % ;
g = x / ;
}
return c;
} /*高精度的减法*/
bign operator - (const bign& b)
{
bign c; c.len = ;
for (long long i = , g = ; i < len; i++)
{
long long x = s[i] - g;
if (i < b.len) x -= b.s[i];
if (x >= )
g = ;
else
{
g = ;
x += ;
}
c.s[c.len++] = x;
}
c.clean();
return c;
} /*高精度的乘法*/
bign operator * (const bign& b)
{
bign c; c.len = len + b.len;
for (long long i = ; i < len; i++)
for (long long j = ; j < b.len; j++)
c.s[i+j] += s[i] * b.s[j];
for (long long i = ; i < c.len-; i++)
{
c.s[i+] += c.s[i] / ;
c.s[i] %= ;
}
c.clean();
return c;
} /*高精度除以低精度*/ /*用到除法和取余的时候可能需要把全局的int替换成long long*/
bign operator / (long long b) const
{
assert(b > );
bign c;c.len = len;
for (long long i = len-, g = ; i >= ; --i)
{
long long x = *g+s[i]; //这里可能会超过int 故用long long c.s[i] = x/b; //这里可能会超过int g = x-c.s[i]*b; //这里可能会超过int
}
c.clean();
return c;
} /*高精度对低精度取余*/ /*用到除法和取余的时候可能需要把全局的int替换成long long*/
bign operator % (long long b)
{
assert(b > );
bign d = b;
bign c = *this-*this/b*d;
return c;
} bool operator < (const bign& b) const
{
if (len != b.len) return len < b.len;
for (long long i = len-; i >= ; i--)
if (s[i] != b.s[i]) return s[i] < b.s[i];
return false;
} bool operator > (const bign& b) const
{
return b < *this;
} bool operator <= (const bign& b)
{
return !(b > *this);
} bool operator >= (const bign& b)
{
return !(b < *this);
} bool operator == (const bign& b)
{
return !(b < *this) && !(*this < b);
} bool operator != (const bign& b)
{
return (b < *this) || (*this < b);
} bign operator += (const bign& b)
{
*this = *this + b;
return *this;
}
}; istream& operator >> (istream &in, bign& x)
{
string s;
in >> s;
x = s.c_str();
return in;
} ostream& operator << (ostream &out, const bign& x)
{
out << x.str();
return out;
} int main()
{
bign a;
char b;
long long c;
while (cin >> a >> b >> c)
{
if (b == '/')
cout << a/c << endl;
else
cout << a%c << endl;
}
}
uva 10494 - If We Were a Child Again 大数除法和取余的更多相关文章
- (高精度运算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 ...
- UVA - 10494 If We Were a Child Again
用java写的大数基本操作,java要求的格式比较严谨. import java.util.*; import java.math.*; public class Main { public stat ...
- UVA 10494 (13.08.02)
点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...
- 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 ...
- UVa 10213 How Many Pieces of Land ? (计算几何+大数)欧拉定理
题意:一块圆形土地,在圆周上选n个点,然后两两连线,问把这块土地分成多少块? 析:这个题用的是欧拉公式,在平面图中,V-E+F=2,其中V是顶点数,E是边数,F是面数.对于这个题只要计算V和E就好. ...
- UVa 10213 How Many Pieces of Land ? (计算几何+大数)
题意:一块圆形土地,在圆周上选n个点,然后两两连线,问把这块土地分成多少块? 析:这个题用的是欧拉公式,在平面图中,V-E+F=2,其中V是顶点数,E是边数,F是面数.对于这个题只要计算V和E就好. ...
- UVA+POJ中大数实现的题目,持续更新(JAVA实现)
UVA10494:If We Were a Child Again 大数除法加取余 import java.util.Arrays; import java.util.Scanner; import ...
- TLCL
参考阅读:http://billie66.github.io/TLCL/book/chap04.html 绝对路径 An absolute pathname begins with the root ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
随机推荐
- Gradle在Android中的基本使用
1.基本概念 程序开发作为一种工程作业,不光是编写代码,还涉及到工程的各种管理(依赖,打包,部署,发布,各种渠道的差异管理.....).很多时候,我们反复的build,clean,签名,打包,发布,那 ...
- LR打不开浏览器的解决方法
很久没用LoadRunner了,今天想复习一下,免得技能生疏,安装了一个LR11,跑一下,竟然打不开IE浏览器: 这时肯定是靠谷哥跟度娘的,经过一轮搜索,可以解决打开IE了,但录制不了解决,又 ...
- Python-Mac OS X EI Capitan下安装Scrapy
sudo pip install scrapy --ignore-installed six #sudo pip install scrapy --upgrade --ignore-installed ...
- oracle 查询 函数练习2
/*以下代码是对emp表/dept表/salgrade表进行显示宽度设置 */col empno for 9999;col ename for a10;col job for a10;col mgr ...
- oc 阿拉伯数字转中文数字
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; formatter.numberStyle = NSNumberFor ...
- 在Ubuntu下安装mongodb
一. 在Ubuntu下最傻瓜的步骤(以下都在root用户下进行操作): 1.运行"apt-get install mongo" 如果遇到找不到安装包的话运行"apt-ge ...
- atitit.html5动画特效----打水漂 ducks_and_drakes
atitit.html5动画特效----打水漂 ducks_and_drakes 1. 原理 1 2. fly jquery插件 1 3. ---------code 2 4. 参考 4 1. 原理 ...
- Visual Studio - 安装VAX
安装方法: 我的VS2013装D盘了,安装VA以后,把VA_X.dll替换到下面的程序路径中即可完成破解. C:\Users\Administrator\AppData\Local\Microsoft ...
- QT .pro文件 LIBS用法详解
在程序中需要使用到团队其它成员开发的静态库和动态库,起初是知道使用LIBS变量在在.pro文件中指定需要包含的库,但是实际使用的时候却遇到很大麻烦,但其实确实是因为自己看官方文档不太用心造成的. 下面 ...
- jsp tld的function 自定义方法扩展
引入方式示例: <%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %> 写法示例: <? ...