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. Windows下mysql安装配置问题

    下载最新版的mysql: https://dev.mysql.com/downloads/mysql/ 下载完成后解压打开安装包如下 打开cmd以管理员身份运行(一定要以管理员身份运行) 然后输入命令 ...

  2. python 之网络并发(非阻塞IO模型)

    实现gevent模块 服务端: from socket import * import time s = socket() s.bind(('127.0.0.1',8080)) s.listen(5) ...

  3. doDBA工具使用详解

    目录 1.简介 2.下载 3.使用帮助 4.配置 4.1.模板 4.2.启动命令 5.部署流程 5.1.下载 5.2.选定被监控主机 5.3.在被监控主机上添加Linux用户.MySQL 用户 5.4 ...

  4. python基础 — 参数组合

    参数组合 >>>def f1(a, b, c=0, *args, **kw): ... print('a =', a, 'b =', b, 'c =', c, 'args =', a ...

  5. 隐藏GridControl的“Drag a column header here to group by that column”

    打开设计器,找到OptionsView,往下拉设置showGroupPanel为false

  6. 【LEETCODE】59、数组分类,适中级别,题目:39、48、64

    package y2019.Algorithm.array.medium; import java.util.*; /** * @ProjectName: cutter-point * @Packag ...

  7. response letter

    1.Firstly, we would like to thank you for your kind letter and for reviewers’ constructive commentsc ...

  8. easyui-datagrid清空表中原有数据

    $('#dg').datagrid('loadData', { total: 0, rows: [] });

  9. .net core使用ocelot---第八篇 Consul

    简介 .net core使用ocelot---第一篇 简单使用   .net core使用ocelot---第二篇 身份验证使用  .net core使用ocelot---第三篇 日志记录  .net ...

  10. web API .net - .net core 对比学习-依赖注入

    今天我们来看一下 .net web api 和 .net core web api依赖注入机制的差异. 首先我们分别在.net web api 和 .net core web api新建文件夹Serv ...