1. string add(string a, string b){
  2. int nlength;
  3. int diff;
  4. if (a.size() > b.size()){
  5. nlength = a.size();
  6. diff = a.size() - b.size();
  7. b.insert(b.begin(), diff, '');
  8. //cout << b << endl;
  9. }
  10. else{
  11. nlength = b.size();
  12. diff = b.size() - a.size();
  13. a.insert(a.begin(), diff, '');
  14. //cout << a << endl;
  15. }
  16. //cout << a << endl;
  17. //cout << b << endl;
  18. //cout << c << endl;
  19. int takeover = ;
  20. for (int i = nlength - ; i >= ; i--){
  21. int temp = a[i]-'' + b[i] - '' + takeover;
  22. cout << a[i] << " " << b[i] << endl;
  23. cout << temp << endl;
  24. if (temp >= ){
  25. takeover = ;
  26. b[i] = temp + '' - ;
  27.  
  28. //cout << c[j] << endl;
  29. }
  30. else{
  31. b[i] = temp + '';
  32. takeover = ;
  33.  
  34. }
  35. }
  36. //cout << takeover<<" " << j << endl;
  37. if (takeover == )b = '' + b;
  38. return b;
  39. }
  40.  
  41. void print(string str){
  42. int count = ;
  43. for (int i = ; i < str.size(); i++){
  44. if (str[i] == '')count++;
  45. else
  46. break;
  47. }
  48. cout << str.substr(count, str.size() - count) << endl;;
  49. }

注:char a='9';

int b=a-'0';

a的范围只能是0到9

better:

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

  1. string addStrings(string num1, string num2) {
  2. string res = "";
  3. int m = num1.size(), n = num2.size(), i = m - , j = n - , carry = ;
  4. while (i >= || j >= ) {
  5. int a = i >= ? num1[i--] - '' : ;
  6. int b = j >= ? num2[j--] - '' : ;
  7. int sum = a + b + carry;
  8. res.insert(res.begin(), sum % + '');
  9. carry = sum / ;
  10. }
  11. return carry ? "" + res : res;
  12. }

大数相乘

  1. class Solution {
  2. public:
  3. string multiply(string num1, string num2) {
  4. int n=num1.size();
  5. int m=num2.size();
  6. vector<int>temp(n+m,);//一个m位和n位数相乘得到的数最多是n+m位
  7. for(int i=n-;i>=;i--){
  8.  
  9. for(int j=m-;j>=;j--){
  10. temp[i+j+]+=(num1[i]-'')*(num2[j]-'');
  11. }
  12. }
  13. int bi=;
  14. for(int i=temp.size()-;i>=;i--){
  15. temp[i]=temp[i]+bi;
  16. bi=temp[i]/;
  17. temp[i]=temp[i]%;
  18. }
  19. int i = ;
  20. while (temp[i] == )i++;//跳过前面多余的0
  21. if(i>=temp.size())return "";
  22. string res="";
  23. for(;i<temp.size();i++){
  24. res+=temp[i]+'';
  25. }
  26.  
  27. return res;
  28. }
  29. };

打印从1到最大n位数

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. using namespace std;
  5.  
  6. bool stop(string &str){
  7. bool stop = false;
  8. int i = str.size() - ;
  9. int carry = ;
  10. while (i >= ){
  11. int tmp = str[i] - '' + carry;
  12. if (i == str.size() - )tmp++;
  13. if (tmp >=){
  14. if (i == ){
  15. stop = true;
  16. }
  17. else{
  18. str[i] = tmp % + '';
  19. carry = tmp / ;
  20. i--;
  21. }
  22.  
  23. }
  24. else{
  25. str[i] = tmp + '';
  26. break;
  27. }
  28. }
  29. return stop;
  30. }
  31.  
  32. void print(string &str){
  33. bool flag = false;
  34. string res;
  35. for (int i = ; i < str.size(); i++){
  36. if (str[i] == ''&&flag == false)continue;
  37. else if (str[i] != ''&&flag == false){
  38. flag = true;
  39. res.push_back(str[i]);
  40. }
  41. else{
  42. res.push_back(str[i]);
  43. }
  44. }
  45. cout << res << endl;
  46. }
  47. void PrintNumber(int n){
  48. string res(n,'');
  49. while (!stop(res)){
  50. print(res);
  51. }
  52. }
  53. int main(){
  54. int n = ;
  55. PrintNumber(n);
  56. system("pause");
  57. return ;
  58. }

大数相加和大数相乘以及打印从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-10-框架类 框架类(框架初始化类) 将原来入口文件中功能,放在该类中完成,入口文件变得简单,轻量! 将入口文件中的各个功能,由框架类的各个方法,完成: 为了简单化,使用纯静态的类.(看 ...

  2. gorm 实现 mysql for update 排他锁

    关于 MySQL 的排他锁网上已经有很多资料进行了介绍,这里主要是记录一下 gorm 如果使用排他锁. 排他锁是需要对索引进行锁操作,同时需要在事务中才能生效.具体操作如下: 假设有如下数据库表结构: ...

  3. Java doc注释

    常用Java注释标签(Java comment tags) @author 作者 适用范围:文件.类.方法 (多个作者使用多个@author标签标识,java doc中显示按输入时间顺序罗列.) 例: ...

  4. @Autowired 和 @Resource注解, 一个接口有多个实现类的时候Spring注入遇到的问题

    先说下我遇到的问题,有一个接口 CompensationService, 有两个实现类 MusicCompensationStrategyImpl  和  TakeDeliveryCompensati ...

  5. Centos 6.10 安装 Jenkins

    前言 持续集成的概念 持续集成,Continuous integration ,简称CI. 持续集成正是针对这一类问题的一种软件开发实践.它倡导团队开发成员必须经常集成他们的工作,甚至每天都可能发生多 ...

  6. Linux进程间通信—使用共享内存

    Linux进程间通信-使用共享内存 转自: https://blog.csdn.net/ljianhui/article/details/10253345 下面将讲解进程间通信的另一种方式,使用共享内 ...

  7. Spring Cloud Alibaba学习笔记(17) - Spring Cloud Gateway 自定义路由谓词工厂

    在前文中,我们介绍了Spring Cloud Gateway内置了一系列的路由谓词工厂,但是如果这些内置的路由谓词工厂不能满足业务需求的话,我们可以自定义路由谓词工厂来实现特定的需求. 例如有某个服务 ...

  8. Java内存模型——方法区

    方法区(Method Area) ①      对每个加载的类型,JVM必须在方法区中存储以下类信息: 1)        这个类型的完整有效名(类型信息) 类型名称在Java类文件和JVM中都以完整 ...

  9. java之struts2的配置讲解(2)

    在 java之struts框架入门教程 基础上,进行下列操作 1.结构对比 原来的项目结构图 现在的结构图 即从结构上可以看出,在HelloStruts项目中增加了config 文件夹(Source ...

  10. isolate两三事

    1.1. 第一步:创建并握手 如前所述,Isolate 不共享任何内存并通过消息进行交互,因此,我们需要找到一种方法在「调用者」与新的 isolate 之间建立通信. 每个 Isolate 都暴露了一 ...