usaco-1-1

Since I don’t have much experience in coding and I just do it for fun, I hope my code can bring you some happiness and understnading here…

This is just USACO 1.1, easy. But the problem afterwards are hard…

Prob 0. 1 + 1 = 2

Just used for testing the input and output form of usaco testing system.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
ID:
LANG: C
TASK: test
*/
#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *fin = fopen("test.in", "r");
FILE *fout = fopen("test.out", "w");
int a, b;
fscanf(fin, "%d %d", &a, &b);
fprintf(fout, "%d\n", a + b);
exit(0);
}

Prob 1. Your Ride Is Here

I wrote some not-elegant code for this problem. Anyway, I solved it after all.

  • We transfer the letters into numbers.
  • Then get the product $mod$ 47.
  • If the result is equal to 27, output ‘go’, else output ‘stay’.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
ID:
LANG: C
PROG: ride
*/
#include <stdio.h>
#include <stdlib.h>

int ctoi(char bit);

int inscope(char bit);

int transform(char *command);

int main() {
FILE *fin = fopen("ride.in", "r");
FILE *fout = fopen("ride.out", "w");
char command1[6], command2[6];
fscanf(fin, "%s %s", command1, command2);
if (transform(command1) == transform(command2))
fprintf(fout, "GO\n");
else
fprintf(fout, "STAY\n");
exit(0);
}

int ctoi(char bit) { return bit - 'A' + 1; }

int inscope(char bit) { return (bit >= 'A' && bit <= 'Z'); }

int transform(char *command) {
int target = 1;
for (int i = 0; i < 6; i++) {
if (inscope(command[i]))
target *= ctoi(command[i]);
else
break;
}
return target % 47;
}

Prob 2. Greedy Gift Givers

A little complicated, but we can still see it through easily.

  • We need to establish a relationship between name array and money amount array, so I create a function called find_name.
  • Since all the money should be given in an integer form. Then I created a function called give_over and left_over.
    • left_over returns how much you left after you give off money.
    • give_over returns how much you need to give off to others.
  • So use find_name to locate the giver and the given, then use give_over and left_over to determine the amount of money change.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
ID:
PROG: gift1
LANG: C
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int find_name(char dictionary[][20], int name_quantity, char name_target[]);

int left_over(int quantity, int target_num);

int give_over(int quantity, int target_num);

int main() {
FILE *fin = fopen("gift1.in", "r");
FILE *fout = fopen("gift1.out", "w");
int person_num;
fscanf(fin, "%d", &person_num);
char person_name[10][20];
for (int i = 0; i < person_num; i++)
fscanf(fin, "%s", person_name[i]);
int money_pocket[10];
for (int i = 0; i < person_num; i++)
money_pocket[i] = 0;
for (int i = 0; i < person_num; i++) {
char giver_temp[20];
fscanf(fin, "%s", giver_temp);
int giver_plc_temp = find_name(person_name, person_num, giver_temp);
int quantity, target_num;
fscanf(fin, "%d %d", &quantity, &target_num);
money_pocket[giver_plc_temp]
= money_pocket[giver_plc_temp] - quantity + left_over(quantity, target_num);
for (int i = 0; i < target_num; i++) {
char taker_temp[20];
fscanf(fin, "%s", taker_temp);
int taker_plc_temp = find_name(person_name, person_num, taker_temp);
money_pocket[taker_plc_temp] += give_over(quantity, target_num);
}
}
for (int i = 0; i < person_num; i++)
fprintf(fout, "%s %d\n", person_name[i], money_pocket[i]);
exit(0);
}

int find_name(char dictionary[][20], int name_quantity, char name_target[]) {
for (int i = 0; i < name_quantity; i++)
if (strcmp(dictionary[i], name_target) == 0)
return i;
return -1;
}

int left_over(int quantity, int target_num) {
if (target_num != 0)
return quantity % target_num;
else
return quantity;
}

int give_over(int quantity, int target_num) {
if (target_num != 0)
return (quantity - left_over(quantity, target_num)) / target_num;
else
return 0;
}

Prob 3. Friday the Thirteenth

This is a easy problem. Just match the days and $mod$ 7 to check and count the times.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
ID:
LANG: C
PROG: friday
*/

#include <stdio.h>
#include <stdlib.h>

int month_length(int year, int month);

int main() {
FILE *fin = fopen("friday.in", "r");
FILE *fout = fopen("friday.out", "w");
int years;
fscanf(fin, "%d", &years);
int counter[7];
for (int i = 0; i < 7; i++)
counter[i] = 0;
int current_year = 1900;
int current_weekday = 1;
for (int i = 0; i < years; i++) {
for (int j = 1; j <= 12; j++) {
counter[(current_weekday + 12) % 7]++;
current_weekday += month_length(current_year, j);
current_weekday %= 7;
}
current_year++;
}
fprintf(fout, "%d ", counter[6]);
for (int i = 0; i < 5; i++)
fprintf(fout, "%d ", counter[i]);
fprintf(fout, "%d\n", counter[5]);
exit(0);
}

int month_length(int year, int month) {
int origin_length[12] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
origin_length[1] += 1;
return origin_length[month - 1];
}
0%