Last active
August 29, 2015 13:57
-
-
Save shanehou/9891622 to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
int number_of_digits(int num) { | |
int count = 0; | |
for (count = 0; num != 0; count++, num /= 10); | |
return count; | |
} | |
int highest_digit(int num, int nod) { | |
/* | |
while (num > 9) { | |
num /= 10; | |
} | |
return num; | |
*/ | |
static const int powers_of_10[33] = { | |
1000000000, 1000000000, | |
100000000, 100000000, 100000000, | |
10000000, 10000000, 10000000, | |
1000000, 1000000, 1000000, 1000000, | |
100000, 100000, 100000, | |
10000, 10000, 10000, | |
1000, 1000, 1000, 1000, | |
100, 100, 100, | |
10, 10, 10, | |
1, 1, 1, 1, 1 | |
}; | |
int leading_zeros = __builtin_clz(num); | |
num /= powers_of_10[leading_zeros]; | |
return num >= 10 ? 1 : num; | |
} | |
bool is_nineseries(int num) { | |
int nod = number_of_digits(num); | |
int p1_nod = number_of_digits(num + 1); | |
int hd = highest_digit(num, nod); | |
int p1_hd = highest_digit(num+1, p1_nod); | |
if ((nod+1 == p1_nod) || (hd+1 == p1_hd) && (nod > 1)) { | |
return true; | |
} | |
return false; | |
} | |
int generate_int_by_nod(int hd, int nod) { | |
int i = 1; | |
for (i = 1; i < nod; i++) { | |
hd *= 10; | |
} | |
return hd; | |
} | |
void output_nineseries(int hd, int nod) { | |
printf("%d", hd); | |
while (--nod > 0) { | |
printf("%d", 9); | |
} | |
} | |
void find_nineseries(int a, int b) { | |
int nod_a = number_of_digits(a); | |
int nod_b = number_of_digits(b); | |
int hd_a = highest_digit(a, nod_a); | |
int hd_b = highest_digit(b, nod_b); | |
if (is_nineseries(b)) { | |
output_nineseries(hd_b, nod_b); | |
} else { | |
if (nod_a == nod_b) { | |
if (hd_a == hd_b) { | |
output_nineseries(hd_b, 1); | |
int rcsv_a = a - generate_int_by_nod(hd_a, nod_a); | |
int rcsv_b = b - generate_int_by_nod(hd_b, nod_b); | |
int gap = nod_b - number_of_digits(rcsv_b); | |
while (gap-- > 1) { | |
output_nineseries(0, 1); | |
} | |
find_nineseries(rcsv_a, rcsv_b); | |
} else { | |
output_nineseries(hd_b-1, nod_b); | |
} | |
} else { | |
if (nod_b == 1) { | |
output_nineseries(b, 1); | |
} else { | |
if (hd_b > 1) { | |
output_nineseries(hd_b-1, nod_b); | |
} else { | |
output_nineseries(9, nod_b-1); | |
} | |
} | |
} | |
} | |
} | |
int main(int argc, char *argv[]) { | |
int a, b; | |
if (argc == 1) { | |
int i = 0, n = 0; | |
scanf("%d", &n); | |
for (i = 0; i < n; i++) { | |
scanf("%d %d", &a, &b); | |
find_nineseries(a, b); | |
printf("\n"); | |
} | |
} else if (argc == 3) { | |
a = atoi(argv[1]); | |
b = atoi(argv[2]); | |
if (a < 1 || b > 1000000 || a > b) { | |
printf("Error: 1 <= a < b <= 1000000\n"); | |
return -1; | |
} | |
find_nineseries(a, b); | |
printf("\n"); | |
} else { | |
printf("Error: Please input a and b.\n"); | |
return -1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment