-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode-numbers.c
176 lines (116 loc) · 4.85 KB
/
decode-numbers.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*[31 Points]
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Automated Grading Scheme:
Visible: 1 marks for each visible test case.
Hidden: 2 marks for 1, 2 and 3 test case.
Hidden: 3 marks for 7 and 8 test case.
Hidden: 4 marks for 4, 5, 6 and 9 test case.
Manually Grading Scheme:
Note: Give 0 marks for test-case component if there is any form of hard-coding. Eg: printf("0");
Penalty :
-20% for using strings, arrays, or array indexing to solve this problem,or any future concepts
-20% each for using any library function (string library) other than printf and scanf
-20% each for using any library other than stdio.h
---------------------------------------------------------------------
Decode Numbers
You were taught about the binary and hexadecimal representations in the lectures about the ASCII codes. It is also possible to have numbers represented with bases other than the powers of two.
Let us say that you want to represent a number with the base 17. Any integer can be uniquely represented as: an17n-1 + an-117n-2 + ... + a1*170, where for each ai, 0 <= ai < 17
In the hexadecimal representation, we represented 10 by A, 11 by B, ..., and 15 by F. Similarly, in our base 17 representation we can represent 1-15 as we did in the hexadecimal representation, and represent 16 by G.
Using this scheme we can represent any number to any base in the range 2-20 (even larger bases are possible, but we are going to restrict to 20). You are given a base and a number represented with the base. Your job is to convert the number to its decimal representation or print "INVALID" if it is not possible to do the same.
It may not always be possible to construct the decimal representation of a given number as it may contain characters, which should not be part of the representation. For instance, let us say that you are asked to convert "1012" base 2 to its decimal representation. It is not possible to do so as 2 is >= 2, and the representation of a number base k should contain characters from 0 to (k-1) only. Similarly, if you are asked to convert "ABC" base 12 to its decimal representation, it is not possible to do so as it contains C which represents 12, which is equal to the base. For such cases, you should print "INVALID".
You need not worry about integer overflow issues as the inputs are small enough to fit inside the integer datatype.
Constraints
You are not allowed to use strings, arrays, or array indexing to solve this problem, you can only scan the input character by character.
Input:
The first line contains an integer from 2-20 which is to be interpreted as the base of the number.
The second line contains the number of characters in the following representation.
The third line contains as many characters as given in the previous input. Each of these characters can be either uppercase English letters or numbers from 0-9.
Output:
Your program should print the decimal representation of the number given in the third line
Sample TestCases
Test Case 1
Input:
2
4
1010
Output:
10
Explanation: "1010" can be converted to its decimal representation as 1(2^3) + 0(2^2) + 1(2^1) + 0(2^0) = 8 + 0 + 2 + 0 = 10
Test Case 2
Input:
12
4
AB10
Output:
18876
Explanation: "AB10" can be converted to its decimal representation as 10(12^3) + 11(12^2) + 1(12^1) + 0(12^0) = 17280 + 1584 + 12 + 0 = 18876
Test Case 3
Input:
17
4
ABHE
Output:
INVALID
Explanation: "ABHE" contains the character H, which expands to 17 in the scheme described above, and it cannot be in a number represented to the base 17.*/
// solution:
#include <stdio.h>
int main()
{
int b;
int n;
char c;
int decimal = 0;
int flag = 0;
scanf("%d", &b);
scanf("%d", &n);
getchar();
for (int i = 1; i <= n; i++)
{
scanf("%c", &c);
if (c <= '9' && c >= '0')
{
if (c - 48 >= b)
{
flag = 1;
break;
}
else
{
int p = (c - 48);
for (int count = 1; count <= n - i; count++)
{
p = p * b;
}
decimal = decimal + p;
}
}
else if (c >= 'A' && c <= 'Z')
{
if (c - 55 >= b)
{
flag = 1;
break;
}
else
{
int q = 1;
int g = c - 55;
for (int count = 1; count <= n - i; count++)
{
q = q * b;
}
q = q * g;
decimal = decimal + q;
}
}
}
if (flag == 0)
{
printf("%d", decimal);
}
else if (flag == 1)
{
printf("INVALID");
}
return 0;
}