string add(string a, string b){
int nlength;
int diff;
if (a.size() > b.size()){
nlength = a.size();
diff = a.size() - b.size();
b.insert(b.begin(), diff, '');
//cout << b << endl;
}
else{
nlength = b.size();
diff = b.size() - a.size();
a.insert(a.begin(), diff, '');
//cout << a << endl;
}
//cout << a << endl;
//cout << b << endl;
//cout << c << endl;
int takeover = ;
for (int i = nlength - ; i >= ; i--){
int temp = a[i]-'' + b[i] - '' + takeover;
cout << a[i] << " " << b[i] << endl;
cout << temp << endl;
if (temp >= ){
takeover = ;
b[i] = temp + '' - ; //cout << c[j] << endl;
}
else{
b[i] = temp + '';
takeover = ; }
}
//cout << takeover<<" " << j << endl;
if (takeover == )b = '' + b;
return b;
} void print(string str){
int count = ;
for (int i = ; i < str.size(); i++){
if (str[i] == '')count++;
else
break;
}
cout << str.substr(count, str.size() - count) << endl;;
}

注:char a='9';

int b=a-'0';

a的范围只能是0到9

better:

一位一位相加,然后算和算进位,最后根据进位情况看需不需要补一个高位

string addStrings(string num1, string num2) {
string res = "";
int m = num1.size(), n = num2.size(), i = m - , j = n - , carry = ;
while (i >= || j >= ) {
int a = i >= ? num1[i--] - '' : ;
int b = j >= ? num2[j--] - '' : ;
int sum = a + b + carry;
res.insert(res.begin(), sum % + '');
carry = sum / ;
}
return carry ? "" + res : res;
}

大数相乘

class Solution {
public:
string multiply(string num1, string num2) {
int n=num1.size();
int m=num2.size();
vector<int>temp(n+m,);//一个m位和n位数相乘得到的数最多是n+m位
for(int i=n-;i>=;i--){ for(int j=m-;j>=;j--){
temp[i+j+]+=(num1[i]-'')*(num2[j]-'');
}
}
int bi=;
for(int i=temp.size()-;i>=;i--){
temp[i]=temp[i]+bi;
bi=temp[i]/;
temp[i]=temp[i]%;
}
int i = ;
while (temp[i] == )i++;//跳过前面多余的0
if(i>=temp.size())return "";
string res="";
for(;i<temp.size();i++){
res+=temp[i]+'';
} return res;
}
};

打印从1到最大n位数

#include<iostream>
#include<string>
#include<vector>
using namespace std; bool stop(string &str){
bool stop = false;
int i = str.size() - ;
int carry = ;
while (i >= ){
int tmp = str[i] - '' + carry;
if (i == str.size() - )tmp++;
if (tmp >=){
if (i == ){
stop = true;
}
else{
str[i] = tmp % + '';
carry = tmp / ;
i--;
} }
else{
str[i] = tmp + '';
break;
}
}
return stop;
} void print(string &str){
bool flag = false;
string res;
for (int i = ; i < str.size(); i++){
if (str[i] == ''&&flag == false)continue;
else if (str[i] != ''&&flag == false){
flag = true;
res.push_back(str[i]);
}
else{
res.push_back(str[i]);
}
}
cout << res << endl;
}
void PrintNumber(int n){
string res(n,'');
while (!stop(res)){
print(res);
}
}
int main(){
int n = ;
PrintNumber(n);
system("pause");
return ;
}

大数相加和大数相乘以及打印从1到最大的n位数的更多相关文章

  1. Linux C/C++ 编程练手 --- 大数相加和大数相乘

    最近写了一个大数相乘和相加的程序,结果看起来是对的.不过期间的效率可能不是最好的,有些地方也是临时为了解决问题而直接写出来的. 可以大概说一下相乘和相加的解决思路(当然,大数操作基本就是两个字符串的操 ...

  2. Java 大数相乘、大数相加、大数相减

    思路来源:: https://blog.csdn.net/lichong_87/article/details/6860329 /** * @date 2018/6/22 * @description ...

  3. HDU 1316 (斐波那契数列,大数相加,大数比较大小)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1316 Recall the definition of the Fibonacci numbers: ...

  4. 代码题(59)— 字符串相加、字符串相乘、打印最大n位数

    1.415. 字符串相加 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 思路:和链表相加类似,求进位. class Solution { public: string addS ...

  5. Java实现大数相加、相乘(不使用BigInteger)

    大数相加: package algorithm; //使用BigInteger类验证 import java.math.BigInteger; public class BigAdd { public ...

  6. 求解Catalan数,(大数相乘,大数相除,大数相加)

    Catalan数 卡塔兰数是组合数学中一个常在各种计数问题中出现的数列.以比利时的数学家欧仁·查理·卡塔兰(1814–1894)命名.历史上,清代数学家明安图(1692年-1763年)在其<割圜 ...

  7. A+B and A*B problem 大数相加 相乘 模拟

    A+B and A*B problem 大数相加 相乘 模拟 题意 给你两个数a和b,这两个数很大,然后输出这两个数相加的和,相乘的积. 解题思路 模拟,但是还是搜了搜代码实现,发现这个大佬写的是真的 ...

  8. hdu acm-1047 Integer Inquiry(大数相加)

    Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

随机推荐

  1. php_mvc实现步骤六

    shop34-1-目录布局 存在项目的根目录 shop34 框架代码与应用程序代码划分 框架代码: 在各个应用程序间可以通用的代码. 应用程序代码: 当前项目的业务逻辑实现的代码. 分别创建两个子目录 ...

  2. 【LeetCode】整数反转【不能借助辅助空间,需要处理溢出】

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: ...

  3. python函数知识五 推导式和内置函数一(了解)

    17.推导式: 推导式:将for循环多行变成一行 list推导式:[] #普通模式 print([i for i in range(20)]) #循环模式 #[变量 for i in range(20 ...

  4. 第13章 Salesforce Lightning应用程序

    13.1 Lightning应用程序 13.1.1 什么是闪电应用程序 Salesforce应用程序有两种风格:经典应用程序和闪电应用程序.经典应用程序在Salesforce Classic中创建和管 ...

  5. tidb测试环境搭建

    tidb ansible 部署方式环境检查过于严格,测试环境往往达不到标准,需调整一些参数才能部署成功. 基于tidb2.0版本需要调整的参数 [tidb@ansible01 tidb-ansible ...

  6. IDEA远程DEBUG Tomcat配置

    IDEA远程DEBUG Tomcat配置 IDEA远程DEBUG Tomcat很简单,配置如下: 1.修改tomcat服务器配置 打开tomcat/bin/catalina.sh 在空白处添加如下参数 ...

  7. kafka和zookeeper安装部署(版本弄不好就是坑)

    yum install -y unzip zip 配置host vi /etc/host172.19.68.10 zk1 1. zookeeper zookeeper下载地址 http://mirro ...

  8. tsql获取sqlserver某个库下所有表

    ) declare my_cursor cursor for (select [name] from SysObjects where XType='U') open my_cursor fetch ...

  9. java ArithUtil 数据计算精度工具

    ArithUtil: /** * 如果需要精确计算,非要用String来够造BigDecimal不可 */ package com.leaniot.securitymonitor.util; impo ...

  10. 修复win10系统的引导

    上周末时,在安装完linux后,进入win10系统后,想做个系统的引导菜单,用了easyBCD,后来一不小心删除了win10的引导菜单(window boot manager). 这样,就造成了我wi ...