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.高精度减法
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 大数除法和取余的更多相关文章

  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. 运行时权限请求框架easypermissions

    前言 之前使用过AndPermission权限申请库,当开发者执行有权限的代码发生异常时,AndPermission会抓到异常并回调到失败中,这里要注意的是会抓到任何异常,不仅仅是没有权限时的异常. ...

  2. 算法笔记_068:Dijkstra算法简单介绍(Java)

    目录 1 问题描述 2 解决方案 2.1 使用Dijkstra算法得到最短距离示例 2.2 具体编码   1 问题描述 何为Dijkstra算法? Dijkstra算法功能:给出加权连通图中一个顶点, ...

  3. HDU 1073 Online Judge(字符串)

    Online Judge Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  4. ANDROID颜色设置

    Android中的颜色设置   1.在android中经常看到设置的颜色为八位的十六进制的颜色值,例如: public static final class color { public static ...

  5. Android出现“Read-only file system”解决办法

    操作设备文件系统上的文件结果遇到"... Read-only file system". 解决办法: 1. 最简单的,adb remount 2. 不行的话,adb shell s ...

  6. Codeigniter MongoDB扩展之使用Aggregate实现Sum方法

    本篇文章由:http://xinpure.com/codeigniter-mongodb-extension-using-aggregate-sum-method/ Codeigniter Mongo ...

  7. 接收广播BroadcastReceiver

    Broadcast Receiver用于接收并处理广播通知(broadcast announcements).多数的广播是系统发起的,如地域变换.电量不足.来电来信等.程序也可以播放一个广播.程序可以 ...

  8. Powershell - 获取OS版本信息和catpion信息

    Environment  获取 OSversion: $OSVersion = [System.Environment]::OSVersion.Version WMI获取Caption: $OSCap ...

  9. mybatis开发流程,增删改查

    一.开发流程 1)引jar包 //mybatis_core mybatis3.4core\asm-5.2.jar mybatis3.4core\cglib-.jar mybatis3.4core\co ...

  10. PHP的session存储对PHP运行环境的影响

    转于:http://hmw.iteye.com/blog/1704020 这个问题的引入是由于公司一个项目里需要使用单点登录的功能,为了方便起见,就使用redis来替换php默认的文件存储sessio ...