Two big numbers to multiply
来源:互联网 发布:软件性能指标描述 编辑:程序博客网 时间:2024/05/21 21:02
Description:
Input two numbers (A and B) which are overflow, then output answer (A * B).
My code is as follows:
// bigNumberToMultiply.cpp#include <stdio.h>#include <stdlib.h>#include <string.h>#include <conio.h>#include <time.h>#include <math.h>#define MAX_BIT 100 // The most bits is changed by changing MAX_BIT#define subtract48(a) a -= 48#define plus48(a) a += 48int main(){ // Initial arraies to store big number. char num1[MAX_BIT], num2[MAX_BIT], numTemp[MAX_BIT][MAX_BIT], numFinal[2 * MAX_BIT + 2]; memset(num1, 0, sizeof(num1)); memset(num2, 0, sizeof(num2)); memset(*numTemp, 0, sizeof(numTemp)); memset(numFinal, 0, sizeof(numFinal)); int i = 0, j = 0, k = 0; gets(num1); gets(num2); while (num1[i++]){ subtract48(num1[i - 1]); } while (num2[j++]){ subtract48(num2[j - 1]); } int bit1 = i - 2, bit2 = j - 2; // Multiply by each bit int more = 0; for (i = bit2; i >= 0; i--){ for (j = bit1; j >= 0; j--){ numTemp[bit2 - i][bit1 - j] = (num1[j] * num2[i] + more) % 10; more = (num1[j] * num2[i] + more) / 10; } numTemp[bit2 - i][bit1 - j] = more; more = 0; } // To get final answer to numFinal int maxBit, currentBit; //currentBit is for numFinal for (i = 0; i <= bit2; i++){ j = MAX_BIT; while (!numTemp[i][--j] && j >= 0); maxBit = j; if (maxBit != 0){ for (j = 0; j <= maxBit; j++){ currentBit = j + i; numFinal[currentBit] += numTemp[i][j]; while (numFinal[currentBit++] > 9){ numFinal[currentBit] += numFinal[currentBit - 1] / 10; numFinal[currentBit - 1] %= 10; } } } } i = 2 * MAX_BIT + 2; while (!numFinal[--i]); for (i; i >= 0; i--){ plus48(numFinal[i]); printf ("%c", numFinal[i]); } putchar ('\n'); system("pause"); return 0;}
Welcome to contact to me if you find bugs in this program.
My email: alanCode@aliyun.com
阅读全文
1 0
- Two big numbers to multiply
- Divide Two Numbers Without Symbol And Also Minus And Multiply
- multiply of big Number
- [LeetCode] Add Two Numbers、Divide Two Integers、Multiply Strings、Add Binary、Plus One
- UVa10464 - Big Big Real Numbers
- Two Numbers
- Two little classes to easily get pseudo-random numbers
- (链表) Add to List 2. Add Two Numbers
- [Leetcode] 2.Add Two Numbers(List To Long,模拟)
- I Love Big Numbers!
- HDOJ1018 Big Numbers
- C. Really Big Numbers
- codeforces817c Really Big Numbers
- C. Really Big Numbers
- 补题补题补题~~~~really big numbers
- 129. Sum Root to Leaf Numbers\43. Multiply Strings\494. Target Sum\98. Validate Binary Search Tree
- Add, Subtract, Multiply numbers without using "+", "-", "*" operation
- Two Sum & Add Two Numbers
- CentOS下SSH配置无密码登录
- 程序设计你不知道的几个原则
- ListView控件功能全解析
- mybatis入门
- 【Android学习笔记系列】AsyncTask的使用和介绍(获取网络图片与进度条实例)
- Two big numbers to multiply
- 安卓RecyclerView万能适配器之baserecyclerviewadapterhelper详解
- Linux开机启动程序详解
- CodeChef SnakeDown2017 E解题报告
- JAVA代码块非静态代码块与静态代码块构造函数比较
- Effective Objective-C 2.0 总结(四)
- 1066. 图像过滤(15)
- Maven私服nexus添加第三方JAR
- 2.linux内核模块