-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse.c
67 lines (63 loc) · 1.07 KB
/
reverse.c
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
#include <stdio.h>
unsigned long merge2(unsigned int a, unsigned int b);
unsigned long reverse2(unsigned int x);
unsigned int length2(int num);
unsigned long reverse2(unsigned int x){
long num = 0;
while (x != 0)
{
num *= 10;
num += x % 10;
x /= 10;
}
return num;
}
unsigned int length2(int num){
int counter = 0;
while (num!=0)
{
counter++;
num /= 10;
}
return counter;
}
unsigned long merge2(unsigned int a, unsigned int b){
int countA = length2(a), countB = length2(b);
a = a * 10 + 1;
b = b * 10 + 1;
long reverseNum=0,revA = reverse2(a), revB = reverse2(b);
while (revA != 0 || revB != 0)
{
if (countA > 0)
{
if (revA != 0){
reverseNum *= 10;
reverseNum += revA % 10;
countA--;
revA /= 10;
}
}
else{
revA = 0;
//printf("revA ==0\n");
}
if (countB > 0)
{
if (revB != 0){
reverseNum *= 10;
reverseNum += revB % 10;
countB--;
revB /= 10;
}
}
else{
revB = 0;
//printf("revB ==0\n");
}
}
return reverseNum;
}
void mainReverseNew(){
printf("%ld\n",merge2(4158,10000));
system("pause");
}