From b541e00bad61537bd0911b816ebfd66cc59c6f42 Mon Sep 17 00:00:00 2001 From: nihonium Date: Sun, 20 Feb 2022 17:32:35 +0300 Subject: [PATCH] any --- numbers/any.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 numbers/any.c diff --git a/numbers/any.c b/numbers/any.c new file mode 100644 index 0000000..8d0141e --- /dev/null +++ b/numbers/any.c @@ -0,0 +1,60 @@ +#include +#include +#include + +int to_dec(char *s, int n); +void to_n(int x, int n, char *res); +void print_reverse(char *s); + +int main() { + int n, k; + char A[1000]; + char res[1000] = ""; + scanf("%d\n%s\n%d", &n, A, &k); + if ((strlen(A) == 1) && (A[0] == '0')) { + printf("0\n"); + return 0; + } + n = to_dec(A, n); + to_n(n, k, res); + print_reverse(res); + return 0; +} + +void to_n(int x, int n, char *res) { + int a, b; + int i; + a = x; + for (i = 0; a > 0; ++i) { + b = a % n; + a = (int)(a / n); + if (b < 10) { + res[i] = '0' + b; + } + else { + res[i] = 'A' + b - 10; + } + } + res[i] = '\0'; +} + +int to_dec(char *s, int n) { + int len = strlen(s); + int res = 0; + for (int i = len - 1; i >= 0; --i) { + if (s[i] >= '0' && s[i] <= '9') { + res += (s[i] - '0') * pow(n, len-i-1); + } + else { + res += (s[i] - 'A' + 10) * pow(n, len-i-1); + } + } + return res; +} + +void print_reverse(char *s) { + for (int i = strlen(s) - 1; i >= 0; --i) { + printf("%c", s[i]); + } + putchar('\n'); +}