Jump to content

Recommended Posts

Posted

"Se dau doua numere cu maxim 100 de cifre fiecare. Sa se afiseze suma acestora."

 

Deci, din prima ne putem da seama ca numerele nu pot fi retinute intr-o singura variabila. Asa ca vom retine fiecare numar intr-un vector, o celula reprezentand o cifra a sa. Acest vector va fi pus invers. De exemplu:

 

Numarul 123456789 va fi retinut intr-un vector V in felul urmator:

 

V| 9 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 0 | ....

i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10|11| ....

 

unde V[0] = numarul de cifre ale numarului.

 

 

Programul va arata in felul urmator:

/* Author : Kid Kode
   Create Date: 23-Jul-14, 12:13
   Language: C++
   Description: Big Numbers ("+")
*/


#include <fstream>
#include <cstring>
#include <algorithm>


using namespace std;


#define DIM 102


ifstream fin("date.in");
ofstream fout("date.out");


int a[DIM], b[DIM], i, lg, cat, rez[DIM];
char s[DIM];


int main() {
    fin.get(s, 102);


    lg = strlen(s);


    for(i = lg - 1;i >= 0;i--) {
        a[++a[0]] = s[i] - '0';
    }


    fin.get();


    fin.get(s, 102);


    lg = strlen(s);


    for(i = lg - 1;i >= 0;i--) {
        b[++b[0]] = s[i] - '0';
    }


    cat = 0;
    i = 1;
    while(i <= a[0] || i <= b[0] || cat != 0) {
        rez[i] = a[i] + b[i] + cat;
        cat = rez[i] / 10;
        rez[i] %= 10;
        rez[0] = max(rez[0], i);
        i++;
    }


    for(i = rez[0];i >= 1;i--) {
        fout << rez[i];
    }


    fout << 'n';


    fin.close();
    fout.close();


    return 0;
}

_______________________________________________________________________________

  • Upvote 1

Daca iti iese un program din prima, inseamna ca ceva e gresit...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.