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 ...
随机推荐
- ZK框架笔记2、ZK框架安装、相关类库、web及zk配置
1.先去ZK官网注册一个账号 2.在MyEclipse菜单栏中Help----Eclipse Marketplace中搜索ZK Studio,点击install安装即可 3.相关类库 ...
- struts struts拦截器(过滤器)
在struts中尽量避免自定义拦截器,因为大部分需要自己定义拦截器的时候,设计思路就不对了.大部分拦截器框架都有给你定义好了.而且如果在struts中定义拦截器相当于和这个框架绑定了,假如以后要扩展或 ...
- LaTeX 中使用三级标题
需要在导言区加入命令:\setcounter{secnumdepth}{4} 而后: \section{一级标题} \subsection{二级标题} \subsubsection{三级标题}
- chrome 脚本学习
# 编写可复用的代码段(snippet)教程 https://jingyan.baidu.com/article/67508eb423d2929ccb1ce45b.html # chrome 脚本开发 ...
- windows server 2003中端口默认不能使用问题
问题:在windows server 2003中IIS6.0新建站点,给了一个新端口(非80),然后配置好后不能访问 解决方案:系统内置防火墙需要添加对应端口,如下图: 即解决.
- atitit.《金刚经》与it软件项目管理的启发 读后感attilax
atitit.<金刚经>与it软件项目管理的启发 读后感attilax 1.1. 经中宣称一切世间事物空幻不实,如梦幻泡如梦幻泡影,实相者则是非相.主 张 放弃对现实世间的执著或眷恋,以般 ...
- PHPCMS V9数据库表结构分析
PHPCMS V9可以轻松承载百万级的访问数据,最大的功臣就是PHPCMS良好的数据库结构,在数据库的设计方面,一定是下足了功夫. 一般网站的信息量离这个级别相差甚远,但是了解学习一下PHPCMS ...
- Android实现微信自己主动抢红包的程序
简单实现了微信自己主动抢红包的服务,原理就是依据keyword找到对应的View, 然后自己主动点击.主要是用到AccessibilityService这个辅助服务,基本能够满足自己主动抢红包的功能, ...
- 前端_basic
web: 分三部分:1.HTML:2.CSS:3.JavaScript. 1.HTML:用来构建网页的结构和内容: 2.CSS:用来给网页化妆,美化网页: 3.JavaScript:用来让网页呈现动态 ...
- linux之mail
使用该命令自动把系统发给root用户的邮件发送到自己的邮箱 #vi /etc/aliases # 编辑该文件并在最后一行添加即可,如图所示