高精度系列——反正高精除高精我不会
来源:互联网 发布:木天蓼对猫的作用知乎 编辑:程序博客网 时间:2024/05/20 18:51
高精度小贴士:
除法/减法从最高位开始,乘法/加法从最低位开始;
高精度的实质是模拟,注意边界/进位/退位等等。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;char s1[3010], s2[3010];int x, lenf, leng, f[3010], g[3010], temp[4000010]; //f[1]存个位 void add() {//如果是单精就搞成高精>~<我不管我不管!! scanf("%s", s1); scanf("%s", s2); lenf = strlen(s1); for(int i = 0; i < lenf; i ++) f[lenf - i] = s1[i] - '0'; leng = strlen(s2); for(int i = 0; i < leng; i ++) g[leng - i] = s2[i] - '0'; int len = max(lenf, leng); for(int i = 1; i <= len; i ++) { if(f[i] + g[i] > 9) f[i + 1] ++; f[i] = (f[i] + g[i]) % 10; } if(f[len + 1]) len ++; for(int i = len; i >= 1; i --) printf("%d", f[i]);}void mul() { //高精乘单精 scanf("%s", s1); scanf("%d", &x); lenf = strlen(s1); for(int i = 0; i < lenf; i ++) f[lenf - i] = s1[i] - '0', f[lenf - i] *= x; for(int i = 1; i <= lenf; i ++) { if(f[i] > 9) f[i + 1] += f[i] / 10, f[i] %= 10; } while(f[lenf + 1]) { lenf ++; f[lenf + 1] += f[lenf] / 10; f[lenf] %= 10; } for(int i = lenf; i >= 1; i --) printf("%d", f[i]);}void big_mul() { //高精乘高精 scanf("%s", s1); scanf("%s", s2); lenf = strlen(s1); for(int i = 0; i < lenf; i ++) f[lenf - i - 1] = s1[i] - '0'; leng = strlen(s2); for(int i = 0; i < leng; i ++) g[leng - i - 1] = s2[i] - '0'; int mmax = 0, len = 0; for(int i = 0; i < lenf; i ++) for(int j = 0; j < leng; j ++) { temp[i + j - 1] += f[i] * g[j]; if(temp[i + j - 1]) mmax = i + j - 1; } for(int i = 0; i <= mmax + 15; i ++) { temp[i + 1] += temp[i] / 10; temp[i] %= 10; if(temp[i]) len = i; } for(int i = len; i >= 0; i --) printf("%d", temp[i]);}void div() {//高精除单精 scanf("%s", s1); scanf("%d", &x); lenf = strlen(s1); for(int i = 0; i < lenf; i ++) f[lenf - i] = s1[i] - '0'; int mod = 0; for(int i = lenf; i >= 1; i --) { temp[i] = (f[i] + mod * 10) / x; mod = (f[i] + mod * 10) % x; } int i = lenf; for(; !temp[i]; i --); for(; i; i --) printf("%d", temp[i]);}int main() { add(); mul(); big_mul(); div(); return 0;}
阅读全文
0 0
- 高精度系列——反正高精除高精我不会
- 高精度之高精除低精
- 高精度除法(高精除高精)
- 【高精度模板】【高精加减乘没有除】
- 高精度除法(高除高)
- 高精除
- 高精度之高精除以高精
- pascal 高精度压位(加减乘&高精除单精)
- 高精度之高精度除法(高精除以高精)
- 高精度——A+B Problem(高精)
- 高精度之高精度除法(高精除以低精)
- 【给将来学神的算法详解--高精】(1)(我的)高精度模板
- 高精度除法(高精除以低精)——一步一步算法篇
- 高精度加法 洛谷 P1601 A+B Problem(高精)
- 高精度之高精除以低精取余(天使的起誓)
- 【基础代码】高精{加.减.乘.除}
- 1028. Hanoi Tower Sequence (高精,除2)
- 西行寺幽幽子(恶心的高精除)
- js 数组转json,json转数组
- MVP注册登录
- Mybatis框架
- 使用Spring的@Scheduled实现定时任务
- Android 证书检测问题
- 高精度系列——反正高精除高精我不会
- spring的简单介绍及简单配置
- 十九、封装
- 将内核生成的打印信息,提出后,得到相关的cases信息
- APK反编译之APKTOOL的使用
- 栈的基本知识及应用
- 深度学习Python3.x前期的准备
- struts2的核心和工作原理
- hadoop服务器节点间免密通信图文过程