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 ...
随机推荐
- JAVA学习笔记 -- 读写XML
XML是一种可扩展标记语言 以下是一个完整的XML文件(也是下文介绍读写XML的样本): <? xml version="1.0" encoding="UTF-8& ...
- iOS真机调试 for Xcode 5
由于Xcode5的到来,关于iOS软件进行真机调试方面,有了一些变化,苹果在Xcode 5中修改了一些规则,一方面是阻止以往破解的方式进行调试(免证书).另一方面是添加了自动生成证书的功能特性,来加快 ...
- 算法笔记_099:蓝桥杯练习 算法提高 排列数(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 0.1.2三个数字的全排列有六种,按照字母序排列如下: 012.021.102.120.201.210 输入一个数n 求0~9十个数的全排 ...
- python——python数据结构之栈、队列的实现
这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...
- Xshell5 破解
Xshell5激活码 Xshell5激活方式Xshell5破解版 Xshell是一个用于MS Windows平台的强大的SSH,TELNET,和RLOGIN终端仿真软件.它使得用户能轻松和安全地从Wi ...
- 对象的序列化(Serialization)
一.什么是序列化 序列化表示将一个对象转换成可存储或可传输的状态,序列化后对象可以在网络上进行传输,也可以存储到本地.对象的寿命通常随着生成该对象的程序的终止而终止.有时候,可能需要将对象的状态保存下 ...
- Aeroo Reports Linux server
This article covers installation process for Aeroo reporting engine on Linux servers. If you find th ...
- 让你的 wowza server提供 RESTful web 服务
有时我们 nginx 须要和 wowza 服务器交互以进行一些 LB 事宜:有时我们的管理员须要实时了解 wowza 服务器的一些其它状态信息(比方一些自己定义对象的状态等等).而用 ...
- ZendServer中关于php.ini不同环境的建议
ZendServer根据开发环境和产品环境的不同情况,对php.ini中的一些选项做了建议设置,列表如下: ;;;;;;;;;;;;;;;;;;; ; Quick Reference ; ;;;;;; ...
- Python-文件修改器
#-*- coding: utf-8 -*- import os import sys import glob from PyQt4.QtGui import * from PyQt4.QtCore ...