#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
typedef unsigned long int ul;
typedef string s;
s generujZera(ul d);
#define REP(x, y, z) for(ul z = x; z < y; z++)
#define REPE(x, y, z) for(ul z = x; z <= y; z++)
int main()
{
ul ilosc_danych, int_bufor, wynik = 0;
s str_bufor;
scanf("%lu", &ilosc_danych);
s* str_dane = new s[ilosc_danych];
ul* int_dane = new ul[ilosc_danych];
REP(0, ilosc_danych, i)
{
scanf("%lu", &int_bufor);
int_dane[i] = int_bufor;
str_dane[i] = to_string(int_bufor);
}
REP(1, ilosc_danych, i)
{
if (int_dane[i - 1] >= int_dane[i])
{
int_bufor = atoi(str_dane[i - 1].substr(0, str_dane[i].length()).c_str());
if (int_bufor > int_dane[i]) //Dodanie tylu zer, aby dlugosc rozpatrywanej liczby byla o 1 wieksza od dlugosci poprzedniej
{
wynik += (str_dane[i - 1].length() - str_dane[i].length() + 1);
str_dane[i] += generujZera((str_dane[i - 1].length() - str_dane[i].length() + 1));
int_dane[i] = atoi(str_dane[i].c_str());
}
else if (int_bufor == int_dane[i])
{
ul ilosc_dziewiatek = 0;
REP(str_dane[i].length(), str_dane[i - 1].length(), j)
{
str_bufor = str_dane[i - 1].substr(j, 1);
if (str_bufor == "9")
++ilosc_dziewiatek;
}
if ((ilosc_dziewiatek == (str_dane[i - 1].length() - str_dane[i].length() + 1)) || (str_dane[i - 1].length() == str_dane[i].length())) //Dodanie tylu zer, aby dlugosc rozpatrywanej liczby byla o 1 wieksza od dlugosci poprzedniej
{
wynik += (str_dane[i - 1].length() - str_dane[i].length() + 1);
str_dane[i] += generujZera((str_dane[i - 1].length() - str_dane[i].length() + 1));
int_dane[i] = atoi(str_dane[i].c_str());
}
else //Dodanie tylu cyfr, aby rozpatrywana wartosc byla o jeden wieksza od poprzedniej
{
wynik += (str_dane[i - 1].length() - str_dane[i].length());
int_dane[i] = int_dane[i - 1] + 1;
str_dane[i] = to_string(int_dane[i]);
}
}
else //Dodanie tylu zer, aby dlugosc rozpatrywanej liczby byla rowna dlugosci poprzedniej
{
wynik += (str_dane[i - 1].length() - str_dane[i].length());
str_dane[i] += generujZera((str_dane[i - 1].length() - str_dane[i].length()));
int_dane[i] = atoi(str_dane[i].c_str());
}
}
}
printf("%lu", wynik);
}
s generujZera(ul d)
{
s bufor = "";
REP(0, d, i)
bufor += "0";
return bufor;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <iostream> #include <cstdio> #include <string> using namespace std; typedef unsigned long int ul; typedef string s; s generujZera(ul d); #define REP(x, y, z) for(ul z = x; z < y; z++) #define REPE(x, y, z) for(ul z = x; z <= y; z++) int main() { ul ilosc_danych, int_bufor, wynik = 0; s str_bufor; scanf("%lu", &ilosc_danych); s* str_dane = new s[ilosc_danych]; ul* int_dane = new ul[ilosc_danych]; REP(0, ilosc_danych, i) { scanf("%lu", &int_bufor); int_dane[i] = int_bufor; str_dane[i] = to_string(int_bufor); } REP(1, ilosc_danych, i) { if (int_dane[i - 1] >= int_dane[i]) { int_bufor = atoi(str_dane[i - 1].substr(0, str_dane[i].length()).c_str()); if (int_bufor > int_dane[i]) //Dodanie tylu zer, aby dlugosc rozpatrywanej liczby byla o 1 wieksza od dlugosci poprzedniej { wynik += (str_dane[i - 1].length() - str_dane[i].length() + 1); str_dane[i] += generujZera((str_dane[i - 1].length() - str_dane[i].length() + 1)); int_dane[i] = atoi(str_dane[i].c_str()); } else if (int_bufor == int_dane[i]) { ul ilosc_dziewiatek = 0; REP(str_dane[i].length(), str_dane[i - 1].length(), j) { str_bufor = str_dane[i - 1].substr(j, 1); if (str_bufor == "9") ++ilosc_dziewiatek; } if ((ilosc_dziewiatek == (str_dane[i - 1].length() - str_dane[i].length() + 1)) || (str_dane[i - 1].length() == str_dane[i].length())) //Dodanie tylu zer, aby dlugosc rozpatrywanej liczby byla o 1 wieksza od dlugosci poprzedniej { wynik += (str_dane[i - 1].length() - str_dane[i].length() + 1); str_dane[i] += generujZera((str_dane[i - 1].length() - str_dane[i].length() + 1)); int_dane[i] = atoi(str_dane[i].c_str()); } else //Dodanie tylu cyfr, aby rozpatrywana wartosc byla o jeden wieksza od poprzedniej { wynik += (str_dane[i - 1].length() - str_dane[i].length()); int_dane[i] = int_dane[i - 1] + 1; str_dane[i] = to_string(int_dane[i]); } } else //Dodanie tylu zer, aby dlugosc rozpatrywanej liczby byla rowna dlugosci poprzedniej { wynik += (str_dane[i - 1].length() - str_dane[i].length()); str_dane[i] += generujZera((str_dane[i - 1].length() - str_dane[i].length())); int_dane[i] = atoi(str_dane[i].c_str()); } } } printf("%lu", wynik); } s generujZera(ul d) { s bufor = ""; REP(0, d, i) bufor += "0"; return bufor; } |
English