zoukankan      html  css  js  c++  java
  • 算法笔记-----大整数相+------数组---效率

      1 //
      2 // Created by alim on 2017/12/23.
      3 //
      4 
      5 
      6 #include <stdlib.h>
      7 #include <cstring>
      8 #include <iostream>
      9 using namespace std;
     10 
     11 #define M 100
     12 
     13 char sa[1000];
     14 char sb[1000];
     15 
     16 typedef struct _Node{
     17     int s[M];//数字
     18     int l;//数字长度
     19 }Node,*pNode;
     20 
     21 
     22 void dzs_add(pNode pa,pNode pb,pNode result){
     23     int i, cc;
     24     pNode temp;
     25 
     26     if (pa->l < pb->l) {
     27         temp = pb;
     28         pb = pa;
     29         pa = temp;
     30     }
     31 
     32     for (int j = 0; j < pa->l; ++j) {
     33         result->s[j] = pa->s[j];
     34     }
     35     result->l = pa->l;
     36 
     37     cc = 0;
     38     for (i = 0; i < pb->l; ++i) {
     39         result->s[i] = (pa->s[i] + pb->s[i] + cc) % 10;
     40         cc = (pa->s[i] + pb->s[i]) / 10;
     41     }
     42     if (cc != 0) {
     43         result->s[i] = result->s[i]+cc;
     44     }
     45     if (i >= result->l) {
     46         result->l = i+1;
     47     }
     48 
     49     return;
     50 }
     51 
     52 void dzs_jian(pNode pa,pNode pb,pNode result){
     53     int i, cc;
     54     pNode temp;
     55 
     56     if (pa->l < pb->l) {
     57         temp = pb;
     58         pb = pa;
     59         pa = temp;
     60     }
     61     for (int j = 0; j < pa->l; ++j) {
     62         result->s[j] = pa->s[j];
     63     }
     64     result->l = pa->l;
     65     cc = 0;
     66     for (i = 0; i < pb->l; ++i) {
     67         if (pa->s[i] >= pb->s[i]) {
     68             result->s[i] = pa->s[i] - pb->s[i];
     69         } else {
     70             result->s[i + 1] -= 1;
     71             result->s[i] = pa->s[i] + 10 - pb->s[i];
     72         }
     73     }
     74     if (i >= result->l) {
     75         result->l = i+1;
     76     }
     77 }
     78 int main() {
     79     Node ans,a,b;
     80     cout << "请输入大整数 a:"<<endl;
     81     cin >> sa;
     82     cout << "请输入大整数 b:" << endl;
     83     cin >> sb;
     84     a.l = strlen(sa);//以字符串进行处理,计算长度
     85     b.l = strlen(sb);
     86 
     87     //倒向存储
     88     int z=0,i;
     89     for (int i = a.l-1; i >= 0; i--) {
     90         a.s[z++] = sa[i]-'0';
     91     }
     92 
     93     z=0;
     94     for (int i = b.l-1; i >= 0; i--) {
     95         b.s[z++] = sb[i] - '0';
     96     }
     97 //    dzs_add(&a, &b, &ans);
     98     dzs_jian(&a, &b, &ans);
     99     cout << "最终结果为:
    ";
    100     for (i = ans.l-1; i >= 0; i--) {
    101         cout<<ans.s[i];
    102     }
    103 
    104     cout << endl;
    105     return 0;
    106 }
  • 相关阅读:
    SpringSecurity03 基于内存验证
    SpringSecurity02 表单登录、SpringSecurity配置类
    SpringSecurity01 SpringSecurity环境搭建
    密码的加密和匹配
    SpringBoot05 数据操作03 -> JPA查询方法的规则定义
    SpringBoot05 数据操作02 -> JPA接口详解
    SpringBoot05 数据操作01 -> JPA的基本使用、基本使用02
    linux中目录操作<1>
    linux以行为单位进行读写操作
    linux以字符为单位进行读写操作
  • 原文地址:https://www.cnblogs.com/alimjan/p/8097593.html
Copyright © 2011-2022 走看看