12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- #include <stdio.h>
- #include <stdlib.h>
- int* numbers = NULL;
- int dices = 0;
- void calc(int diceSum, int leftDices) {
- if(leftDices <= 0) {
- numbers[diceSum - dices]++;
- return;
- }
- for(int i = 1; i <= 6; i++) {
- calc(diceSum + i, leftDices - 1);
- }
- }
- int main(int argAmount, const char** args) {
- if(argAmount < 3) {
- if(argAmount > 0) {
- printf("%s <dices> <print_width>\n", args[0]);
- } else {
- puts("... <dices> <print_width>");
- }
- return 0;
- }
- dices = atoi(args[1]);
- int width = atoi(args[2]);
- if(dices < 1 || dices > 10) {
- puts("dices must be in the range [1, 10]");
- return 0;
- } else if(width < 1 || width > 200) {
- puts("print width must be in the range [1, 200]");
- return 0;
- }
- int range = 6 * dices - dices + 1;
- numbers = calloc(range, sizeof(int));
- calc(0, dices);
- int max = 0;
- for(int i = 0; i < range; i++) {
- if(numbers[i] > max) {
- max = numbers[i];
- }
- }
- const char* format = dices == 1 ? "%d: " : "%2d: ";
- for(int i = 0; i < range; i++) {
- printf(format, i + dices);
- int chars = (numbers[i] * width) / max;
- for(int k = 0; k < chars; k++) {
- putchar('X');
- }
- putchar('\n');
- }
- free(numbers);
- return 0;
- }
|