Last active
December 25, 2018 01:37
-
-
Save AnirudhDagar/6aac9a15e6072aa9eac5f8be57caf352 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////https://www.spoj.com/problems/JULKA/ | |
#include <iostream> | |
#include <cstring> | |
using namespace std; | |
int main() | |
{ | |
int t=10; | |
int size = 105; | |
while (t--) | |
{ | |
char app_together[size], app_more[size], app_together_untouched[size]; | |
cin>>app_together; | |
cin>>app_more; | |
for(int i=0; i<size; i++) | |
{ | |
app_together_untouched[i] = app_together[i]; | |
} | |
int len_app_together = strlen(app_together); | |
int len_app_more = strlen(app_more); | |
char ans[size]; | |
ans[len_app_together] = '\0'; | |
int flag = 1; | |
for (int i=len_app_together-1; i>=0; i--) | |
{ | |
if (flag) | |
{ | |
for (int j=len_app_more-1; j>=0; j--) | |
{ | |
char a = app_together[i]; | |
char b = app_more[j]; | |
if(a >= b) | |
{ | |
ans[i] = 48 + a - b; | |
} | |
if(a < b) | |
{ | |
ans[i] = 48 + (10 + a - b); | |
app_together[i-1] -= 1; | |
} | |
i--; | |
} | |
} | |
flag = 0; | |
/*CAUTION: Index may get out of bounds here when i=-1*/ | |
if (i>-1 && (int)app_together[i] + 1 == 48) | |
{ | |
ans[i] = app_together[i] + 10; | |
app_together[i-1] -= 1; | |
} | |
if (i>-1 && (int)app_together[i] >= 48) | |
{ | |
ans[i] = app_together[i]; | |
} | |
} | |
int len_ans = strlen(ans); | |
//Finding the final answer for Natalia below by diving ans by 2. | |
char final_ans[size]; | |
final_ans[len_ans] = '\0'; | |
int next_step_add = 0; | |
for (int i=0 ; i < len_ans; i++) | |
{ | |
int x = ans[i]; | |
x = x-48+next_step_add; | |
if(x>=2) | |
{ | |
if (x%2 == 0) | |
{ | |
final_ans[i] = x/2 + 48; | |
next_step_add = 0; | |
} | |
else | |
{ | |
final_ans[i] = x/2 + 48; | |
next_step_add=10; | |
continue; | |
} | |
} | |
else | |
{ | |
final_ans[i] = x/2 + 48; | |
if (x%2 == 0) | |
{ | |
next_step_add = 0; | |
} | |
else | |
{ | |
next_step_add = 10; | |
} | |
} | |
} | |
int len_final_ans = strlen(final_ans); | |
char final_ans_mod[size]; | |
int flag_for_zeros_before_num = 1; | |
int counter =0; | |
for(int i=0; i<=len_final_ans; i++) | |
{ | |
if (final_ans[i] == '0' && flag_for_zeros_before_num) | |
{ | |
counter++; | |
continue; | |
} | |
else | |
{ | |
final_ans_mod[i-counter] = final_ans[i]; | |
flag_for_zeros_before_num = 0; | |
} | |
} | |
/*Above I've saved Final Answer for natalia in final_ans_mod. Later, in the last line I'll print it*/ | |
//Finding the final answer for Klaudia below by subtracting natalias ans from app_together. | |
char final_ans_klaudia[size]; | |
int len_final_ans_mod = strlen(final_ans_mod); | |
int len_app_together_untouched = strlen(app_together_untouched); | |
final_ans_klaudia[len_app_together_untouched] = '\0'; | |
flag = 1; | |
for (int i=len_app_together_untouched-1; i>=0; i--) | |
{ | |
if (flag) | |
{ | |
for (int j=len_final_ans_mod-1; j>=0; j--) | |
{ | |
char a = app_together_untouched[i]; | |
char b = final_ans_mod[j]; | |
if(a >= b) | |
{ | |
final_ans_klaudia[i] = 48 + a - b; | |
} | |
if(a < b) | |
{ | |
final_ans_klaudia[i] = 48 + (10 + a - b); | |
app_together_untouched[i-1] -= 1; | |
} | |
i--; | |
} | |
} | |
flag = 0; | |
if (i>-1 && (int)app_together_untouched[i] + 1 == 48) | |
{ | |
final_ans_klaudia[i] = app_together[i] + 10; | |
app_together_untouched[i-1] -= 1; | |
} | |
if (i>-1 && (int)app_together_untouched[i] >= 48) | |
{ | |
final_ans_klaudia[i] = app_together_untouched[i]; | |
} | |
} | |
int len_final_ans_klaudia = strlen(final_ans_klaudia); | |
char final_ans_klaudia_mod[size]; | |
flag_for_zeros_before_num = 1; | |
counter = 0; | |
for(int i=0; i<=len_final_ans_klaudia; i++) | |
{ | |
if (final_ans_klaudia[i] == '0' && flag_for_zeros_before_num) | |
{ | |
counter++; | |
continue; | |
} | |
else | |
{ | |
final_ans_klaudia_mod[i-counter] = final_ans_klaudia[i]; | |
flag_for_zeros_before_num = 0; | |
} | |
} | |
cout<<final_ans_klaudia_mod<<"\n"; | |
cout<<final_ans_mod<<"\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/garganshul108/b82e137099c1db7726d044e95f802800