Kid Koder Posted July 23, 2014 Report Share Posted July 23, 2014 "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; } _______________________________________________________________________________ 1 Quote Daca iti iese un program din prima, inseamna ca ceva e gresit... Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.