diff --git a/hundred_chicken.c b/hundred_chicken.c new file mode 100644 index 0000000..afd83c5 --- /dev/null +++ b/hundred_chicken.c @@ -0,0 +1,34 @@ +#include + +/* + * 三种鸡单价分别为 R, S, 1/T,其中 R > S + * 花 M 钱买 N 只 + */ +void calc(int R, int S, int T, int M, int N) +{ + int v = 0; + int r = 1; + for (; r < N && r * R < M; r++) { + int m = M - R * r; + int n = N - r; + if (m >= n * S || m * T <= n) continue; + + int a = T * m - n; + int b = T * (S * n - m); + int c = S * T - 1; + if (a < 0 || b < 0 || a % c || b % c) continue; + + int s = a / c; + int t = b / c; + printf("----- %d -----\n", ++v); + printf("%d + %d + %d = %d\n", r, s, t, N); + printf("%d * %d + %d * %d + %d / %d = %d\n", r, R, s, S, t, T, M); + } +} + + +int main() +{ + calc(5, 3, 3, 100, 100); + return 0; +} diff --git a/number_to_pinyin.c b/number_to_pinyin.c new file mode 100644 index 0000000..2333742 --- /dev/null +++ b/number_to_pinyin.c @@ -0,0 +1,31 @@ +#include + +char *map[] = { + "ling", "yi", "er", "san", "si", + "wu", "liu", "qi", "ba", "jiu" +}; + +void display_num_with_pinyin(int num) +{ + char *stack[10] = {0,}; + int stack_top = 0; + char *sign = ""; + if (num < 0) { + sign = "fu "; + num = -num; + } + for (; num; num = num / 10) { + stack[stack_top++] = map[num % 10]; + } + printf("%s", sign); + while (--stack_top > -1) { + printf("%s ", stack[stack_top]); + } + printf("\b\n"); +} + +int main() +{ + display_num_with_pinyin(-6543210); + return 0; +} diff --git a/simplify_the_denominator.c b/simplify_the_denominator.c new file mode 100644 index 0000000..0e39cda --- /dev/null +++ b/simplify_the_denominator.c @@ -0,0 +1,22 @@ +#include + +int gcd(int a, int b) +{ + return (b == 0) ? a : gcd(b, a % b); +} + +int show_simplified(int a, int b) +{ + if (!a || !b) return printf("a, b should not be zero!\n"); + int sign = (a > 0) ^ (b > 0); + a = (a > 0) ? a : -a; + b = (b > 0) ? b : -b; + int d = gcd(a, b); + return printf("simplified is: %s%d/%d\n", sign ? "-" : "", a / d, b / d); +} + +int main() +{ + show_simplified(123, 333); + return 0; +}