fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_THROWS 21
  6.  
  7. int main(){
  8.  
  9. char input[100];
  10. int throws[MAX_THROWS] = {0};
  11. int score[10] = {0};
  12. int throwIndex = 0, roundIndex = 0;
  13.  
  14. //input
  15. printf("Input: ");
  16. scanf("%s", input);
  17.  
  18. //Rounds Parse
  19. char *roundsToken = strtok(input, ":");
  20. int numRounds = atoi(roundsToken);
  21. roundsToken = strtok(NULL, ":");
  22.  
  23. char *throwToken = strtok(roundsToken, ",");
  24. while (throwToken != NULL) {
  25. throws[throwIndex++] = atoi(throwToken);
  26. throwToken = strtok(NULL, ",");
  27. }
  28.  
  29. for(int i = 0, roundIndex = 0; roundIndex < numRounds; roundIndex++){
  30. if (throws[i] == 10)
  31. {
  32. //if strike
  33. score[roundIndex] = 10 + throws[i+1] + throws[i+2];
  34. i++;
  35. }
  36. else if(throws[i] + throws[i+2] == 10){
  37. //if spare
  38. score[roundIndex] = 10 + throws[i+1];
  39. i+=2;
  40. }
  41. else{
  42. //if open
  43. score[roundIndex] = throws[i] + throws[i+1];
  44. i+=2;
  45. }
  46.  
  47. if (roundIndex > 0)
  48. {
  49. score[roundIndex] += score[roundIndex - 1];
  50. }
  51.  
  52. }
  53. printf("Output: ");
  54. for (int i = 0; i < numRounds; i++) {
  55. if (i > 0) printf(",");
  56. printf("%d", score[i]);
  57. }
  58. printf("\n");
  59.  
  60. return 0;
  61. }
Success #stdin #stdout 0.01s 5260KB
stdin
4:2,7,4,6,10,4,5
stdout
Input: Output: 9,19,38,47