Description:
Our football team finished the championship. The result of each match look like "x:y". Results of all matches are recorded in the collection.
For example: ["3:1", "2:2", "0:1", ...]
Write a function that takes such collection and counts the points of our team in the championship. Rules for counting points for each match:
if x>y - 3 points
if x<y - 0 point
if x=y - 1 point
Notes:
there are 10 matches in the championship
0 <= x <= 4
0 <= y <= 4
int points(const char* const games[10])
{
int i = 0, point = 0;;
while (i < 10)
{
if (games[i][0] > games[i][2])
point += 3;
else if (games[i][0] < games[i][2])
point += 0;
else if (games[i][0] == games[i][2])
point += 1;
i++;
}
return point;
}
another solution
#include <stdlib.h>
int points(char* games[]) {
int p = 0;
for (int i = 0; i < 10; ++i) {
int x = games[i][0] - '0';
int y = games[i][2] - '0';
p += (x > y) * 3 + (x == y);
}
return p;
}
another solution
#include <stdlib.h>
int points(char* games[]) {
int points = 0;
int x, y;
for (int i = 0; i < 10; i++)
{
sscanf(games[i], "%d:%d", &x, &y);
if (x > y) points += 3;
else if (x == y) points += 1;
}
return points;
} -> sscanf() 로 받아서 x, y에 저장할 수 있는 듯.
원리는 배열에 들어갈때 input stream을 열어주는 듯?