Cod sursa(job #1982135)

Utilizator YusyBoss04Fares Yusuf YusyBoss04 Data 17 mai 2017 19:01:11
Problema Next Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <fstream>
#include <algorithm>
#include <cstring>
using namespace std;
ifstream cin ("next.in");
ofstream cout ("next.out");
const int NMAX = 1000000;
char a[NMAX + 5], c [NMAX + 5];
int main()
{
    char ch;
    ch = cin.get ();
    while (ch != '\n') {
        if (ch == ' ') {
            ch = cin.get();
            continue;
        }
        else {
        a[++a[0]] = ch - '0';
        ch = cin.get();
        }
    }
    reverse (a + 1, a + a[0] + 1);
    int i;
    long long k, r, aux;
    cin >> k;
    memset(c, 0, sizeof(c));
    r = 0;
    for (i = a[0];i >= 1; i--) {
        r = r * 10 + a[i];
        c [++ c[0]] = r / k;
        r %= k;
    }
    reverse (c + 1, c + c[0] + 1);
    while (c[c[0]] == 0 && c[0] > 1)
        c[0]--;
    int tr = 0;
    c[1]++;
    if (c[1] > 9) {
        tr = 1;
        c[1] = c[1] % 10;
    }
    for (i = 2; i <= c[0]; i++) {
        aux = c[i] + tr;
        c[i] = aux % 10;
        tr = aux / 10;
    }
    if (tr)
        c[++c[0]] = tr;
        long long tra1 = 0;
        for (i = 1; i <= c[0]; i++) {
            aux = c[i] * k + tr;
            c[i] = aux % 10;
            tra1 = aux / 10;
        }
        while (tra1) {
            c[++c[0]] = tra1 % 10;
            tra1 /= 10;
        }
    for (i = c[0]; i >= 1; i--)
        cout <<(int) c[i];
    return 0;
}