Main.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int* numbers = NULL;
  4. int dices = 0;
  5. void calc(int diceSum, int leftDices) {
  6. if(leftDices <= 0) {
  7. numbers[diceSum - dices]++;
  8. return;
  9. }
  10. for(int i = 1; i <= 6; i++) {
  11. calc(diceSum + i, leftDices - 1);
  12. }
  13. }
  14. int main(int argAmount, const char** args) {
  15. if(argAmount < 3) {
  16. if(argAmount > 0) {
  17. printf("%s <dices> <print_width>\n", args[0]);
  18. } else {
  19. puts("... <dices> <print_width>");
  20. }
  21. return 0;
  22. }
  23. dices = atoi(args[1]);
  24. int width = atoi(args[2]);
  25. if(dices < 1 || dices > 10) {
  26. puts("dices must be in the range [1, 10]");
  27. return 0;
  28. } else if(width < 1 || width > 200) {
  29. puts("print width must be in the range [1, 200]");
  30. return 0;
  31. }
  32. int range = 6 * dices - dices + 1;
  33. numbers = calloc(range, sizeof(int));
  34. calc(0, dices);
  35. int max = 0;
  36. for(int i = 0; i < range; i++) {
  37. if(numbers[i] > max) {
  38. max = numbers[i];
  39. }
  40. }
  41. const char* format = dices == 1 ? "%d: " : "%2d: ";
  42. for(int i = 0; i < range; i++) {
  43. printf(format, i + dices);
  44. int chars = (numbers[i] * width) / max;
  45. for(int k = 0; k < chars; k++) {
  46. putchar('X');
  47. }
  48. putchar('\n');
  49. }
  50. free(numbers);
  51. return 0;
  52. }