-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy path10018.cpp
More file actions
executable file
·86 lines (73 loc) · 1.84 KB
/
10018.cpp
File metadata and controls
executable file
·86 lines (73 loc) · 1.84 KB
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
/* Problem: Reverse and Add UVa 10018
Programmer: Md. Mahmud Ahsan
Compiled: Visual C++ 6.0
Date: 14-09-04
*/
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
typedef unsigned long int lint;
// namespace global for use all the function
namespace global{
bool palin;
int count;
char strFirst[100], strSecond[100], strSum[100];
lint iFirst, iSecond, iSum;
}
// prototypes
void reverseStr(char *one, char *two);
bool chkPalin(char *str);
void calculation(char *);
//=======================================================================
int main(){
char strInput[100];
int test;
cin >> test;
for (int t = 0; t < test; t++){
using namespace global;
// each time set main terms to default terms
palin = false;
count = 0;
iSum = iFirst = iSecond = 0;
cin >> strInput;
calculation(strInput); // calculate and print
}
return 0;
}
//=======================================================================
void reverseStr(char *one, char *two){
int h = strlen(one) - 1;
int i = 0;
for (int j = h; j >= 0; j--){
two[i] = one[j];
++i;
}
two[i] = '\0'; // must set to null at last for "two" second string
}
bool chkPalin(char *str){
int high = strlen(str) - 1;
int middle = strlen(str) / 2;
for (int low = 0; low < middle; low++){
if (str[low] != str[high - low])
return false;
}
return true;
}
void calculation(char *strFirst){
using namespace global;
if (chkPalin(strFirst)){
cout << "0" << " " << strFirst << endl;
return;
}
do{
count++;
reverseStr(strFirst, strSecond);
sscanf(strFirst, "%lu", &iFirst);
sscanf(strSecond, "%lu", &iSecond);
iSum = iFirst + iSecond;
sprintf(strSum, "%lu", iSum);
strcpy(strFirst, strSum);
}while (!chkPalin(strSum));
cout << count << " " << strSum << endl;
}