BIG style update, similar to suckless style

This commit is contained in:
Lu Xu
2022-01-08 03:01:51 +08:00
parent f24c66355a
commit 2f5f467638
138 changed files with 3261 additions and 3432 deletions
+10 -11
View File
@@ -3,18 +3,17 @@
int main()
{
int a, b, pos;
char num[11];
int a, b, pos;
char num[11];
scanf("%d%d", &a, &b);
sprintf(num, "%d", a + b);
scanf("%d%d", &a, &b);
sprintf(num, "%d", a + b);
for(pos = strlen(num) - 3; pos > 0 && num[pos - 1] != '-'; pos -= 3)
{
memmove(num + pos + 1, num + pos, strlen(num) - pos + 1);
num[pos] = ',';
}
for (pos = strlen(num) - 3; pos > 0 && num[pos - 1] != '-'; pos -= 3) {
memmove(num + pos + 1, num + pos, strlen(num) - pos + 1);
num[pos] = ',';
}
puts(num);
return 0;
puts(num);
return 0;
}
+31 -35
View File
@@ -1,49 +1,45 @@
#include <stdio.h>
#include <math.h>
typedef struct Poly{
double coef;
int exp;
typedef struct Poly {
double coef;
int exp;
} Poly[20];
int main()
{
int KA, KB, Ksum = 0;
Poly A, B, sum;
int KA, KB, Ksum = 0;
Poly A, B, sum;
scanf("%d", &KA);
for(int i = 0; i < KA; i++)
scanf("%d %lf", &A[i].exp, &A[i].coef);
scanf("%d", &KA);
for (int i = 0; i < KA; i++)
scanf("%d %lf", &A[i].exp, &A[i].coef);
scanf("%d", &KB);
for(int i = 0; i < KB; i++)
scanf("%d %lf", &B[i].exp, &B[i].coef);
scanf("%d", &KB);
for (int i = 0; i < KB; i++)
scanf("%d %lf", &B[i].exp, &B[i].coef);
int i = 0, j = 0;
while(i < KA || j < KB)
{
if(i == KA || (j < KB && A[i].exp < B[j].exp))
{
sum[Ksum].exp = B[j].exp;
sum[Ksum].coef = B[j++].coef;
}
else if(j == KB || (i < KA && A[i].exp > B[j].exp))
{
sum[Ksum].exp = A[i].exp;
sum[Ksum].coef = A[i++].coef;
}
else
{
sum[Ksum].exp = A[i].exp;
sum[Ksum].coef = A[i++].coef + B[j++].coef;
}
if(fabs(sum[Ksum].coef) >= 0.05) Ksum++;
}
int i = 0, j = 0;
while (i < KA || j < KB) {
if (i == KA || (j < KB && A[i].exp < B[j].exp)) {
sum[Ksum].exp = B[j].exp;
sum[Ksum].coef = B[j++].coef;
} else if (j == KB || (i < KA && A[i].exp > B[j].exp)) {
sum[Ksum].exp = A[i].exp;
sum[Ksum].coef = A[i++].coef;
} else {
sum[Ksum].exp = A[i].exp;
sum[Ksum].coef = A[i++].coef + B[j++].coef;
}
printf("%d", Ksum);
if (fabs(sum[Ksum].coef) >= 0.05)
Ksum++;
}
for(int i = 0; i < Ksum; i++)
printf(" %d %.1lf", sum[i].exp, sum[i].coef);
printf("%d", Ksum);
return 0;
for (int i = 0; i < Ksum; i++)
printf(" %d %.1lf", sum[i].exp, sum[i].coef);
return 0;
}
+83 -91
View File
File diff suppressed because it is too large Load Diff
+39 -43
View File
@@ -6,58 +6,54 @@ typedef struct Member *Member;
typedef struct Child *Child;
struct Member{
int level;
Child child;
int level;
Child child;
};
struct Child{
int ID;
Child iter;
int ID;
Child iter;
};
int main()
{
int N, M, ID, cID, K;
struct Member nodes[100];
struct Child children[100];
int N, M, ID, cID, K;
struct Member nodes[100];
struct Child children[100];
/* Read data and initiate a adjacent list */
scanf("%d %d", &N, &M);
for(int i = 1; i <= N; i++)
{
nodes[i].level = MAX;
nodes[i].child = NULL;
}
nodes[1].level = 0; /* root node at level 0 */
for(int i = 0, k = 0; i < M; i++)
{
scanf("%d %d", &ID, &K);
for(; K--; k++)
{
scanf("%d", &cID);
children[k].ID = cID;
children[k].iter = nodes[ID].child;
nodes[ID].child = &children[k];
}
}
/* Read data and initiate a adjacent list */
scanf("%d %d", &N, &M);
for (int i = 1; i <= N; i++) {
nodes[i].level = MAX;
nodes[i].child = NULL;
}
nodes[1].level = 0; /* root node at level 0 */
for (int i = 0, k = 0; i < M; i++) {
scanf("%d %d", &ID, &K);
for (; K--; k++) {
scanf("%d", &cID);
children[k].ID = cID;
children[k].iter = nodes[ID].child;
nodes[ID].child = &children[k];
}
}
/* For every level, find leaf nodes */
int n = N, count;
for(int level = 0; n; level++)
{
count = 0;
for(int i = 1; i <= N; i++) if(nodes[i].level == level)
{
n--;
if(nodes[i].child == NULL)
count++;
/* set the children to next level */
for(Child c = nodes[i].child; c; c = c->iter)
nodes[c->ID].level = level + 1;
}
/* For every level, find leaf nodes */
int n = N, count;
for (int level = 0; n; level++) {
count = 0;
for (int i = 1; i <= N; i++)
if (nodes[i].level == level) {
n--;
if (nodes[i].child == NULL)
count++;
/* set the children to next level */
for (Child c = nodes[i].child; c; c = c->iter)
nodes[c->ID].level = level + 1;
}
printf("%d%c", count, n ? ' ' : '\0');
}
printf("%d%c", count, n ? ' ' : '\0');
}
return 0;
return 0;
}
+13 -13
View File
@@ -2,20 +2,20 @@
int main()
{
int sum = 0;
char c, *digits[] = {
"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"
};
int sum = 0;
char c, *digits[] = {
"zero", "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"
};
while((c = getchar()) != EOF && c != '\n')
sum += c - '0';
while ((c = getchar()) != EOF && c != '\n')
sum += c - '0';
if(sum >= 100)
printf("%s ", digits[sum / 100]);
if(sum >= 10)
printf("%s ", digits[sum % 100 / 10]);
printf("%s", digits[sum % 10]);
if (sum >= 100)
printf("%s ", digits[sum / 100]);
if (sum >= 10)
printf("%s ", digits[sum % 100 / 10]);
printf("%s", digits[sum % 10]);
return 0;
return 0;
}
+20 -23
View File
@@ -3,31 +3,28 @@
int main()
{
int N, HH, MM, SS;
int firsttime = 86400, lasttime = -1, time;
char firstname[16], lastname[16], name[16];
int N, HH, MM, SS;
int firsttime = 86400, lasttime = -1, time;
char firstname[16], lastname[16], name[16];
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%s %d:%d:%d", name, &HH, &MM, &SS);
time = (HH * 60 + MM) * 60 + SS;
if(time < firsttime)
{
firsttime = time;
strcpy(firstname, name);
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%s %d:%d:%d", name, &HH, &MM, &SS);
time = (HH * 60 + MM) * 60 + SS;
if (time < firsttime) {
firsttime = time;
strcpy(firstname, name);
}
scanf("%d:%d:%d", &HH, &MM, &SS);
time = (HH * 60 + MM) * 60 + SS;
if(time > lasttime)
{
lasttime = time;
strcpy(lastname, name);
}
}
scanf("%d:%d:%d", &HH, &MM, &SS);
time = (HH * 60 + MM) * 60 + SS;
if (time > lasttime) {
lasttime = time;
strcpy(lastname, name);
}
}
printf("%s %s", firstname, lastname);
printf("%s %s", firstname, lastname);
return 0;
return 0;
}
+21 -20
View File
@@ -2,28 +2,29 @@
int main()
{
int K, n, first = 0;
int start, end, sum = -1; /* the current non-negative subsequence */
int a = -1, b, s = 0; /* the maximum-sum subsequence */
int K, n, first = 0;
int start, end, sum = -1; /* the current non-negative subsequence */
int a = -1, b, s = 0; /* the maximum-sum subsequence */
scanf("%d", &K);
for(int i = 0; i < K; i++)
{
scanf("%d", &n);
if(i == 0) first = n; /* record the first number */
scanf("%d", &K);
for (int i = 0; i < K; i++) {
scanf("%d", &n);
if (i == 0) first = n; /* record the first number */
/* update start, end and sum for the current non-negative subsequence */
if(sum < 0) start = n, sum = 0; /* reset if sum < 0 */
sum += n; /* update sum */
if(sum >= 0) end = n; /* update end if sum >= 0 */
/* update start, end and sum for the current non-negative subsequence */
if (sum < 0) start = n, sum = 0; /* reset if sum < 0 */
sum += n; /* update sum */
if (sum >= 0) end = n; /* update end if sum >= 0 */
/* update a, b and s for the so-far maximum-sum subsequence */
if(sum > s || (!sum && !s)) /* special case is max-sum is 0 */
a = start, b = end, s = sum;
}
/* a won't be updated if all the numbers are negative */
if(a == -1) printf("0 %d %d", first, n);
else printf("%d %d %d", s, a, b);
/* update a, b and s for the so-far maximum-sum subsequence */
if (sum > s || (!sum && !s)) /* special case is max-sum is 0 */
a = start, b = end, s = sum;
}
/* a won't be updated if all the numbers are negative */
if (a == -1)
printf("0 %d %d", first, n);
else
printf("%d %d %d", s, a, b);
return 0;
return 0;
}
+11 -10
View File
@@ -1,17 +1,18 @@
#include <stdio.h>
int main()
{
int N, time, cur, pre;
int N, time, cur, pre;
scanf("%d", &N);
for(pre = 0, time = 0; N--; pre = cur)
{
scanf("%d", &cur);
if(cur > pre) time += (cur - pre) * 6 + 5;
else time += (pre - cur) * 4 + 5;
}
scanf("%d", &N);
for (pre = 0, time = 0; N--; pre = cur) {
scanf("%d", &cur);
if (cur > pre)
time += (cur - pre) * 6 + 5;
else
time += (pre - cur) * 4 + 5;
}
printf("%d", time);
printf("%d", time);
return 0;
return 0;
}
+22 -15
View File
@@ -1,24 +1,31 @@
#include <stdio.h>
int main()
{
int N, exp, count = 0;
float coef, A[1001] = {0}, B[1001] = {0}, MUL[2001] = {0};
int N, exp, count = 0;
float coef, A[1001] = {0}, B[1001] = {0}, MUL[2001] = {0};
scanf("%d", &N);
while(N--){ scanf("%d %f", &exp, &coef); A[exp] = coef; }
scanf("%d", &N);
while(N--){ scanf("%d %f", &exp, &coef); B[exp] = coef; }
scanf("%d", &N);
while (N--) {
scanf("%d %f", &exp, &coef);
A[exp] = coef;
}
scanf("%d", &N);
while (N--) {
scanf("%d %f", &exp, &coef);
B[exp] = coef;
}
for(int i = 0; i < 1001; i++)
for(int j = 0; j < 1001; j++)
MUL[i + j] += A[i] * B[j];
for (int i = 0; i < 1001; i++)
for (int j = 0; j < 1001; j++)
MUL[i + j] += A[i] * B[j];
for(int i = 0; i < 2001; i++)
if(MUL[i]) count++;
for (int i = 0; i < 2001; i++)
if (MUL[i]) count++;
printf("%d", count);
for(int i = 2000; i >= 0; i--) if(MUL[i])
printf(" %d %.1f", i, MUL[i]);
printf("%d", count);
for (int i = 2000; i >= 0; i--)
if (MUL[i])
printf(" %d %.1f", i, MUL[i]);
return 0;
return 0;
}
+45 -48
View File
@@ -10,69 +10,66 @@
/* Calculate decimal value of a text-form number s under a radix */
long long base10(char *s, long long radix)
{
long long n, sum;
for(sum = 0; *s; s++)
{
n = CBASE10(*s);
if((LLONG_MAX - n) / radix < sum) /* overflow */
return OVERFLOW;
sum = sum * radix + n;
}
return sum;
long long n, sum;
for (sum = 0; *s; s++) {
n = CBASE10(*s);
if ((LLONG_MAX - n) / radix < sum) /* overflow */
return OVERFLOW;
sum = sum * radix + n;
}
return sum;
}
/* Find the smallest possible radix of a numbers */
int minradix(char *s)
{ /* Simply the largest digit in the number plus 1 */
char r;
for(r = '0'; *s; s++)
if(*s > r)
r = *s;
return CBASE10(r) + 1;
char r;
for (r = '0'; *s; s++)
if (*s > r)
r = *s;
return CBASE10(r) + 1;
}
/* Use binary search to locate radix of s which makes it equals n */
long long binsearch(char *s, long long n, long long rmin, long long rmax)
{
long long r, m;
while(rmax >= rmin)
{
r = rmin + (rmax - rmin) / 2; /* avoid (rmin + rmax) overflow */
if((m = base10(s, r)) > n || m == OVERFLOW)
rmax = r - 1;
else if(m < n)
rmin = r + 1;
else
return r;
}
return NOTFOUNT;
long long r, m;
while (rmax >= rmin) {
r = rmin + (rmax - rmin) / 2; /* avoid (rmin + rmax) overflow */
if ((m = base10(s, r)) > n || m == OVERFLOW)
rmax = r - 1;
else if (m < n)
rmin = r + 1;
else
return r;
}
return NOTFOUNT;
}
int main()
{
int tag, radix;
long long N1, rmin, rmax, r;
char buf1[11], buf2[11], *S1, *S2;
int tag, radix;
long long N1, rmin, rmax, r;
char buf1[11], buf2[11], *S1, *S2;
/* Make S1 point to the number with known radix, S2 to the other */
scanf("%s %s %d %d", buf1, buf2, &tag, &radix);
if(tag == 1) S1 = buf1, S2 = buf2;
if(tag == 2) S1 = buf2, S2 = buf1;
/* Make S1 point to the number with known radix, S2 to the other */
scanf("%s %s %d %d", buf1, buf2, &tag, &radix);
if (tag == 1) S1 = buf1, S2 = buf2;
if (tag == 2) S1 = buf2, S2 = buf1;
N1 = base10(S1, radix); /* Corresponding decimal of S1 */
rmin = minradix(S2); /* Smallest possible radix of S2 */
rmax = LLONG_MAX; /* Largest possible radix of S2 */
if(strlen(S2) == 1) /* If so, N2 will be same value under any radix */
{ /* rmin - 1 (naturally equals N2) equals N1 */
if(N1 == rmin - 1) printf("%lld", rmin);
else printf("Impossible");
}
else /* Binary search to find the radix of N2 */
{
r = binsearch(S2, N1, rmin, rmax);
if(r != NOTFOUNT) printf("%lld", r);
else printf("Impossible");
}
N1 = base10(S1, radix); /* Corresponding decimal of S1 */
rmin = minradix(S2); /* Smallest possible radix of S2 */
rmax = LLONG_MAX; /* Largest possible radix of S2 */
if (strlen(S2) == 1) { /* If so, N2 will be same value under any radix */
if (N1 == rmin - 1) /* rmin - 1 (naturally equals N2) equals N1 */
printf("%lld", rmin);
else
printf("Impossible");
} else { /* Binary search to find the radix of N2 */
r = binsearch(S2, N1, rmin, rmax);
if (r != NOTFOUNT) printf("%lld", r);
else printf("Impossible");
}
return 0;
return 0;
}
+16 -19
View File
@@ -2,25 +2,22 @@
int main()
{
char c = '\0';
float odd, maxodd, maxprofit = 1;
char c = '\0';
float odd, maxodd, maxprofit = 1;
for(int i = 0; i < 3; i++)
{
maxodd = 0;
for(int j = 0; j < 3; j++)
{
scanf("%f", &odd);
if(maxodd < odd)
{
maxodd = odd;
c = "WTL"[j];
}
}
printf("%c ", c);
maxprofit *= maxodd;
}
printf("%.2f", (maxprofit * 0.65 - 1) * 2);
for (int i = 0; i < 3; i++) {
maxodd = 0;
for (int j = 0; j < 3; j++) {
scanf("%f", &odd);
if (maxodd < odd) {
maxodd = odd;
c = "WTL"[j];
}
}
printf("%c ", c);
maxprofit *= maxodd;
}
printf("%.2f", (maxprofit * 0.65 - 1) * 2);
return 0;
return 0;
}
+28 -30
View File
@@ -4,39 +4,37 @@ typedef struct Student{ int ID, score[4]; } Student;
int main()
{
/* scores array stores the number of students of any score */
int N, M, ID, scores[4][102] = {{0}};
/* scores array stores the number of students of any score */
int N, M, ID, scores[4][102] = {{0}};
/* Setting a score of 101 is to make it easy to calculate rank */
scores[0][101] = scores[1][101] = scores[2][101] = scores[3][101] = 1;
Student students[2000];
/* Setting a score of 101 is to make it easy to calculate rank */
scores[0][101] = scores[1][101] = scores[2][101] = scores[3][101] = 1;
Student students[2000];
scanf("%d %d", &N, &M);
for(int i = 0; i < N; i++)
{ /* record all the scores for every student: A, C, M, E */
Student *s = students + i;
scanf("%d %d %d %d", &s->ID, &s->score[1], &s->score[2], &s->score[3]);
s->score[0] = (s->score[1] + s->score[2] + s->score[3] + 1) / 3;
for(int j = 0; j < 4; j++) /* +1 for rounding */
scores[j][s->score[j]]++; /* record how many got this score */
}
scanf("%d %d", &N, &M);
for (int i = 0; i < N; i++) { /* record all the scores for every student: A, C, M, E */
Student *s = students + i;
scanf("%d %d %d %d", &s->ID, &s->score[1], &s->score[2], &s->score[3]);
s->score[0] = (s->score[1] + s->score[2] + s->score[3] + 1) / 3;
for (int j = 0; j < 4; j++) /* +1 for rounding */
scores[j][s->score[j]]++; /* record how many got this score */
}
for(int i = 0; i < M; i++)
{
int max = 3, ranks[4] = {0}, stu;
for (int i = 0; i < M; i++) {
int max = 3, ranks[4] = {0}, stu;
scanf("%d", &ID);
for(stu = 0; stu < N && students[stu].ID != ID; stu++) ; /* find */
if(stu == N) { puts("N/A"); continue;}
scanf("%d", &ID);
for (stu = 0; stu < N && students[stu].ID != ID; stu++) ; /* find */
if (stu == N) { puts("N/A"); continue;}
for(int j = 3; j >= 0; j--)
{ /* calculate the rank: sum the counts from score + 1 to 101 */
/* e.g. score 100 will have rank 1, since scores[101] is 1 */
for(int score = 100; score >= students[stu].score[j]; score--)
ranks[j] += scores[j][score + 1];
if(ranks[j] <= ranks[max]) /* The best rank with highest priority */
max = j;
}
printf("%d %c\n", ranks[max], "ACME"[max]);
}
/* calculate the rank: sum the counts from score + 1 to 101 */
for (int j = 3; j >= 0; j--) {
/* e.g. score 100 will have rank 1, since scores[101] is 1 */
for (int score = 100; score >= students[stu].score[j]; score--)
ranks[j] += scores[j][score + 1];
if (ranks[j] <= ranks[max]) /* The best rank with highest priority */
max = j;
}
printf("%d %c\n", ranks[max], "ACME"[max]);
}
}
+23 -25
View File
@@ -4,37 +4,35 @@
void DFS(int v, int known[], int G[][MAX])
{
known[v] = 1;
for(int i = 0; i < MAX; i++)
if((G[v][i] || G[i][v]) && !known[i])
DFS(i, known, G);
known[v] = 1;
for (int i = 0; i < MAX; i++)
if ((G[v][i] || G[i][v]) && !known[i])
DFS(i, known, G);
}
int main()
{
int N, M, K;
scanf("%d %d %d", &N, &M, &K);
int N, M, K;
scanf("%d %d %d", &N, &M, &K);
int Graph[MAX][MAX] = {{0}};
int Graph[MAX][MAX] = {{0}};
int city1, city2;
for(int i = 0; i < M; i++)
{
scanf("%d %d", &city1, &city2);
Graph[city1][city2] = 1;
}
int city1, city2;
for (int i = 0; i < M; i++) {
scanf("%d %d", &city1, &city2);
Graph[city1][city2] = 1;
}
for(int i = 0; i < K; i++)
{
int known[MAX] = {0}, lostcity, count = 0;
scanf("%d", &lostcity);
for (int i = 0; i < K; i++) {
int known[MAX] = {0}, lostcity, count = 0;
scanf("%d", &lostcity);
known[lostcity] = 1;
for(int i = 1; i <= N; i++) if(!known[i])
{
DFS(i, known, Graph);
count++;
}
printf("%d\n", count - 1);
}
known[lostcity] = 1;
for (int i = 1; i <= N; i++)
if (!known[i]) {
DFS(i, known, Graph);
count++;
}
printf("%d\n", count - 1);
}
}
+51 -56
View File
@@ -7,64 +7,59 @@
int main()
{
int N, M, K, Q, query;
int time[1000] = {0}, queue[20][11] = {{0}};
int front[20] = {0}, rear[20] = {0}, length[20] = {0};
int N, M, K, Q, query;
int time[1000] = {0}, queue[20][11] = {{0}};
int front[20] = {0}, rear[20] = {0}, length[20] = {0};
scanf("%d %d %d %d", &N, &M, &K, &Q);
for(int i = 1; i <= K; i++)
scanf("%d", time + i);
scanf("%d %d %d %d", &N, &M, &K, &Q);
for (int i = 1; i <= K; i++)
scanf("%d", time + i);
/* Total number of operations */
int count = (K < M * N) ? (2 * K) : (K + M * N);
/* Doing dequeues and enqueues for every customer */
for(int i = 1; i <= count; i++)
{
if(i > count - K) /* Dequeue in the last K operations */
{
/* Find the next customer */
int time_span = 9999, next = -1;
for(int j = 0; j < N; j++)
if(length[j])
{
if(TIME_FRONT(j) < time_span)
next = j, time_span = TIME_FRONT(j);
else if(next == -1 && TIME_FRONT(j) == LATE_FLAG)
next = j;
}
/* Dequeue */
FORWARD(front[next]);
length[next]--;
}
if(i <= K) /* Enqueue in the first K operations */
{
/* Find shortest queue */
int shortest = 0;
for(int j = 0; j < N; j++)
if(length[shortest] > length[j])
shortest = j;
/* Set flag or add time */
int previous_time = TIME_REAR_PREVIOUS(shortest);
if(previous_time >= 9 * 60 || previous_time == LATE_FLAG)
time[i] = LATE_FLAG;
else
time[i] += previous_time;
/* Enqueue */
queue[shortest][rear[shortest]] = i;
FORWARD(rear[shortest]);
length[shortest]++;
}
}
/* Total number of operations */
int count = (K < M * N) ? (2 * K) : (K + M * N);
/* Doing dequeues and enqueues for every customer */
for (int i = 1; i <= count; i++) {
if (i > count - K) { /* Dequeue in the last K operations */
/* Find the next customer */
int time_span = 9999, next = -1;
for (int j = 0; j < N; j++)
if (length[j]) {
if (TIME_FRONT(j) < time_span)
next = j, time_span = TIME_FRONT(j);
else if (next == -1 && TIME_FRONT(j) == LATE_FLAG)
next = j;
}
/* Dequeue */
FORWARD(front[next]);
length[next]--;
}
if (i <= K) { /* Enqueue in the first K operations */
/* Find shortest queue */
int shortest = 0;
for (int j = 0; j < N; j++)
if (length[shortest] > length[j])
shortest = j;
/* Set flag or add time */
int previous_time = TIME_REAR_PREVIOUS(shortest);
if (previous_time >= 9 * 60 || previous_time == LATE_FLAG)
time[i] = LATE_FLAG;
else
time[i] += previous_time;
/* Enqueue */
queue[shortest][rear[shortest]] = i;
FORWARD(rear[shortest]);
length[shortest]++;
}
}
/* Read queries and print answers */
for(int i = 0; i < Q; i++)
{
scanf("%d", &query);
if(time[query] != LATE_FLAG)
printf("%02d:%02d\n", 8 + time[query] / 60, time[query] % 60);
else
printf("Sorry\n");
}
/* Read queries and print answers */
for (int i = 0; i < Q; i++) {
scanf("%d", &query);
if (time[query] != LATE_FLAG)
printf("%02d:%02d\n", 8 + time[query] / 60, time[query] % 60);
else
printf("Sorry\n");
}
return 0;
return 0;
}
+16 -17
View File
@@ -2,31 +2,30 @@
int iPrime(int N)
{
if(N == 0 || N == 1)
return 0;
for(int i = 2; i * i <= N; i++)
if(N % i == 0)
return 0;
return 1;
if (N == 0 || N == 1)
return 0;
for (int i = 2; i * i <= N; i++)
if (N % i == 0)
return 0;
return 1;
}
int Rev(int N, int D)
{
int Nrev;
for(Nrev = 0; N; N /= D)
{
Nrev *= D;
Nrev += N % D;
}
return Nrev;
int Nrev;
for (Nrev = 0; N; N /= D) {
Nrev *= D;
Nrev += N % D;
}
return Nrev;
}
int main()
{
int N, D;
int N, D;
while(scanf("%d %d", &N, &D) == 2)
puts(iPrime(N) && iPrime(Rev(N, D)) ? "Yes" : "No");
while (scanf("%d %d", &N, &D) == 2)
puts(iPrime(N) && iPrime(Rev(N, D)) ? "Yes" : "No");
return 0;
return 0;
}
+51 -56
View File
@@ -4,80 +4,75 @@
/* Structure to store phone records */
typedef struct {
char name[21];
int month, day, hour, min, time, state;
char name[21];
int month, day, hour, min, time, state;
}Record, *pRecord;
/* Compare first by name, then by date and time */
int cmp(const void *record1, const void *record2)
{
pRecord r1 = *(pRecord*)record1, r2 = *(pRecord*)record2;
return strcmp(r1->name, r2->name) ?
strcmp(r1->name, r2->name) : r1->time - r2->time;
pRecord r1 = *(pRecord*)record1, r2 = *(pRecord*)record2;
return strcmp(r1->name, r2->name) ? strcmp(r1->name, r2->name) : r1->time - r2->time;
}
/* Calculate the charge of the call with start record p1 and end record p2 */
int calccharge(pRecord p1, pRecord p2, int toll[])
{
int charge = 0, start = p1->time, end = p2->time, h, time1, time2;
int charge = 0, start = p1->time, end = p2->time, h, time1, time2;
for(time1 = start; time1 < end; time1 = time2)
{ /* Add the charge hour by hour */
time2 = (time1 / 60 + 1) * 60; /* time2 will be the time of next hour */
h = time1 / 60 % 24; /* h will be the index of the hour */
charge += ((time2 > end ? end : time2) - time1) * toll[h];
}
for (time1 = start; time1 < end; time1 = time2) { /* Add the charge hour by hour */
time2 = (time1 / 60 + 1) * 60; /* time2 will be the time of next hour */
h = time1 / 60 % 24; /* h will be the index of the hour */
charge += ((time2 > end ? end : time2) - time1) * toll[h];
}
return charge;
return charge;
}
int main()
{
char state[9];
int N, toll[24], charge, charge_total = 0;
Record records[1001] = {0};
pRecord precords[1001] = {0}, *p = precords;
char state[9];
int N, toll[24], charge, charge_total = 0;
Record records[1001] = {0};
pRecord precords[1001] = {0}, *p = precords;
/* Read data */
for(int i = 0; i < 24; i++)
scanf("%d", toll + i);
scanf("%d", &N);
for(int i = 0; i < N; i++, p++)
{
*p = records + i;
scanf("%s %d:%d:%d:%d %s", (*p)->name,
&(*p)->month, &(*p)->day, &(*p)->hour, &(*p)->min, state);
(*p)->time = ((*p)->day * 24 + (*p)->hour) * 60 + (*p)->min;
(*p)->state = strcmp(state, "on-line") ? 0 : 1;
}
/* Read data */
for (int i = 0; i < 24; i++)
scanf("%d", toll + i);
scanf("%d", &N);
for (int i = 0; i < N; i++, p++) {
*p = records + i;
scanf("%s %d:%d:%d:%d %s", (*p)->name,
&(*p)->month, &(*p)->day, &(*p)->hour, &(*p)->min, state);
(*p)->time = ((*p)->day * 24 + (*p)->hour) * 60 + (*p)->min;
(*p)->state = strcmp(state, "on-line") ? 0 : 1;
}
/* Sort first by name, then by date and time */
qsort(precords, N, sizeof(pRecord), cmp);
/* Sort first by name, then by date and time */
qsort(precords, N, sizeof(pRecord), cmp);
/* Print phone bill one by one */
for(p = precords + 1; *p; p++)
{
if(strcmp((*p)->name, (*(p - 1))->name))
{ /* A new customer, print last total amount if any */
if(charge_total)
printf("Total amount: $%.2f\n", charge_total * 1e-2);
charge_total = 0;
}
else if((*(p - 1))->state == 1 && (*p)->state == 0)
{ /* Still the same customer, finding on/off record pair */
if(charge_total == 0)
printf("%s %02d\n", (*p)->name, (*p)->month);
charge = calccharge(*(p - 1), *p, toll);
charge_total += charge;
/* Print info of this call */
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",
(*(p - 1))->day, (*(p - 1))->hour, (*(p - 1))->min,
(*p)->day, (*p)->hour, (*p)->min,
(*p)->time - (*(p - 1))->time, charge * 1e-2);
}
}
if(charge_total)
printf("Total amount: $%.2f\n", charge_total * 1e-2);
/* Print phone bill one by one */
for (p = precords + 1; *p; p++) {
if (strcmp((*p)->name, (*(p - 1))->name)) { /* A new customer, print last total amount if any */
if (charge_total)
printf("Total amount: $%.2f\n", charge_total * 1e-2);
charge_total = 0;
}
else if ((*(p - 1))->state == 1 && (*p)->state == 0) { /* Still the same customer, finding on/off record pair */
if (charge_total == 0)
printf("%s %02d\n", (*p)->name, (*p)->month);
charge = calccharge(*(p - 1), *p, toll);
charge_total += charge;
/* Print info of this call */
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",
(*(p - 1))->day, (*(p - 1))->hour, (*(p - 1))->min,
(*p)->day, (*p)->hour, (*p)->min,
(*p)->time - (*(p - 1))->time, charge * 1e-2);
}
}
return 0;
if (charge_total)
printf("Total amount: $%.2f\n", charge_total * 1e-2);
return 0;
}
+42 -47
View File
@@ -2,68 +2,63 @@
#include <stdlib.h>
typedef struct Customer{
int start, len;
int start, len;
}Customer;
/* Compare two Customer stucture by start time */
int cmp(const void *a, const void *b)
{
Customer c1 = *(Customer*)a;
Customer c2 = *(Customer*)b;
return c1.start - c2.start;
Customer c1 = *(Customer*)a;
Customer c2 = *(Customer*)b;
return c1.start - c2.start;
}
int main()
{
int N, K, earliest, i;
int HH, MM, SS;
int wait_time = 0, queue_time[100] = {0};
Customer customers[10000], *p;
int N, K, earliest, i;
int HH, MM, SS;
int wait_time = 0, queue_time[100] = {0};
Customer customers[10000], *p;
scanf("%d %d", &N, &K);
for(int i = 0; i < N; i++)
{
scanf("%d:%d:%d %d", &HH, &MM, &SS, &customers[i].len);
/* Relative time to 08:00:00 */
customers[i].start = SS + 60 * (MM + 60 * (HH - 8));
customers[i].len *= 60;
}
scanf("%d %d", &N, &K);
for (int i = 0; i < N; i++) {
scanf("%d:%d:%d %d", &HH, &MM, &SS, &customers[i].len);
/* Relative time to 08:00:00 */
customers[i].start = SS + 60 * (MM + 60 * (HH - 8));
customers[i].len *= 60;
}
qsort(customers, N, sizeof(Customer), cmp);
qsort(customers, N, sizeof(Customer), cmp);
for(i = 0; i < N; i++)
{
p = customers + i;
for (i = 0; i < N; i++) {
p = customers + i;
/* Find the queue number which will finish next */
earliest = 0;
for(int i = 0; i < K; i++)
if(queue_time[i] < queue_time[earliest])
earliest = i;
/* Find the queue number which will finish next */
earliest = 0;
for (int i = 0; i < K; i++)
if (queue_time[i] < queue_time[earliest])
earliest = i;
/* later than 17:00:00 */
if(p->start > (17 - 8) * 3600)
break;
/* processing time longer than one hour */
if(p->len > 3600)
p->len = 3600;
/* later than 17:00:00 */
if (p->start > (17 - 8) * 3600)
break;
/* processing time longer than one hour */
if (p->len > 3600)
p->len = 3600;
/* increase total waiting time and modify the time of each queue */
if(p->start < queue_time[earliest])
{
wait_time += queue_time[earliest] - p->start;
queue_time[earliest] += p->len;
}
else
{
queue_time[earliest] = p->start + p->len;
}
}
/* increase total waiting time and modify the time of each queue */
if (p->start < queue_time[earliest]) {
wait_time += queue_time[earliest] - p->start;
queue_time[earliest] += p->len;
} else {
queue_time[earliest] = p->start + p->len;
}
}
if(i)
printf("%.1f", wait_time / 60.0 / i);
else
printf("0.0");
if (i)
printf("%.1f", wait_time / 60.0 / i);
else
printf("0.0");
return 0;
return 0;
}
+16 -16
View File
@@ -2,25 +2,25 @@
int main()
{
int N, b, c, isPalindromicNumber = 1, b_base[30] = {0};
int N, b, c, isPalindromicNumber = 1, b_base[30] = {0};
scanf("%d %d", &N, &b);
scanf("%d %d", &N, &b);
/* Calculate N in b-base */
for(c = 0; N; N /= b, c++)
b_base[c] = N % b;
/* Calculate N in b-base */
for (c = 0; N; N /= b, c++)
b_base[c] = N % b;
/* Check if it is palindromic number */
for(int i = 0; i < c / 2; i++)
if(b_base[i] != b_base[c - i - 1])
isPalindromicNumber = 0;
puts(isPalindromicNumber ? "Yes" : "No");
/* Check if it is palindromic number */
for (int i = 0; i < c / 2; i++)
if (b_base[i] != b_base[c - i - 1])
isPalindromicNumber = 0;
puts(isPalindromicNumber ? "Yes" : "No");
if(c == 0)
printf("0");
/* Reversely printing is the correct order */
while(c--)
printf("%d%c", b_base[c], c ? ' ' : '\0');
if (c == 0)
printf("0");
/* Reversely printing is the correct order */
while (c--)
printf("%d%c", b_base[c], c ? ' ' : '\0');
return 0;
return 0;
}
+44 -45
View File
@@ -4,67 +4,66 @@
#define CNODE 31
typedef struct node{
int *post, *in, length;
int *post, *in, length;
}node;
typedef struct queue{
node *nodes[QLEN];
int front, rear, count;
node *nodes[QLEN];
int front, rear, count;
}queue;
void enqueue(queue *q, node *n)
{
q->nodes[q->rear] = n;
q->rear = (q->rear == QLEN - 1) ? 0 : q->rear + 1;
q->count++;
q->nodes[q->rear] = n;
q->rear = (q->rear == QLEN - 1) ? 0 : q->rear + 1;
q->count++;
}
node *dequeue(queue *q)
{
node *n = q->nodes[q->front];
q->front = (q->front == QLEN - 1) ? 0 : q->front + 1;
q->count--;
return n;
node *n = q->nodes[q->front];
q->front = (q->front == QLEN - 1) ? 0 : q->front + 1;
q->count--;
return n;
}
int main()
{
int post[CNODE] = {0}, in[CNODE] = {0}, N;
queue q = {0}; /* Initialize to zeros or NULL array */
int root, index, count;
int post[CNODE] = {0}, in[CNODE] = {0}, N;
queue q = {0}; /* Initialize to zeros or NULL array */
int root, index, count;
scanf("%d", &N);
for(int i = 0; i < N; i++)
scanf("%d", post + i);
for(int i = 0; i < N; i++)
scanf("%d", in + i);
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%d", post + i);
for (int i = 0; i < N; i++)
scanf("%d", in + i);
node nodes[CNODE] = {{post, in, N}}, *p = nodes, *n;
enqueue(&q, p++);
for(count = 0; q.count; count++)
{
n = dequeue(&q);
/* Find root node in post-order, which is the last one */
root = n->post[n->length - 1];
/* Find the root node in in-order */
for(index = 0; index < n->length; index++)
if(n->in[index] == root)
break;
/* The subsequence post/in-order of left child */
p->post = n->post;
p->in = n->in;
p->length = index;
if(p->length != 0) /* left child not NULL */
enqueue(&q, p++);
/* The subsequence post/in-order of right child */
p->post = n->post + index;
p->in = n->in + index + 1;
p->length = n->length - index - 1;
if(p->length != 0) /* right child not NULL */
enqueue(&q, p++);
/* print */
printf("%d%c", root, (count == N - 1) ? '\0' : ' ');
}
node nodes[CNODE] = {{post, in, N}}, *p = nodes, *n;
enqueue(&q, p++);
for (count = 0; q.count; count++) {
n = dequeue(&q);
/* Find root node in post-order, which is the last one */
root = n->post[n->length - 1];
/* Find the root node in in-order */
for (index = 0; index < n->length; index++)
if (n->in[index] == root)
break;
/* The subsequence post/in-order of left child */
p->post = n->post;
p->in = n->in;
p->length = index;
if (p->length != 0) /* left child not NULL */
enqueue(&q, p++);
/* The subsequence post/in-order of right child */
p->post = n->post + index;
p->in = n->in + index + 1;
p->length = n->length - index - 1;
if (p->length != 0) /* right child not NULL */
enqueue(&q, p++);
/* print */
printf("%d%c", root, (count == N - 1) ? '\0' : ' ');
}
return 0;
return 0;
}
+62 -68
View File
@@ -1,92 +1,86 @@
#include <stdio.h>
typedef struct node{
int visited, level, depth;
struct adj *adj;
int visited, level, depth;
struct adj *adj;
} node;
typedef struct adj{
struct node *node;
struct adj *next;
struct node *node;
struct adj *next;
} adj;
void DFS(node *n, int level)
{
n->visited = 1;
n->level = level + 1;
n->visited = 1;
n->level = level + 1;
for(adj *a = n->adj; a; a = a->next)
if(a->node->visited == 0)
DFS(a->node, level + 1);
for (adj *a = n->adj; a; a = a->next)
if (a->node->visited == 0)
DFS(a->node, level + 1);
}
int main()
{
int N, n1, n2, count, depth, maxdepth = 0;
node nodes[10000] = {0}, *pnode;
adj edges[20000] = {0}, *padj;
int N, n1, n2, count, depth, maxdepth = 0;
node nodes[10000] = {0}, *pnode;
adj edges[20000] = {0}, *padj;
/* Read and build the adjacent linked list */
scanf("%d", &N);
for(int i = 0; i < N - 1; i++)
{
scanf("%d %d", &n1, &n2);
/* n1 to n2 */
pnode = &nodes[n1 - 1];
padj = &edges[i * 2];
padj->node = &nodes[n2 - 1];
padj->next = pnode->adj;
pnode->adj = padj;
/* n2 to n1 */
pnode = &nodes[n2 - 1];
padj = &edges[i * 2 + 1];
padj->node = &nodes[n1 - 1];
padj->next = pnode->adj;
pnode->adj = padj;
}
/* Read and build the adjacent linked list */
scanf("%d", &N);
for (int i = 0; i < N - 1; i++) {
scanf("%d %d", &n1, &n2);
/* n1 to n2 */
pnode = &nodes[n1 - 1];
padj = &edges[i * 2];
padj->node = &nodes[n2 - 1];
padj->next = pnode->adj;
pnode->adj = padj;
/* n2 to n1 */
pnode = &nodes[n2 - 1];
padj = &edges[i * 2 + 1];
padj->node = &nodes[n1 - 1];
padj->next = pnode->adj;
pnode->adj = padj;
}
for(int i = 0; i < N; i++)
{
/* Reset the whole graph */
depth = 0;
count = 1;
for(int i = 0; i < N; i++)
nodes[i].visited = nodes[i].level = 0;
for (int i = 0; i < N; i++) {
/* Reset the whole graph */
depth = 0;
count = 1;
for (int i = 0; i < N; i++)
nodes[i].visited = nodes[i].level = 0;
/* Start from the ith node */
DFS(nodes + i, 0);
/* Start from the ith node */
DFS(nodes + i, 0);
/* Get the depth of the tree */
for(int i = 0; i < N; i++)
if(nodes[i].visited == 1)
if(nodes[i].level > depth)
depth = nodes[i].level;
/* Get the depth of the tree */
for (int i = 0; i < N; i++)
if (nodes[i].visited == 1)
if (nodes[i].level > depth)
depth = nodes[i].level;
/* Try to find other disjoint components */
for(int i = 0; i < N; i++)
if(nodes[i].visited == 0)
{
DFS(nodes + i, 0);
count ++;
}
/* Try to find other disjoint components */
for (int i = 0; i < N; i++)
if (nodes[i].visited == 0) {
DFS(nodes + i, 0);
count ++;
}
if(count != 1) /* If not all the nodes are visited */
{
printf("Error: %d components", count);
return 0; /* Only have to do it once */
}
else /* It is one tree */
{
nodes[i].depth = depth;
if(maxdepth < depth)
maxdepth = depth;
}
}
if (count != 1) { /* If not all the nodes are visited */
printf("Error: %d components", count);
return 0; /* Only have to do it once */
} else { /* It is one tree */
nodes[i].depth = depth;
if (maxdepth < depth)
maxdepth = depth;
}
}
/* Find the root with the same maximum depth */
for(int i = 0; i < N; i++)
if(nodes[i].depth == maxdepth)
printf("%d\n", i + 1);
/* Find the root with the same maximum depth */
for (int i = 0; i < N; i++)
if (nodes[i].depth == maxdepth)
printf("%d\n", i + 1);
return 0;
return 0;
}
+71 -79
View File
@@ -3,95 +3,87 @@
#include <string.h>
typedef struct book {
int ID;
char title[81];
char author[81];
char keywords[5][11];
char publisher[81];
int year;
int ID;
char title[81];
char author[81];
char keywords[5][11];
char publisher[81];
int year;
} Book;
int cmpbyid(const void *a, const void *b)
{
return (*(Book**)a)->ID - (*(Book**)b)->ID;
return (*(Book**)a)->ID - (*(Book**)b)->ID;
}
int main()
{
int N, M, count, query_type, query_year;
char space, query_str[81];
Book books[10000] = {0}, *p, *booksbyid[10000];
int N, M, count, query_type, query_year;
char space, query_str[81];
Book books[10000] = {0}, *p, *booksbyid[10000];
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
p = booksbyid[i] = books + i;
scanf("%d%c", &p->ID, &space);
scanf("%[^\n]%c", p->title, &space);
scanf("%[^\n]%c", p->author, &space);
space = '\0';
for(int k = 0; space != '\n'; k++)
scanf("%s%c", p->keywords[k], &space);
scanf("%[^\n]%c", p->publisher, &space);
scanf("%d", &p->year);
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
p = booksbyid[i] = books + i;
scanf("%d%c", &p->ID, &space);
scanf("%[^\n]%c", p->title, &space);
scanf("%[^\n]%c", p->author, &space);
space = '\0';
for (int k = 0; space != '\n'; k++)
scanf("%s%c", p->keywords[k], &space);
scanf("%[^\n]%c", p->publisher, &space);
scanf("%d", &p->year);
}
qsort(booksbyid, N, sizeof(Book*), cmpbyid);
qsort(booksbyid, N, sizeof(Book*), cmpbyid);
scanf("%d", &M);
for(int i = 0; i < M; i ++)
{
count = 0;
scanf("%d: %[^\n]%c", &query_type, query_str, &space);
printf("%d: %s\n", query_type, query_str);
for(int j = 0; j < N; j++)
{
p = booksbyid[j];
switch(query_type)
{
case 1:
if(strcmp(query_str, p->title) == 0)
{
printf("%07d\n", p->ID);
count ++;
}
break;
case 2:
if(strcmp(query_str, p->author) == 0)
{
printf("%07d\n", p->ID);
count ++;
}
break;
case 3:
for(int k = 0; k < 5; k ++)
if(strcmp(query_str, p->keywords[k]) == 0)
{
printf("%07d\n", p->ID);
count ++;
}
break;
case 4:
if(strcmp(query_str, p->publisher) == 0)
{
printf("%07d\n", p->ID);
count ++;
}
break;
case 5:
sscanf(query_str, "%d", &query_year);
if(query_year == p->year)
{
printf("%07d\n", p->ID);
count ++;
}
break;
default:
break;
}
}
if(count == 0) puts("Not Found");
}
scanf("%d", &M);
for (int i = 0; i < M; i ++) {
count = 0;
scanf("%d: %[^\n]%c", &query_type, query_str, &space);
printf("%d: %s\n", query_type, query_str);
for (int j = 0; j < N; j++) {
p = booksbyid[j];
switch(query_type) {
case 1:
if (strcmp(query_str, p->title) == 0) {
printf("%07d\n", p->ID);
count ++;
}
break;
case 2:
if (strcmp(query_str, p->author) == 0) {
printf("%07d\n", p->ID);
count ++;
}
break;
case 3:
for (int k = 0; k < 5; k ++)
if (strcmp(query_str, p->keywords[k]) == 0) {
printf("%07d\n", p->ID);
count ++;
}
break;
case 4:
if (strcmp(query_str, p->publisher) == 0) {
printf("%07d\n", p->ID);
count ++;
}
break;
case 5:
sscanf(query_str, "%d", &query_year);
if (query_year == p->year) {
printf("%07d\n", p->ID);
count ++;
}
break;
default:
break;
}
}
if (count == 0)
puts("Not Found");
}
return 0;
return 0;
}
+38 -41
View File
@@ -3,60 +3,57 @@
void reverse(char n[])
{
char temp;
int len = strlen(n);
for(int i = 0; i < len / 2; i++)
{
temp = n[i];
n[i] = n[len - i - 1];
n[len - i - 1] = temp;
}
char temp;
int len = strlen(n);
for (int i = 0; i < len / 2; i++) {
temp = n[i];
n[i] = n[len - i - 1];
n[len - i - 1] = temp;
}
}
void doubleLargeNumber(char n[], char n2[])
{
int l, s = 0, len = strlen(n);
int l, s = 0, len = strlen(n);
for(int i = 0; i < len; i++)
{
s += 2 * (n[i] - '0');
l = s / 10;
s %= 10;
n2[i] = s + '0';
for (int i = 0; i < len; i++) {
s += 2 * (n[i] - '0');
l = s / 10;
s %= 10;
n2[i] = s + '0';
s = l;
}
s = l;
}
if(s)
n2[len] = s + '0';
if (s)
n2[len] = s + '0';
}
int main()
{
char N[22] = {0}, N2[22] = {0};
int diffcount[10] = {0};
char N[22] = {0}, N2[22] = {0};
int diffcount[10] = {0};
scanf("%s", N);
/* store number in reverse order */
reverse(N);
doubleLargeNumber(N, N2);
/* reverse back (just need N2) for printing */
reverse(N2);
scanf("%s", N);
/* store number in reverse order */
reverse(N);
doubleLargeNumber(N, N2);
/* reverse back (just need N2) for printing */
reverse(N2);
/* Count the difference of counts of digits 0-9 */
for(int i = 0; i < strlen(N); i++)
diffcount[N[i] - '0']++;
for(int i = 0; i < strlen(N2); i++)
diffcount[N2[i] - '0']--;
/* Count the difference of counts of digits 0-9 */
for (int i = 0; i < strlen(N); i++)
diffcount[N[i] - '0']++;
for (int i = 0; i < strlen(N2); i++)
diffcount[N2[i] - '0']--;
/* Check */
for(int i = 0; i < 10; i++)
if(diffcount[i])
{
printf("No\n%s", N2);
return 0;
}
printf("Yes\n%s", N2);
/* Check */
for (int i = 0; i < 10; i++)
if (diffcount[i]) {
printf("No\n%s", N2);
return 0;
}
printf("Yes\n%s", N2);
return 0;
return 0;
}
+36 -39
View File
@@ -3,63 +3,60 @@
int isPalindromic(char n[])
{
int len = strlen(n);
for(int i = 0; i < len / 2; i++)
if(n[i] != n[len - i - 1])
return 0;
return 1;
int len = strlen(n);
for (int i = 0; i < len / 2; i++)
if (n[i] != n[len - i - 1])
return 0;
return 1;
}
char* reverse(char n[])
{
char temp;
int len = strlen(n);
for(int i = 0; i < len / 2; i++)
{
temp = n[i];
n[i] = n[len - i - 1];
n[len - i - 1] = temp;
}
return n;
char temp;
int len = strlen(n);
for (int i = 0; i < len / 2; i++) {
temp = n[i];
n[i] = n[len - i - 1];
n[len - i - 1] = temp;
}
return n;
}
/* only works when a and b are of the same length */
void addAtoB(char a[], char b[])
{
int l, s = 0, len = strlen(a);
int l, s = 0, len = strlen(a);
for(int i = 0; i < len; i++)
{
s += (a[i] - '0') + (b[i] - '0');
l = s / 10;
s %= 10;
b[i] = s + '0';
for (int i = 0; i < len; i++) {
s += (a[i] - '0') + (b[i] - '0');
l = s / 10;
s %= 10;
b[i] = s + '0';
s = l;
}
s = l;
}
if(s)
b[len] = s + '0';
if (s)
b[len] = s + '0';
}
int main()
{
int K, steps;
char s1[100] = {0}, s2[100] = {0}, *n = s1, *m = s2;
int K, steps;
char s1[100] = {0}, s2[100] = {0}, *n = s1, *m = s2;
scanf("%s %d", n, &K);
reverse(n);
scanf("%s %d", n, &K);
reverse(n);
for(steps = 0; steps < K && !isPalindromic(n); steps++)
{
/* change 'm' into reverse of 'n' */
strncpy(m, n, 100);
reverse(m);
/* add n and reversed n, and keep the result in n */
addAtoB(m, n);
}
for (steps = 0; steps < K && !isPalindromic(n); steps++) {
/* change 'm' into reverse of 'n' */
strncpy(m, n, 100);
reverse(m);
/* add n and reversed n, and keep the result in n */
addAtoB(m, n);
}
printf("%s\n%d", reverse(n), steps);
printf("%s\n%d", reverse(n), steps);
return 0;
return 0;
}
+49 -53
View File
@@ -4,71 +4,67 @@
/* registration number, location number, total score, final rank, local rank */
typedef struct {
char reg[14];
int loc, score, rank, lrank;
char reg[14];
int loc, score, rank, lrank;
} Testee, *pTestee;
int cmp(const void *a, const void *b)
{
pTestee p1 = *(pTestee*)a, p2 = *(pTestee*)b;
if(p1->score != p2->score)
return p2->score - p1->score;
else
return strcmp(p1->reg, p2->reg);
pTestee p1 = *(pTestee*)a, p2 = *(pTestee*)b;
if (p1->score != p2->score)
return p2->score - p1->score;
else
return strcmp(p1->reg, p2->reg);
}
int main()
{
int N, K;
Testee buffers[100 * 300] = {0}, *q = buffers;
pTestee testees[100 * 300] = {0}, *p = testees;
int N, K;
Testee buffers[100 * 300] = {0}, *q = buffers;
pTestee testees[100 * 300] = {0}, *p = testees;
scanf("%d", &N);
for(int n = 0; n < N; n++)
{
/* read one test location */
scanf("%d", &K);
for(int k = 0; k < K; k++, p++)
{
*p = q++; /* use a new cell */
(*p)->loc = n + 1;
scanf("%s %d", (*p)->reg, &(*p)->score);
}
scanf("%d", &N);
for (int n = 0; n < N; n++) {
/* read one test location */
scanf("%d", &K);
for (int k = 0; k < K; k++, p++) {
*p = q++; /* use a new cell */
(*p)->loc = n + 1;
scanf("%s %d", (*p)->reg, &(*p)->score);
}
/* sort the kth location sublist */
qsort(p - K, K, sizeof(pTestee), cmp);
/* sort the kth location sublist */
qsort(p - K, K, sizeof(pTestee), cmp);
/* get the local rank, i is the offset (we need left K cells of p) */
int lrank = 1;
pTestee *lp;
(*(p - K))->lrank = lrank; /* boundary special case */
for(int i = 1; i < K; i++)
{
lp = p - K + i;
if((*lp)->score != (*(lp - 1))->score)
lrank = i + 1;
(*lp)->lrank = lrank;
}
}
/* get the local rank, i is the offset (we need left K cells of p) */
int lrank = 1;
pTestee *lp;
(*(p - K))->lrank = lrank; /* boundary special case */
for (int i = 1; i < K; i++) {
lp = p - K + i;
if ((*lp)->score != (*(lp - 1))->score)
lrank = i + 1;
(*lp)->lrank = lrank;
}
}
/* sort the whole list */
qsort(testees, p - testees, sizeof(pTestee), cmp);
/* get the final rank */
int rank = 1;
pTestee *fp;
(*testees)->rank = rank;
for(int i = 1; testees + i < p; i++)
{
fp = testees + i;
if((*fp)->score != (*(fp - 1))->score)
rank = i + 1;
(*fp)->rank = rank;
}
/* sort the whole list */
qsort(testees, p - testees, sizeof(pTestee), cmp);
/* get the final rank */
int rank = 1;
pTestee *fp;
(*testees)->rank = rank;
for (int i = 1; testees + i < p; i++) {
fp = testees + i;
if ((*fp)->score != (*(fp - 1))->score)
rank = i + 1;
(*fp)->rank = rank;
}
/* output */
printf("%ld\n", p - testees);
for(pTestee* i = testees; i < p; i++)
printf("%s %d %d %d\n", (*i)->reg, (*i)->rank, (*i)->loc, (*i)->lrank);
/* output */
printf("%ld\n", p - testees);
for (pTestee* i = testees; i < p; i++)
printf("%s %d %d %d\n", (*i)->reg, (*i)->rank, (*i)->loc, (*i)->lrank);
return 0;
return 0;
}
+8 -9
View File
@@ -2,14 +2,13 @@
int main()
{
int n, DIGITS = 3;
char convert[] = "0123456789ABC";
printf("#");
int n, DIGITS = 3;
char convert[] = "0123456789ABC";
printf("#");
for(int i = 0; i < DIGITS; i++)
{
scanf("%d", &n);
printf("%c%c", convert[n / 13], convert[n % 13]);
}
return 0;
for (int i = 0; i < DIGITS; i++) {
scanf("%d", &n);
printf("%c%c", convert[n / 13], convert[n % 13]);
}
return 0;
}
+35 -36
View File
@@ -3,63 +3,62 @@
#include <stdlib.h>
typedef struct {
int ID;
char name[9];
int grade;
int ID;
char name[9];
int grade;
} Student, *pStudent;
/* increasing by ID */
int cmp1(const void *a, const void *b)
{
pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
return s1->ID - s2->ID;
pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
return s1->ID - s2->ID;
}
/* non decreasing by name, then increasing by ID */
int cmp2(const void *a, const void *b)
{
pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
if(strcmp(s1->name, s2->name))
return strcmp(s1->name, s2->name);
else
return s1->ID - s2->ID;
pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
if (strcmp(s1->name, s2->name))
return strcmp(s1->name, s2->name);
else
return s1->ID - s2->ID;
}
/* non decreasing by grade, then increasing by ID */
int cmp3(const void *a, const void *b)
{
pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
if(s1->grade - s2->grade)
return s1->grade - s2->grade;
else
return s1->ID - s2->ID;
pStudent s1 = *(pStudent*)a, s2 = *(pStudent*)b;
if (s1->grade - s2->grade)
return s1->grade - s2->grade;
else
return s1->ID - s2->ID;
}
int main()
{
int N, C;
Student buffer[100000] = {0};
pStudent students[100000] = {0}, *s = students;
int N, C;
Student buffer[100000] = {0};
pStudent students[100000] = {0}, *s = students;
scanf("%d %d", &N, &C);
for(int i = 0; i < N; i++, s++)
{
/* use a new struct buffer */
*s = buffer + i;
scanf("%d %s %d", &(*s)->ID, (*s)->name, &(*s)->grade);
}
scanf("%d %d", &N, &C);
for (int i = 0; i < N; i++, s++) {
/* use a new struct buffer */
*s = buffer + i;
scanf("%d %s %d", &(*s)->ID, (*s)->name, &(*s)->grade);
}
if(C == 1) /* increasing ID */
qsort(students, N, sizeof(pStudent), cmp1);
else if(C == 2) /* non-decreasing name */
qsort(students, N, sizeof(pStudent), cmp2);
else /* C == 3, non-decreasing grade */
qsort(students, N, sizeof(pStudent), cmp3);
if (C == 1) /* increasing ID */
qsort(students, N, sizeof(pStudent), cmp1);
else if (C == 2) /* non-decreasing name */
qsort(students, N, sizeof(pStudent), cmp2);
else /* C == 3, non-decreasing grade */
qsort(students, N, sizeof(pStudent), cmp3);
for(int i = 0; i < N; i++)
printf("%06d %s %d\n", students[i]->ID,
students[i]->name,
students[i]->grade);
for (int i = 0; i < N; i++)
printf("%06d %s %d\n", students[i]->ID,
students[i]->name,
students[i]->grade);
return 0;
return 0;
}
+32 -34
View File
@@ -3,43 +3,41 @@
int main()
{
int N1, N2, seq[200001], *p = seq, n, c = 0, median = 0;
int N1, N2, seq[200001], *p = seq, n, c = 0, median = 0;
/* Read one sequence into array */
scanf("%d", &N1);
for(int i = 0; i < N1; i++)
scanf("%d", seq + i);
/* Boundary setup */
seq[N1] = INT_MAX;
/* Read one sequence into array */
scanf("%d", &N1);
for (int i = 0; i < N1; i++)
scanf("%d", seq + i);
/* Boundary setup */
seq[N1] = INT_MAX;
/* Read the other sequence on the fly while doing merge sort.
* This is due to the memory limitation of the problem (1.5MB) */
scanf("%d", &N2);
/* read the first one */
scanf("%d", &n);
/* just enough to find the median: skip the first half */
for(int i = 0; i < (N1 + N2 + 1) / 2; i++)
{
/* Record the smallest of the two */
if(*p < n)
median = *p;
else
median = n;
/* Read the other sequence on the fly while doing merge sort.
* This is due to the memory limitation of the problem (1.5MB) */
scanf("%d", &N2);
/* read the first one */
scanf("%d", &n);
/* just enough to find the median: skip the first half */
for (int i = 0; i < (N1 + N2 + 1) / 2; i++) {
/* Record the smallest of the two */
if (*p < n)
median = *p;
else
median = n;
/* Either point p at the next number or read a new number into n */
if((*p < n && p < seq + N1) || (*p > n && c == N2))
p++;
else
{
if(c == N2 - 1)
n = INT_MAX;
else
scanf("%d", &n);
c++;
}
}
/* Either point p at the next number or read a new number into n */
if ((*p < n && p < seq + N1) || (*p > n && c == N2)) {
p++;
} else {
if (c == N2 - 1)
n = INT_MAX;
else
scanf("%d", &n);
c++;
}
}
printf("%d", median);
printf("%d", median);
return 0;
return 0;
}
+19 -20
View File
@@ -3,28 +3,27 @@
int main()
{
int N, n1, n2, e;
char string[81];
int N, n1, n2, e;
char string[81];
scanf("%s", string);
N = strlen(string);
scanf("%s", string);
N = strlen(string);
/* the math is: n1 = n3; n2 >= n1; n2 + 2n1 = N + 2 */
e = ((N % 3) + 2) % 3;
n1 = (N + 2 - e) / 3;
n2 = n1 + e;
/* the math is: n1 = n3; n2 >= n1; n2 + 2n1 = N + 2 */
e = ((N % 3) + 2) % 3;
n1 = (N + 2 - e) / 3;
n2 = n1 + e;
for(int i = 0; i < n1; i++)
{
putchar(string[i]);
for(int j = 0; j < n2 - 2; j++)
if(i < n1 - 1)
putchar(' ');
else
putchar(string[n1 + j]);
putchar(string[N - 1 - i]);
putchar('\n');
}
for (int i = 0; i < n1; i++) {
putchar(string[i]);
for (int j = 0; j < n2 - 2; j++)
if (i < n1 - 1)
putchar(' ');
else
putchar(string[n1 + j]);
putchar(string[N - 1 - i]);
putchar('\n');
}
return 0;
return 0;
}
+26 -28
View File
@@ -2,40 +2,38 @@
typedef struct Node Node;
struct Node {
int checked;
struct Node *next;
int checked;
struct Node *next;
};
int main()
{
char data;
int start1, start2, address, next, N;
Node list[100000] = {0}, *p;
char data;
int start1, start2, address, next, N;
Node list[100000] = {0}, *p;
scanf("%d %d %d", &start1, &start2, &N);
/* record a linked list */
for(int i = 0; i < N; i++)
{
scanf("%d %c %d", &address, &data, &next);
list[address].next = next == -1 ? NULL : &list[next];
}
scanf("%d %d %d", &start1, &start2, &N);
/* record a linked list */
for (int i = 0; i < N; i++) {
scanf("%d %c %d", &address, &data, &next);
list[address].next = next == -1 ? NULL : &list[next];
}
if(start1 == -1 || start2 == -1)
{
printf("-1");
return 0;
}
/* First traverse the first string */
for(p = list + start1; p; p = p->next)
p->checked = 1;
/* Then traverse the second looking for checked node */
for(p = list + start2; p && !p->checked; p = p->next)
;
if (start1 == -1 || start2 == -1) {
printf("-1");
return 0;
}
/* First traverse the first string */
for (p = list + start1; p; p = p->next)
p->checked = 1;
/* Then traverse the second looking for checked node */
for (p = list + start2; p && !p->checked; p = p->next)
;
if(p)
printf("%05ld", p - list);
else
printf("-1");
if (p)
printf("%05ld", p - list);
else
printf("-1");
return 0;
return 0;
}
+30 -36
View File
@@ -2,43 +2,37 @@
int main()
{
int N, count = 0, modified;
char username[1000][11], password[1000][11];
int N, count = 0, modified;
char username[1000][11], password[1000][11];
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
modified = 0;
scanf("%s %s", username[i], password[i]);
for(char *p = password[i]; *p; p++)
{
switch(*p)
{
case '1': *p = '@'; modified = 1; break;
case '0': *p = '%'; modified = 1; break;
case 'l': *p = 'L'; modified = 1; break;
case 'O': *p = 'o'; modified = 1; break;
default: break;
}
}
if(modified)
count++;
else /* mark unmodified password */
password[i][0] = '\0';
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
modified = 0;
scanf("%s %s", username[i], password[i]);
for (char *p = password[i]; *p; p++) {
switch(*p) {
case '1': *p = '@'; modified = 1; break;
case '0': *p = '%'; modified = 1; break;
case 'l': *p = 'L'; modified = 1; break;
case 'O': *p = 'o'; modified = 1; break;
default: break;
}
}
if (modified)
count++;
else /* mark unmodified password */
password[i][0] = '\0';
}
if(count)
{
printf("%d\n", count);
for(int i = 0; i < N; i++)
if(password[i][0] != '\0')
printf("%s %s\n", username[i], password[i]);
}
else
{
printf("There %s %d %s and no account is modified",
N > 1 ? "are" : "is", N, N > 1 ? "accounts" : "account");
}
if (count) {
printf("%d\n", count);
for (int i = 0; i < N; i++)
if (password[i][0] != '\0')
printf("%s %s\n", username[i], password[i]);
} else {
printf("There %s %d %s and no account is modified",
N > 1 ? "are" : "is", N, N > 1 ? "accounts" : "account");
}
return 0;
return 0;
}
+27 -28
View File
@@ -1,41 +1,40 @@
#include <stdio.h>
typedef struct student {
char name[11];
char gender;
char ID[11];
char name[11];
char gender;
char ID[11];
} Student;
int main()
{
int N, grade, gradeF = -1, gradeM = 101;
Student students[101] = {0}, s;
int N, grade, gradeF = -1, gradeM = 101;
Student students[101] = {0}, s;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%s %c %s %d", s.name, &s.gender, s.ID, &grade);
students[grade] = s;
if(s.gender == 'F')
gradeF = grade > gradeF ? grade : gradeF;
else
gradeM = grade < gradeM ? grade : gradeM;
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%s %c %s %d", s.name, &s.gender, s.ID, &grade);
students[grade] = s;
if (s.gender == 'F')
gradeF = grade > gradeF ? grade : gradeF;
else
gradeM = grade < gradeM ? grade : gradeM;
}
if(gradeF != -1)
printf("%s %s\n", students[gradeF].name, students[gradeF].ID);
else
printf("Absent\n");
if (gradeF != -1)
printf("%s %s\n", students[gradeF].name, students[gradeF].ID);
else
printf("Absent\n");
if(gradeM != 101)
printf("%s %s\n", students[gradeM].name, students[gradeM].ID);
else
printf("Absent\n");
if (gradeM != 101)
printf("%s %s\n", students[gradeM].name, students[gradeM].ID);
else
printf("Absent\n");
if(gradeM == 101 || gradeF == -1)
printf("NA");
else
printf("%d", gradeF - gradeM);
if (gradeM == 101 || gradeF == -1)
printf("NA");
else
printf("%d", gradeF - gradeM);
return 0;
return 0;
}
+17 -17
View File
@@ -5,25 +5,25 @@ int cmp(const void *a, const void *b) { return *(int*)b - *(int*)a; }
int main()
{
int NC, NP;
long sum = 0, coupon[100000] = {0}, product[100000] = {0};
int NC, NP;
long sum = 0, coupon[100000] = {0}, product[100000] = {0};
/* read and sort */
scanf("%d", &NC);
for(int i = 0; i < NC; i++)
scanf("%ld", coupon + i);
qsort(coupon, NC, sizeof(long), cmp);
scanf("%d", &NP);
for(int i = 0; i < NP; i++)
scanf("%ld", product + i);
qsort(product, NP, sizeof(long), cmp);
/* read and sort */
scanf("%d", &NC);
for (int i = 0; i < NC; i++)
scanf("%ld", coupon + i);
qsort(coupon, NC, sizeof(long), cmp);
scanf("%d", &NP);
for (int i = 0; i < NP; i++)
scanf("%ld", product + i);
qsort(product, NP, sizeof(long), cmp);
for(int i = 0; coupon[i] > 0 && product[i] > 0; i++)
sum += coupon[i] * product[i];
for(int i = 0; coupon[NC - i - 1] < 0 && product[NP - i - 1] < 0; i++)
sum += coupon[NC - i - 1] * product[NP - i - 1];
for (int i = 0; coupon[i] > 0 && product[i] > 0; i++)
sum += coupon[i] * product[i];
for (int i = 0; coupon[NC - i - 1] < 0 && product[NP - i - 1] < 0; i++)
sum += coupon[NC - i - 1] * product[NP - i - 1];
printf("%ld\n", sum);
printf("%ld\n", sum);
return 0;
return 0;
}
+25 -25
View File
@@ -4,39 +4,39 @@
int cmp(const void *a, const void *b)
{
char *A = (char*)a, *B = (char*)b;
char s1[17], s2[17];
int lenA = strlen(A), lenB = strlen(B);
char *A = (char*)a, *B = (char*)b;
char s1[17], s2[17];
int lenA = strlen(A), lenB = strlen(B);
/* Repeat the numbers to form a long string
* e.g. 123 -> 123123123123123 */
for(int i = 0; i < 16 / lenA; i++)
strcpy(s1 + i * lenA, A);
for(int i = 0; i < 16 / lenB; i++)
strcpy(s2 + i * lenB, B);
/* Repeat the numbers to form a long string
* e.g. 123 -> 123123123123123 */
for (int i = 0; i < 16 / lenA; i++)
strcpy(s1 + i * lenA, A);
for (int i = 0; i < 16 / lenB; i++)
strcpy(s2 + i * lenB, B);
return strcmp(s1, s2);
return strcmp(s1, s2);
}
int main()
{
int N, k = 0, number = 0;
char numbers[10000][9] = {{0}};
int N, k = 0, number = 0;
char numbers[10000][9] = {{0}};
scanf("%d", &N);
for(int i = 0; i < N; i++)
scanf("%s", numbers[i]);
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%s", numbers[i]);
qsort(numbers, N, 9 * sizeof(char), cmp);
qsort(numbers, N, 9 * sizeof(char), cmp);
/* Go to the first non-zero segment */
while(k < N && number == 0)
sscanf(numbers[k++], "%d", &number);
/* Go to the first non-zero segment */
while (k < N && number == 0)
sscanf(numbers[k++], "%d", &number);
/* Print the first non-zero segment */
printf("%d", number);
/* Print the rest */
while(k < N)
printf("%s", numbers[k++]);
return 0;
/* Print the first non-zero segment */
printf("%d", number);
/* Print the rest */
while (k < N)
printf("%s", numbers[k++]);
return 0;
}
+18 -23
View File
@@ -3,30 +3,25 @@
int main()
{
int maxlen = 0, l, L;
char string[1002];
int maxlen = 0, l, L;
char string[1002];
fgets(string, 1001, stdin);
fgets(string, 1001, stdin);
L = strlen(string);
for(int i = 0; i < L; i++)
{
/* odd symmetric */
l = 0;
while(i - l >= 0
&& i + l < L
&& string[i - l] == string[i + l])
l++;
maxlen = (l * 2 - 1) > maxlen ? (l * 2 - 1) : maxlen;
/* even symmetric */
l = 0;
while(i - l >= 0
&& i + l + 1 < L
&& string[i - l] == string[i + l + 1])
l++;
maxlen = (l * 2) > maxlen ? (l * 2) : maxlen;
}
L = strlen(string);
for (int i = 0; i < L; i++) {
/* odd symmetric */
l = 0;
while (i - l >= 0 && i + l < L && string[i - l] == string[i + l])
l++;
maxlen = (l * 2 - 1) > maxlen ? (l * 2 - 1) : maxlen;
/* even symmetric */
l = 0;
while (i - l >= 0 && i + l + 1 < L && string[i - l] == string[i + l + 1])
l++;
maxlen = (l * 2) > maxlen ? (l * 2) : maxlen;
}
printf("%d", maxlen);
return 0;
printf("%d", maxlen);
return 0;
}
+13 -15
View File
@@ -2,22 +2,20 @@
int main()
{
int N, counts[10001] = {0}, bets[100000] = {0};
int N, counts[10001] = {0}, bets[100000] = {0};
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%d", bets + i);
counts[bets[i]]++;
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", bets + i);
counts[bets[i]]++;
}
for(int i = 0; i < N; i++)
if(counts[bets[i]] == 1)
{
printf("%d", bets[i]);
return 0;
}
for (int i = 0; i < N; i++)
if (counts[bets[i]] == 1) {
printf("%d", bets[i]);
return 0;
}
printf("None");
return 0;
printf("None");
return 0;
}
+17 -17
View File
@@ -2,25 +2,25 @@
int main()
{
int count, order[54], deck[54], pos;
char types[] = "SHCDJ";
int count, order[54], deck[54], pos;
char types[] = "SHCDJ";
scanf("%d", &count);
for(int i = 0; i < 54; i++)
scanf("%d", order + i);
scanf("%d", &count);
for (int i = 0; i < 54; i++)
scanf("%d", order + i);
for(int i = 0; i < 54; i++)
{
pos = i;
for(int j = 0; j < count; j++)
pos = order[pos] - 1; /* the input order[] was 1 based */
deck[pos] = i;
}
for (int i = 0; i < 54; i++) {
pos = i;
for (int j = 0; j < count; j++)
pos = order[pos] - 1; /* the input order[] was 1 based */
deck[pos] = i;
}
for(int i = 0; i < 54; i++)
printf("%c%d%c", types[deck[i] / 13],
deck[i] % 13 + 1,
i == 53 ? '\0' : ' ');
for (int i = 0; i < 54; i++) {
printf("%c", types[deck[i] / 13]);
printf("%d", deck[i] % 13 + 1);
printf("%c", i == 53 ? '\0' : ' ');
}
return 0;
return 0;
}
+28 -31
View File
@@ -2,39 +2,36 @@
int main()
{
int N, M, D[100000] = {0}, pair[100000][2] = {{0}};
int N, M, D[100000] = {0}, pair[100000][2] = {{0}};
scanf("%d %d", &N, &M);
for(int n = 0; n < N; n++)
scanf("%d", D + n);
scanf("%d %d", &N, &M);
for (int n = 0; n < N; n++)
scanf("%d", D + n);
int i = 0, j = 0, sum = D[0], min = 100000000, count = 0;
while(1)
{
if(sum <= min && sum >= M)
{
/* reset if found lower solution */
if(sum < min)
{
min = sum;
count = 0;
}
/* record */
pair[count][0] = i;
pair[count][1] = j;
count ++;
}
/* Find closest solution */
if(sum <= M && j < N - 1)
sum += D[++j];
else if(i < N - 1)
sum -= D[i++];
else
break;
}
int i = 0, j = 0, sum = D[0], min = 100000000, count = 0;
while (1) {
if (sum <= min && sum >= M) {
/* reset if found lower solution */
if (sum < min) {
min = sum;
count = 0;
}
/* record */
pair[count][0] = i;
pair[count][1] = j;
count ++;
}
/* Find closest solution */
if (sum <= M && j < N - 1)
sum += D[++j];
else if (i < N - 1)
sum -= D[i++];
else
break;
}
for(int n = 0; n < count; n++)
printf("%d-%d\n", pair[n][0] + 1, pair[n][1] + 1);
for (int n = 0; n < count; n++)
printf("%d-%d\n", pair[n][0] + 1, pair[n][1] + 1);
return 0;
return 0;
}
+31 -33
View File
@@ -2,40 +2,38 @@
int main()
{
int N, M, L, c, order_to_color[200] = {0}, color_to_order[200] = {0};
int longest_for_color[200] = {0}, longest_before_current, max_length = 0;
int N, M, L, c, order_to_color[200] = {0}, color_to_order[200] = {0};
int longest_for_color[200] = {0}, longest_before_current, max_length = 0;
/* read number of different colors */
scanf("%d", &N);
/* read color-order pairs (colors start from 0, orders start from 1) */
scanf("%d", &M);
for(int i = M; i > 0; i--)
{
scanf("%d", &c);
order_to_color[i] = --c;
color_to_order[c] = i;
}
/* read color list */
scanf("%d", &L);
for(int i = 0; i < L; i++)
{
scanf("%d", &c);
/* this color is not in the favourite list, do nothing */
if(color_to_order[--c] == 0)
continue;
/* for colors of higher/equal order, find the longest sub-stripe */
longest_before_current = 0;
for(int j = M; j >= color_to_order[c]; j--)
if(longest_before_current < longest_for_color[order_to_color[j]])
longest_before_current = longest_for_color[order_to_color[j]];
/* after this color, the length increases by 1 */
longest_for_color[c] = longest_before_current + 1;
}
/* read number of different colors */
scanf("%d", &N);
/* read color-order pairs (colors start from 0, orders start from 1) */
scanf("%d", &M);
for (int i = M; i > 0; i--) {
scanf("%d", &c);
order_to_color[i] = --c;
color_to_order[c] = i;
}
/* read color list */
scanf("%d", &L);
for (int i = 0; i < L; i++) {
scanf("%d", &c);
/* this color is not in the favourite list, do nothing */
if (color_to_order[--c] == 0)
continue;
/* for colors of higher/equal order, find the longest sub-stripe */
longest_before_current = 0;
for (int j = M; j >= color_to_order[c]; j--)
if (longest_before_current < longest_for_color[order_to_color[j]])
longest_before_current = longest_for_color[order_to_color[j]];
/* after this color, the length increases by 1 */
longest_for_color[c] = longest_before_current + 1;
}
for(int i = 0; i < N; i++)
if(max_length < longest_for_color[i])
max_length = longest_for_color[i];
printf("%d\n", max_length);
for (int i = 0; i < N; i++)
if (max_length < longest_for_color[i])
max_length = longest_for_color[i];
printf("%d\n", max_length);
return 0;
return 0;
}
+15 -17
View File
@@ -2,24 +2,22 @@
int main()
{
int N, M, start, end, d, D[100001] = {0};
int N, M, start, end, d, D[100001] = {0};
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%d", D + i + 1);
/* record the distance between the current and the first exits */
D[i + 1] += D[i];
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", D + i + 1);
/* record the distance between the current and the first exits */
D[i + 1] += D[i];
}
scanf("%d", &M);
for(int i = 0; i < M; i++)
{
scanf("%d %d", &start, &end);
d = --start > --end ? D[start] - D[end] : D[end] - D[start];
/* if the distance is more than half total length, the other direction will be shorter */
printf("%d\n", d * 2 > D[N] ? D[N] - d : d);
}
scanf("%d", &M);
for (int i = 0; i < M; i++) {
scanf("%d %d", &start, &end);
d = --start > --end ? D[start] - D[end] : D[end] - D[start];
/* if the distance is more than half total length, the other direction will be shorter */
printf("%d\n", d * 2 > D[N] ? D[N] - d : d);
}
return 0;
return 0;
}
+15 -18
View File
@@ -2,25 +2,22 @@
int main()
{
int N, M, counts[501] = {0}, coin;
int N, M, counts[501] = {0}, coin;
scanf("%d %d", &N, &M);
for(int i = 0; i < N; i++)
{
scanf("%d", &coin);
counts[coin]++;
}
scanf("%d %d", &N, &M);
for (int i = 0; i < N; i++) {
scanf("%d", &coin);
counts[coin]++;
}
for(int i = 1; 2 * i - 1 < M; i++)
{
if((i * 2 == M && counts[i] > 1)
|| (i * 2 != M && M - i < 501 && counts[i] && counts[M - i]))
{
printf("%d %d", i, M - i);
return 0;
}
}
for (int i = 1; 2 * i - 1 < M; i++) {
if ((i * 2 == M && counts[i] > 1)
|| (i * 2 != M && M - i < 501 && counts[i] && counts[M - i])) {
printf("%d %d", i, M - i);
return 0;
}
}
printf("No Solution");
return 0;
printf("No Solution");
return 0;
}
+17 -18
View File
@@ -2,26 +2,25 @@
int main()
{
int N, base10, lo, curr, hi, count = 0;
int N, base10, lo, curr, hi, count = 0;
scanf("%d", &N);
scanf("%d", &N);
/* for every digits, from lower to higher */
for(base10 = 1; base10 <= N; base10 *= 10)
{
lo = N % base10; /* part of the number after current digit */
hi = N / base10 / 10; /* part of the number before current digit */
curr = N / base10 - hi * 10; /* current digit */
/* count numbers that is smaller than N and has '1' in the current digit */
if(curr == 0) /* count: [0~(hi-1)]1[0~9...9] */
count += hi * base10;
else if(curr == 1) /* count: [0~(hi-1)]1[0~9...9] or [hi]1[0~lo] */
count += lo + 1 + hi * base10;
else /* count: [0~hi]1[0~9...9] */
count += (hi + 1) * base10;
}
/* for every digits, from lower to higher */
for (base10 = 1; base10 <= N; base10 *= 10) {
lo = N % base10; /* part of the number after current digit */
hi = N / base10 / 10; /* part of the number before current digit */
curr = N / base10 - hi * 10; /* current digit */
/* count numbers that is smaller than N and has '1' in the current digit */
if (curr == 0) /* count: [0~(hi-1)]1[0~9...9] */
count += hi * base10;
else if (curr == 1) /* count: [0~(hi-1)]1[0~9...9] or [hi]1[0~lo] */
count += lo + 1 + hi * base10;
else /* count: [0~hi]1[0~9...9] */
count += (hi + 1) * base10;
}
printf("%d\n", count);
printf("%d\n", count);
return 0;
return 0;
}
+9 -9
View File
@@ -2,16 +2,16 @@
int main()
{
char c, s1[10001];
int s2[128] = {0};
char c, s1[10001];
int s2[128] = {0};
scanf("%[^\n]%c", s1, &c);
while((c = getchar()) != EOF && c != '\n')
s2[(int)c] = 1;
scanf("%[^\n]%c", s1, &c);
while ((c = getchar()) != EOF && c != '\n')
s2[(int)c] = 1;
for(char *p = s1; *p; p++)
if(!s2[(int)(*p)])
putchar(*p);
for (char *p = s1; *p; p++)
if (!s2[(int)(*p)])
putchar(*p);
return 0;
return 0;
}
+33 -37
View File
@@ -2,43 +2,39 @@
int main()
{
char c;
int M, N, K, stack[7] = {0}, top, pushed, popped, wanted;
char c;
int M, N, K, stack[7] = {0}, top, pushed, popped, wanted;
scanf("%d %d %d", &M, &N, &K);
for(int i = 0; i < K; i++)
{
top = -1;
pushed = 0;
popped = 0;
/* push */
stack[++top] = ++pushed;
scanf("%d", &wanted);
for( ; ; )
{
if(top != -1 && stack[top] == wanted)
{
/* pop */
stack[top--] = 0;
popped++;
/* read */
if(popped < N)
{
scanf("%d", &wanted);
continue;
}
else
break;
}
/* push */
if(pushed < N && top < M - 1)
stack[++top] = ++pushed;
else
break;
}
printf("%s\n", popped < N ? "NO" : "YES");
while((c = getchar()) != EOF && c != '\n');
}
scanf("%d %d %d", &M, &N, &K);
for (int i = 0; i < K; i++) {
top = -1;
pushed = 0;
popped = 0;
/* push */
stack[++top] = ++pushed;
scanf("%d", &wanted);
for ( ; ; ) {
if (top != -1 && stack[top] == wanted) {
/* pop */
stack[top--] = 0;
popped++;
/* read */
if (popped < N) {
scanf("%d", &wanted);
continue;
}
else
break;
}
/* push */
if (pushed < N && top < M - 1)
stack[++top] = ++pushed;
else
break;
}
printf("%s\n", popped < N ? "NO" : "YES");
while ((c = getchar()) != EOF && c != '\n');
}
return 0;
return 0;
}
+9 -9
View File
@@ -2,16 +2,16 @@
int main()
{
int number, steps;
scanf("%d", &number);
int number, steps;
scanf("%d", &number);
for(steps = 0; number != 1; steps++)
if(number % 2 == 0)
number /= 2;
else
number = (3 * number + 1) / 2;
for (steps = 0; number != 1; steps++)
if (number % 2 == 0)
number /= 2;
else
number = (3 * number + 1) / 2;
printf("%d", steps);
printf("%d", steps);
return 0;
return 0;
}
+11 -11
View File
@@ -2,18 +2,18 @@
int main()
{
int sum = 0;
char c, *pinyins[] = {"ling", "yi", "er", "san", "si",
"wu", "liu", "qi", "ba", "jiu"};
int sum = 0;
char c, *pinyins[] = {"ling", "yi", "er", "san", "si",
"wu", "liu", "qi", "ba", "jiu"};
while((c = getchar()) != EOF && c != '\n')
sum += c - '0';
while ((c = getchar()) != EOF && c != '\n')
sum += c - '0';
if(sum / 100) /* hundreds */
printf("%s ", pinyins[sum / 100]);
if(sum / 10) /* tens */
printf("%s ", pinyins[sum / 10 % 10]);
printf("%s", pinyins[sum % 10]); /* units */
if (sum / 100) /* hundreds */
printf("%s ", pinyins[sum / 100]);
if (sum / 10) /* tens */
printf("%s ", pinyins[sum / 10 % 10]);
printf("%s", pinyins[sum % 10]); /* units */
return 0;
return 0;
}
+32 -29
View File
@@ -2,36 +2,39 @@
int main()
{
char c;
int num;
scanf("%d", &num);
while((c = getchar()) != '\n'); /* read the rest of the line,
make sure to start from a new line later */
for(int i = 0; i < num; i++)
{
/* pos: indicate the position of current index for count[] */
/* count[3]: number of As; before P, between P & T and after T */
int pos = 0, count[3] = {0, 0, 0};
while((c = getchar()) != EOF)
{
if(c == 'A') count[pos]++; /* count 'A's */
else if(c == 'P' && pos == 0) pos = 1; /* one P before T */
else if(c == 'T' && pos == 1) pos = 2; /* one T after P */
else break; /* 'wrong' string */
}
char c;
int num;
scanf("%d", &num);
/* read the rest of the line, make sure to start from a new line later */
while ((c = getchar()) != '\n');
if(c == '\n' /* 1. no other characters at end */
&& pos == 2 /* 2. appearance of 'P' and 'T' */
&& count[1] /* 3. existance of 'A' */
&& count[2] == count[1] * count[0]) /* 4. relation between numbers */
puts("YES");
else
puts("NO");
for (int i = 0; i < num; i++) {
/* pos: indicate the position of current index for count[] */
/* count[3]: number of As; before P, between P & T and after T */
int pos = 0, count[3] = {0, 0, 0};
while ((c = getchar()) != EOF) {
if (c == 'A') /* count 'A's */
count[pos]++;
else if (c == 'P' && pos == 0) /* one P before T */
pos = 1;
else if (c == 'T' && pos == 1) /* one T after P */
pos = 2;
else /* 'wrong' string */
break;
}
/* read the rest of the line */
if(c != '\n')
while((c = getchar()) != EOF && c != '\n');
}
if (c == '\n' /* 1. no other characters at end */
&& pos == 2 /* 2. appearance of 'P' and 'T' */
&& count[1] /* 3. existance of 'A' */
&& count[2] == count[1] * count[0]) /* 4. relation between numbers */
puts("YES");
else
puts("NO");
return 0;
/* read the rest of the line */
if (c != '\n')
while ((c = getchar()) != EOF && c != '\n');
}
return 0;
}
+20 -23
View File
@@ -3,32 +3,29 @@
int main()
{
int N;
scanf("%d", &N);
int N;
scanf("%d", &N);
char maxname[11], minname[11], curname[11],
maxid[11], minid[11], curid[11];
int maxgrade = -1, mingrade = 101, curgrade;
char maxname[11], minname[11], curname[11],
maxid[11], minid[11], curid[11];
int maxgrade = -1, mingrade = 101, curgrade;
for(int i = 0; i < N; i++)
{
scanf("%s %s %d", curname, curid, &curgrade);
for (int i = 0; i < N; i++) {
scanf("%s %s %d", curname, curid, &curgrade);
if(curgrade > maxgrade)
{
strcpy(maxname, curname);
strcpy(maxid, curid);
maxgrade = curgrade;
}
if(curgrade < mingrade)
{
strcpy(minname, curname);
strcpy(minid, curid);
mingrade = curgrade;
}
}
if (curgrade > maxgrade) {
strcpy(maxname, curname);
strcpy(maxid, curid);
maxgrade = curgrade;
}
if (curgrade < mingrade) {
strcpy(minname, curname);
strcpy(minid, curid);
mingrade = curgrade;
}
}
printf("%s %s\n%s %s", maxname, maxid, minname, minid);
printf("%s %s\n%s %s", maxname, maxid, minname, minid);
return 0;
return 0;
}
+24 -27
View File
@@ -2,36 +2,33 @@
int main()
{
int K, n, table[101] = {0};
int K, n, table[101] = {0};
scanf("%d", &K);
for(int i = 0; i < K; i++)
{
scanf("%d", &n);
table[n] = 1;
}
scanf("%d", &K);
for (int i = 0; i < K; i++) {
scanf("%d", &n);
table[n] = 1;
}
/* find numbers needed to test */
for(int i = 1; i <= 100; i++)
if(table[i])
for(int j = i; j > 1; )
{
/* calculate for one step */
if(j % 2) j = (3 * j + 1) / 2;
else j /= 2;
/* find numbers needed to test */
for (int i = 1; i <= 100; i++)
if (table[i])
for (int j = i; j > 1; ) {
/* calculate for one step */
if (j % 2) j = (3 * j + 1) / 2;
else j /= 2;
/* see if the new number is in given numbers */
if(j <= 100 && table[j])
{
table[j] = 0; /* 'covered' this number */
K--; /* one less number not 'covered' */
if(j < i) break; /* did this before, no need going on */
}
}
/* see if the new number is in given numbers */
if (j <= 100 && table[j]) {
table[j] = 0; /* 'covered' this number */
K--; /* one less number not 'covered' */
if (j < i) break; /* did this before, no need going on */
}
}
for(int i = 100; i >= 1; i--)
if(table[i] == 1)
printf("%d%c", i, --K ? ' ' : '\0');
for (int i = 100; i >= 1; i--)
if (table[i] == 1)
printf("%d%c", i, --K ? ' ' : '\0');
return 0;
return 0;
}
+9 -6
View File
@@ -2,12 +2,15 @@
int main()
{
int n;
scanf("%d", &n);
int n;
scanf("%d", &n);
for(int i = 0; i < n / 100; i++) putchar('B');
for(int i = 0; i < n / 10 % 10; i++) putchar('S');
for(int i = 0; i < n % 10; i++) putchar('1' + i);
for (int i = 0; i < n / 100; i++)
putchar('B');
for (int i = 0; i < n / 10 % 10; i++)
putchar('S');
for (int i = 0; i < n % 10; i++)
putchar('1' + i);
return 0;
return 0;
}
+25 -27
View File
@@ -2,36 +2,34 @@
int main()
{
int N;
scanf("%d", &N);
int N;
scanf("%d", &N);
/* Record primality of three successive numbers starting from 2, 3, 4 */
int iPrimeMinus2 = 1, iPrimeMinus1 = 1, iPrime;
int primes[100] = {2, 3}; /* Record the prime numbers before sqrt(10^5) */
int twincount = 0; /* Count of twin primes */
int primecount = 2; /* Count of prime numbers */
/* Record primality of three successive numbers starting from 2, 3, 4 */
int iPrimeMinus2 = 1, iPrimeMinus1 = 1, iPrime;
int primes[100] = {2, 3}; /* Record the prime numbers before sqrt(10^5) */
int twincount = 0; /* Count of twin primes */
int primecount = 2; /* Count of prime numbers */
/* Start from 4 */
for(int i = 4; i <= N; i++)
{
/* Test if i is a prime number */
iPrime = 1;
for(int j = 0; iPrime && primes[j] * primes[j] <= i; j++)
if(i % primes[j] == 0)
iPrime = 0;
/* Start from 4 */
for (int i = 4; i <= N; i++) {
/* Test if i is a prime number */
iPrime = 1;
for (int j = 0; iPrime && primes[j] * primes[j] <= i; j++)
if (i % primes[j] == 0)
iPrime = 0;
/* If i is a prime number, record */
if(iPrime)
{
if(primecount < 100) primes[primecount++] = i;
if(iPrimeMinus2 == 1) twincount++; /* a prime pair found */
}
/* If i is a prime number, record */
if (iPrime) {
if (primecount < 100) primes[primecount++] = i;
if (iPrimeMinus2 == 1) twincount++; /* a prime pair found */
}
/* Shift the primality flags to next numbers */
iPrimeMinus2 = iPrimeMinus1;
iPrimeMinus1 = iPrime;
}
printf("%d", twincount);
/* Shift the primality flags to next numbers */
iPrimeMinus2 = iPrimeMinus1;
iPrimeMinus1 = iPrime;
}
printf("%d", twincount);
return 0;
return 0;
}
+13 -13
View File
@@ -2,21 +2,21 @@
int main()
{
int N, M, numbers[100];
int N, M, numbers[100];
scanf("%d %d", &N, &M);
M %= N; /* M could be larger than N */
scanf("%d %d", &N, &M);
M %= N; /* M could be larger than N */
/* Read */
for(int i = 0; i < N; i++)
scanf("%d", &numbers[i]);
/* Read */
for (int i = 0; i < N; i++)
scanf("%d", &numbers[i]);
/* Print */
for(int i = N - M; i < N; i++) /* Print N - M to N - 1 */
printf("%d ", numbers[i]);
for(int i = 0; i < N - M - 1; i++) /* Print 0 to N - M - 2 */
printf("%d ", numbers[i]);
printf("%d", numbers[N - M - 1]); /* Print N - M - 1, no blankspace */
/* Print */
for (int i = N - M; i < N; i++) /* Print N - M to N - 1 */
printf("%d ", numbers[i]);
for (int i = 0; i < N - M - 1; i++) /* Print 0 to N - M - 2 */
printf("%d ", numbers[i]);
printf("%d", numbers[N - M - 1]); /* Print N - M - 1, no blankspace */
return 0;
return 0;
}
+11 -12
View File
@@ -3,19 +3,18 @@
int main()
{
char s[82], *p;
char s[82], *p;
scanf("%[^\n]", s);
scanf("%[^\n]", s);
for(p = s + strlen(s) - 1; p >= s; p--)
{
if(*(p - 1) == ' ')
printf("%s ", p);
if(p == s)
printf("%s", p);
if(*p == ' ')
*p = '\0';
}
for (p = s + strlen(s) - 1; p >= s; p--) {
if (*(p - 1) == ' ')
printf("%s ", p);
if (p == s)
printf("%s", p);
if (*p == ' ')
*p = '\0';
}
return 0;
return 0;
}
+11 -13
View File
@@ -2,20 +2,18 @@
int main()
{
int coef, index, count = 0;
int coef, index, count = 0;
while(scanf("%d %d", &coef, &index) != EOF)
{
if(index) /* Constant terms result in zero */
{
if(count++) putchar(' ');
printf("%d %d", coef * index, index - 1);
}
}
while (scanf("%d %d", &coef, &index) != EOF) {
if (index) { /* Constant terms result in zero */
if (count++) putchar(' ');
printf("%d %d", coef * index, index - 1);
}
}
/* Zero polynomial or constant */
if(count == 0)
puts("0 0");
/* Zero polynomial or constant */
if (count == 0)
puts("0 0");
return 0;
return 0;
}
+8 -9
View File
@@ -2,15 +2,14 @@
int main()
{
int T;
long int A, B, C;
scanf("%d", &T);
int T;
long int A, B, C;
scanf("%d", &T);
for(int i = 0; i < T; i++)
{
scanf("%ld %ld %ld", &A, &B, &C);
printf("Case #%d: %s\n", i + 1, A + B > C ? "true" : "false");
}
for (int i = 0; i < T; i++) {
scanf("%ld %ld %ld", &A, &B, &C);
printf("Case #%d: %s\n", i + 1, A + B > C ? "true" : "false");
}
return 0;
return 0;
}
+48 -22
View File
@@ -2,29 +2,55 @@
int main()
{
int count;
int A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0;
int A2flag = 0, A4count = 0;
int count;
int A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0;
int A2flag = 0, A4count = 0;
scanf("%d", &count);
for(int i = 0, n; i < count; i++)
{
scanf("%d", &n);
switch(n % 5)
{
case 0: A1 += n % 2 ? 0 : n; break;
case 1: A2flag = A2flag == 1 ? -1 : 1; A2 += A2flag * n; break;
case 2: A3 ++; break;
case 3: A4 += n; A4count ++; break;
case 4: A5 = n > A5? n : A5; break;
}
}
scanf("%d", &count);
for (int i = 0, n; i < count; i++)
{
scanf("%d", &n);
switch(n % 5) {
case 0:
A1 += n % 2 ? 0 : n;
break;
case 1:
A2flag = A2flag == 1 ? -1 : 1;
A2 += A2flag * n;
break;
case 2:
A3 ++;
break;
case 3:
A4 += n;
A4count ++;
break;
case 4:
A5 = n > A5? n : A5;
break;
}
}
if(A1 == 0) printf("N "); else printf("%d ", A1);
if(A2flag == 0) printf("N "); else printf("%d ", A2);
if(A3 == 0) printf("N "); else printf("%d ", A3);
if(A4 == 0) printf("N "); else printf("%.1f ", A4 * 1.0 / A4count);
if(A5 == 0) printf("N"); else printf("%d", A5);
if (A1 == 0)
printf("N ");
else
printf("%d ", A1);
if (A2flag == 0)
printf("N ");
else
printf("%d ", A2);
if (A3 == 0)
printf("N ");
else
printf("%d ", A3);
if (A4 == 0)
printf("N ");
else
printf("%.1f ", A4 * 1.0 / A4count);
if (A5 == 0)
printf("N");
else
printf("%d", A5);
return 0;
return 0;
}
+20 -22
View File
@@ -3,30 +3,28 @@
int main()
{
int M, N;
scanf("%d %d", &M, &N);
int primes[10000];
int M, N;
int primes[10000];
for(int n = 2, count = 0; count < N; n++)
{
/* Check if n is prime number */
int iprime = 1;
for(int j = 0; count > 0 && primes[j] * primes[j] <= n; j++)
if(n % primes[j] == 0)
iprime = 0;
scanf("%d %d", &M, &N);
for (int n = 2, count = 0; count < N; n++) {
/* Check if n is prime number */
int iprime = 1;
for (int j = 0; count > 0 && primes[j] * primes[j] <= n; j++)
if (n % primes[j] == 0)
iprime = 0;
/* Record */
if(iprime)
primes[count++] = n;
}
/* Record */
if (iprime)
primes[count++] = n;
}
/* Print */
for(int i = M; i < N; i++)
{
printf("%d", primes[i - 1]);
printf((i - M + 1) % 10 ? " " : "\n");
}
printf("%d", primes[N - 1]);
/* Print */
for (int i = M; i < N; i++) {
printf("%d", primes[i - 1]);
printf((i - M + 1) % 10 ? " " : "\n");
}
printf("%d", primes[N - 1]);
return 0;
return 0;
}
+31 -36
View File
@@ -3,44 +3,39 @@
int main()
{
char str1[61], str2[61], str3[61], str4[61];
scanf("%s %s %s %s", str1, str2, str3, str4);
char str1[61], str2[61], str3[61], str4[61];
scanf("%s %s %s %s", str1, str2, str3, str4);
/* Find day, same char from [A-G] and same position in frist two lines */
int DAY;
char *weekdays[] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
for(DAY = 0; str1[DAY] && str2[DAY]; DAY++)
if(str1[DAY] == str2[DAY] && str1[DAY] >= 'A' && str1[DAY] <= 'G')
{
printf("%s", weekdays[str1[DAY] - 'A']);
break;
}
/* Find day, same char from [A-G] and same position in frist two lines */
int DAY;
char *weekdays[] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
for (DAY = 0; str1[DAY] && str2[DAY]; DAY++)
if (str1[DAY] == str2[DAY] && str1[DAY] >= 'A' && str1[DAY] <= 'G') {
printf("%s", weekdays[str1[DAY] - 'A']);
break;
}
/* Find hour, picking up from DAY, same character and position, [A-N|0-9] */
int HH;
for(HH = DAY + 1; str1[HH] && str2[HH]; HH++)
if(str1[HH] == str2[HH])
{
if(str1[HH] >= 'A' && str1[HH] <= 'N')
{
printf(" %02d", str1[HH] - 'A' + 10);
break;
}
if(isdigit(str1[HH]))
{
printf(" %02d", str1[HH] - '0');
break;
}
}
/* Find hour, picking up from DAY, same character and position, [A-N|0-9] */
int HH;
for (HH = DAY + 1; str1[HH] && str2[HH]; HH++)
if (str1[HH] == str2[HH]) {
if (str1[HH] >= 'A' && str1[HH] <= 'N') {
printf(" %02d", str1[HH] - 'A' + 10);
break;
}
if (isdigit(str1[HH])) {
printf(" %02d", str1[HH] - '0');
break;
}
}
/* Find minute, same alphabet character from last two lines */
int MM;
for(MM = 0; str3[MM] && str4[MM]; MM++)
if(str3[MM] == str4[MM] && isalpha(str3[MM]))
{
printf(":%02d", MM);
break;
}
/* Find minute, same alphabet character from last two lines */
int MM;
for (MM = 0; str3[MM] && str4[MM]; MM++)
if (str3[MM] == str4[MM] && isalpha(str3[MM])) {
printf(":%02d", MM);
break;
}
return 0;
return 0;
}
+33 -34
View File
@@ -2,56 +2,55 @@
#include <stdlib.h>
typedef struct _Student{
int ID;
int D; /** de2 : virtue */
int C; /** cai2: ability */
int rank;
int sum; /** sum = D + C */
int ID;
int D; /** de2 : virtue */
int C; /** cai2: ability */
int rank;
int sum; /** sum = D + C */
}sStudent, *Student;
/* larger the number, higher the rank */
int rank(Student s, int H, int L)
{
if(s->D < L || s->C < L) return 0; /* failed */
else if(s->D >= H && s->C >= H) return 4; /* best */
else if(s->D >= H) return 3; /* second */
else if(s->D >= s->C) return 2; /* third */
else return 1; /* fourth */
if (s->D < L || s->C < L) return 0; /* failed */
else if (s->D >= H && s->C >= H) return 4; /* best */
else if (s->D >= H) return 3; /* second */
else if (s->D >= s->C) return 2; /* third */
else return 1; /* fourth */
}
int comp(const void *a, const void *b)
{
Student s1 = *(Student*)a;
Student s2 = *(Student*)b;
Student s1 = *(Student*)a;
Student s2 = *(Student*)b;
if(s1->rank != s2->rank) return s1->rank - s2->rank;
else if(s1->sum != s2->sum) return s1->sum - s2->sum;
else if(s1->D != s2->D) return s1->D - s2->D;
else if(s1->ID != s2->ID) return s2->ID - s1->ID;
else return 0;
if (s1->rank != s2->rank) return s1->rank - s2->rank;
else if (s1->sum != s2->sum) return s1->sum - s2->sum;
else if (s1->D != s2->D) return s1->D - s2->D;
else if (s1->ID != s2->ID) return s2->ID - s1->ID;
else return 0;
}
int main()
{
int N, L, H, M = 0;
Student students[100000] = {0};
sStudent buffer[100000];
int N, L, H, M = 0;
Student students[100000] = {0};
sStudent buffer[100000];
scanf("%d %d %d", &N, &L, &H);
for(int i = 0; i < N; i++)
{
Student s = buffer + i;
scanf("%d %d %d", &s->ID, &s->D, &s->C);
s->sum = s->D + s->C;
if((s->rank = rank(s, H, L)) != 0) /* record if passed */
students[M++] = s;
}
scanf("%d %d %d", &N, &L, &H);
for (int i = 0; i < N; i++) {
Student s = buffer + i;
scanf("%d %d %d", &s->ID, &s->D, &s->C);
s->sum = s->D + s->C;
if ((s->rank = rank(s, H, L)) != 0) /* record if passed */
students[M++] = s;
}
qsort(students, M, sizeof(Student), comp);
qsort(students, M, sizeof(Student), comp);
printf("%d\n", M);
for(int i = M - 1; i >= 0; i--)
printf("%d %d %d\n", students[i]->ID, students[i]->D, students[i]->C);
printf("%d\n", M);
for (int i = M - 1; i >= 0; i--)
printf("%d %d %d\n", students[i]->ID, students[i]->D, students[i]->C);
return 0;
return 0;
}
+10 -10
View File
@@ -2,19 +2,19 @@
long Dpart(long A, int D_A)
{
long P_A;
for(P_A = 0; A; A /= 10)
if(A % 10 == D_A)
P_A = P_A * 10 + D_A;
return P_A;
long P_A;
for (P_A = 0; A; A /= 10)
if (A % 10 == D_A)
P_A = P_A * 10 + D_A;
return P_A;
}
int main()
{
long A, B;
int D_A, D_B;
scanf("%ld %d %ld %d", &A, &D_A, &B, &D_B);
printf("%ld", Dpart(A, D_A) + Dpart(B, D_B));
long A, B;
int D_A, D_B;
scanf("%ld %d %ld %d", &A, &D_A, &B, &D_B);
printf("%ld", Dpart(A, D_A) + Dpart(B, D_B));
return 0;
return 0;
}
+15 -16
View File
@@ -6,23 +6,22 @@ get a new 2-digits number. Do this until read to the end of A */
int main()
{
int B;
char A[1001], *p = A;
scanf("%s %d", A, &B);
int B;
char A[1001], *p = A;
scanf("%s %d", A, &B);
/* the results are stored in A and B instead of printed out on-the-fly */
int twodigit, remainder = 0;
for(int i = 0; A[i]; i ++)
{
twodigit = remainder * 10 + (A[i] - '0');
A[i] = twodigit / B + '0';
remainder = twodigit % B;
}
B = remainder;
/* the results are stored in A and B instead of printed out on-the-fly */
int twodigit, remainder = 0;
for (int i = 0; A[i]; i ++) {
twodigit = remainder * 10 + (A[i] - '0');
A[i] = twodigit / B + '0';
remainder = twodigit % B;
}
B = remainder;
/* print */
if(A[0] == '0' && A[1] != '\0') p++;
printf("%s %d", p, B);
/* print */
if (A[0] == '0' && A[1] != '\0') p++;
printf("%s %d", p, B);
return 0;
return 0;
}
+27 -28
View File
@@ -2,39 +2,38 @@
char max(int B, int C, int J)
{
if(B >= C && B >= J)
return 'B';
else if(C > B && C >= J)
return 'C';
else
return 'J';
if (B >= C && B >= J)
return 'B';
else if (C > B && C >= J)
return 'C';
else
return 'J';
}
int main()
{
int N;
char a, b;
int AwinB = 0, AwinC = 0, AwinJ = 0;
int BwinB = 0, BwinC = 0, BwinJ = 0;
int N;
char a, b;
int AwinB = 0, AwinC = 0, AwinJ = 0;
int BwinB = 0, BwinC = 0, BwinJ = 0;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf(" %c %c", &a, &b); /* Notice the space before every %c */
if(a == 'B' && b == 'C') AwinB++;
if(a == 'C' && b == 'J') AwinC++;
if(a == 'J' && b == 'B') AwinJ++;
if(a == 'B' && b == 'J') BwinJ++;
if(a == 'C' && b == 'B') BwinB++;
if(a == 'J' && b == 'C') BwinC++;
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf(" %c %c", &a, &b); /* Notice the space before every %c */
if (a == 'B' && b == 'C') AwinB++;
if (a == 'C' && b == 'J') AwinC++;
if (a == 'J' && b == 'B') AwinJ++;
if (a == 'B' && b == 'J') BwinJ++;
if (a == 'C' && b == 'B') BwinB++;
if (a == 'J' && b == 'C') BwinC++;
}
int Awin = AwinB + AwinC + AwinJ;
int Bwin = BwinB + BwinC + BwinJ;
int Tie = N - Awin - Bwin;
printf("%d %d %d\n", Awin, Tie, Bwin);
printf("%d %d %d\n", Bwin, Tie, Awin);
printf("%c %c", max(AwinB, AwinC, AwinJ), max(BwinB, BwinC, BwinJ));
int Awin = AwinB + AwinC + AwinJ;
int Bwin = BwinB + BwinC + BwinJ;
int Tie = N - Awin - Bwin;
printf("%d %d %d\n", Awin, Tie, Bwin);
printf("%d %d %d\n", Bwin, Tie, Awin);
printf("%c %c", max(AwinB, AwinC, AwinJ), max(BwinB, BwinC, BwinJ));
return 0;
return 0;
}
+13 -14
View File
@@ -3,32 +3,31 @@
int cmp(const void *a, const void *b)
{
return *(int*)b - *(int*)a;
return *(int*)b - *(int*)a;
}
int sort(int n)
{
int digits[4] = {n/1000, n%1000/100, n%100/10, n%10};
qsort(digits, 4, sizeof(int), cmp);
return digits[0] * 1000 + digits[1] * 100 + digits[2] * 10 + digits[3];
int digits[4] = {n/1000, n%1000/100, n%100/10, n%10};
qsort(digits, 4, sizeof(int), cmp);
return digits[0] * 1000 + digits[1] * 100 + digits[2] * 10 + digits[3];
}
int reverse(int n)
{
return n/1000 + n%1000/100 * 10 + n%100/10 * 100 + n%10 * 1000;
return n/1000 + n%1000/100 * 10 + n%100/10 * 100 + n%10 * 1000;
}
int main()
{
int N;
int N;
scanf("%d", &N);
do
{
N = sort(N);
printf("%04d - %04d = %04d\n", N, reverse(N), N - reverse(N));
N = N - reverse(N);
}while(N != 0 && N != 6174) ;
scanf("%d", &N);
do {
N = sort(N);
printf("%04d - %04d = %04d\n", N, reverse(N), N - reverse(N));
N = N - reverse(N);
} while (N != 0 && N != 6174);
return 0;
return 0;
}
+21 -25
View File
@@ -2,33 +2,29 @@
int main()
{
int N, max;
float D, Storage[1000], Total = 0, Price[1000];
int N, max;
float D, Storage[1000], Total = 0, Price[1000];
scanf("%d %f", &N, &D);
for(int i = 0; i < N; i++) scanf("%f", Storage + i);
for(int i = 0; i < N; i++) scanf("%f", Price + i);
scanf("%d %f", &N, &D);
for (int i = 0; i < N; i++) scanf("%f", Storage + i);
for (int i = 0; i < N; i++) scanf("%f", Price + i);
while(D > 0)
{
max = 0;
for(int i = 0; i < N; i++)
if(Price[i] / Storage[i] > Price[max] / Storage[max])
max = i;
while (D > 0) {
max = 0;
for (int i = 0; i < N; i++)
if (Price[i] / Storage[i] > Price[max] / Storage[max])
max = i;
if(Storage[max] < D)
{
Total += Price[max];
D -= Storage[max];
Price[max] = 0;
}
else
{
Total += Price[max] * D / Storage[max];
D = 0;
}
}
printf("%.2f", Total);
if (Storage[max] < D) {
Total += Price[max];
D -= Storage[max];
Price[max] = 0;
} else {
Total += Price[max] * D / Storage[max];
D = 0;
}
}
printf("%.2f", Total);
return 0;
return 0;
}
+7 -7
View File
@@ -2,13 +2,13 @@
int main()
{
char c;
int count[10] = {0};
while((c = getchar()) != '\n' && c != EOF)
count[c - '0']++;
char c;
int count[10] = {0};
while ((c = getchar()) != '\n' && c != EOF)
count[c - '0']++;
for(int i = 0; i < 10; i++) if(count[i])
printf("%d:%d\n", i, count[i]);
for (int i = 0; i < 10; i++) if (count[i])
printf("%d:%d\n", i, count[i]);
return 0;
return 0;
}
+12 -11
View File
@@ -2,18 +2,19 @@
int main()
{
int A, B, D, Sum;
scanf("%d %d %d", &A, &B, &D);
Sum = A + B;
int A, B, D, Sum;
scanf("%d %d %d", &A, &B, &D);
Sum = A + B;
/* calculate the bits of Sum */
int power = 1;
/* use Sum / D >= power to avoid using long int */
while(Sum / D >= power) power *= D;
/* calculate the bits of Sum */
int power = 1;
/* use Sum / D >= power to avoid using long int */
while (Sum / D >= power)
power *= D;
/* calculate D-base number. print them on-the-fly */
for(; power > 0; Sum %= power, power /= D)
printf("%d", Sum / power);
/* calculate D-base number. print them on-the-fly */
for (; power > 0; Sum %= power, power /= D)
printf("%d", Sum / power);
return 0;
return 0;
}
+12 -13
View File
@@ -2,19 +2,18 @@
int main()
{
int zero, nonzero;
int zero, nonzero;
scanf("%d", &zero);
for(int i = 1; i < 10; i++)
{
scanf("%d", &nonzero);
while(nonzero--)
{
putchar('0' + i);
for(; zero; zero--) /* after the first non-zero, print all the zeros */
putchar('0');
}
}
scanf("%d", &zero);
for (int i = 1; i < 10; i++) {
scanf("%d", &nonzero);
while (nonzero--) {
putchar('0' + i);
/* after the first non-zero, print all the zeros */
for (; zero; zero--)
putchar('0');
}
}
return 0;
return 0;
}
+20 -23
View File
@@ -2,29 +2,26 @@
int main()
{
int exponent; /* the exponent part */
char line[10000], *p = line;
scanf("%[^E]E%d", line, &exponent);
int exponent; /* the exponent part */
char line[10000], *p = line;
scanf("%[^E]E%d", line, &exponent);
if(*p++ == '-') putchar('-'); /* no print if it's '+' */
if(exponent >= 0) /* print '.' later or add zeros in the end */
{
putchar(*p);
for(p += 2; exponent; exponent--) /* print the integer part */
putchar(*p ? *p++ : '0');
if(*p) /* there is still fraction part */
{
putchar('.');
while(*p) putchar(*p++);
}
}
if(exponent < 0) /* add exponent zeros in the beginning */
{
printf("0.");
for(exponent++; exponent; exponent++) /* add zeros */
putchar('0');
for(; *p; p++) if(*p != '.') putchar(*p); /* the rest */
}
if (*p++ == '-') putchar('-'); /* no print if it's '+' */
if (exponent >= 0) { /* print '.' later or add zeros in the end */
putchar(*p);
for (p += 2; exponent; exponent--) /* print the integer part */
putchar(*p ? *p++ : '0');
if (*p) { /* there is still fraction part */
putchar('.');
while (*p) putchar(*p++);
}
}
if (exponent < 0) { /* add exponent zeros in the beginning */
printf("0.");
for (exponent++; exponent; exponent++) /* add zeros */
putchar('0');
for (; *p; p++) if (*p != '.') putchar(*p); /* the rest */
}
return 0;
return 0;
}
+33 -37
View File
@@ -4,50 +4,46 @@
#define SWAPNODE(A, B) {Node temp = A; A = B; B = temp;}
typedef struct node{
int addr;
int data;
int next;
int addr;
int data;
int next;
}node, *Node;
int main()
{
int A, N, K;
node nodes[100000] = {0};
Node np[100000] = {0}, *p;
int A, N, K;
node nodes[100000] = {0};
Node np[100000] = {0}, *p;
/* read */
scanf("%d %d %d", &A, &N, &K);
for(int i = 0; i < N; i++)
{
np[i] = nodes + i;
scanf("%d %d %d", &np[i]->addr, &np[i]->data, &np[i]->next);
}
/* read */
scanf("%d %d %d", &A, &N, &K);
for (int i = 0; i < N; i++) {
np[i] = nodes + i;
scanf("%d %d %d", &np[i]->addr, &np[i]->data, &np[i]->next);
}
/* link the list */
for(int i = 0; i < N; i++)
{
for(int j = i; j < N; j++)
if(np[j]->addr == (i ? np[i - 1]->next : A))
{
SWAPNODE(np[i], np[j]);
break;
}
if(np[i]->next == -1) /* there could be useless nodes */
N = i + 1;
}
/* link the list */
for (int i = 0; i < N; i++) {
for (int j = i; j < N; j++)
if (np[j]->addr == (i ? np[i - 1]->next : A)) {
SWAPNODE(np[i], np[j]);
break;
}
if (np[i]->next == -1) /* there could be useless nodes */
N = i + 1;
}
/* reverse the list */
for(int i = 0; i < N / K; i++)
{
p = np + i * K;
for(int j = 0; j < K / 2; j++)
SWAPNODE(p[j], p[K - j - 1]);
}
/* reverse the list */
for (int i = 0; i < N / K; i++) {
p = np + i * K;
for (int j = 0; j < K / 2; j++)
SWAPNODE(p[j], p[K - j - 1]);
}
/* print the list */
for(int i = 0; i < N - 1; i++)
printf("%05d %d %05d\n", np[i]->addr, np[i]->data, np[i + 1]->addr);
printf("%05d %d -1\n", np[N - 1]->addr, np[N - 1]->data);
/* print the list */
for (int i = 0; i < N - 1; i++)
printf("%05d %d %05d\n", np[i]->addr, np[i]->data, np[i + 1]->addr);
printf("%05d %d -1\n", np[N - 1]->addr, np[N - 1]->data);
return 0;
return 0;
}
+5 -5
View File
@@ -2,11 +2,11 @@
int main()
{
int C1, C2, T;
int C1, C2, T;
scanf("%d %d", &C1, &C2);
T = (C2 - C1 + 50) / 100; /* rounding */
printf("%02d:%02d:%02d", T/3600, T%3600/60, T%60);
scanf("%d %d", &C1, &C2);
T = (C2 - C1 + 50) / 100; /* rounding */
printf("%02d:%02d:%02d", T/3600, T%3600/60, T%60);
return 0;
return 0;
}
+15 -16
View File
@@ -4,23 +4,22 @@
int main()
{
char c;
int N, M;
scanf("%d %c", &N, &c);
char c;
int N, M;
scanf("%d %c", &N, &c);
for(M = 1; 2 * M * M - 1 <= N; M++) ;
M--; /* determine the size */
for (M = 1; 2 * M * M - 1 <= N; M++) ;
M--; /* determine the size */
/* draw the sandglass */
for(int i = 0; i < 2 * M - 1; i ++)
{
for(int j = 0; j < M - 1 - ABS(M - 1 - i); j++)
putchar(' ');
for(int j = 0; j < 2 * ABS(M - 1 - i) + 1; j++)
putchar(c);
putchar('\n');
}
printf("%d", N - 2 * M * M + 1);
/* draw the sandglass */
for (int i = 0; i < 2 * M - 1; i ++) {
for (int j = 0; j < M - 1 - ABS(M - 1 - i); j++)
putchar(' ');
for (int j = 0; j < 2 * ABS(M - 1 - i) + 1; j++)
putchar(c);
putchar('\n');
}
printf("%d", N - 2 * M * M + 1);
return 0;
return 0;
}
+19 -21
View File
@@ -3,28 +3,26 @@
int main()
{
int N, count = 0;
/* store name and birthday in one string: "YYYY/MM/DD\0NAMES\0" */
char cur[17], eldest[17] = {'9'}, youngest[17] = {'0'};
int N, count = 0;
/* store name and birthday in one string: "YYYY/MM/DD\0NAMES\0" */
char cur[17], eldest[17] = {'9'}, youngest[17] = {'0'};
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%s %s", cur + 11, cur);
if(strcmp(cur, "1814/09/06") >= 0 && strcmp(cur, "2014/09/06") <= 0)
{
if(strcmp(cur, eldest) <= 0)
memcpy(eldest, cur, 17);
if(strcmp(cur, youngest) >= 0)
memcpy(youngest, cur, 17);
count++;
}
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%s %s", cur + 11, cur);
if (strcmp(cur, "1814/09/06") >= 0 && strcmp(cur, "2014/09/06") <= 0) {
if (strcmp(cur, eldest) <= 0)
memcpy(eldest, cur, 17);
if (strcmp(cur, youngest) >= 0)
memcpy(youngest, cur, 17);
count++;
}
}
if(count)
printf("%d %s %s", count, eldest + 11, youngest + 11);
else
printf("0");
if (count)
printf("%d %s %s", count, eldest + 11, youngest + 11);
else
printf("0");
return 0;
return 0;
}
+13 -15
View File
@@ -3,21 +3,19 @@
int main()
{
int printed[128] = {0};
char c, line[82];
int printed[128] = {0};
char c, line[82];
fgets(line, 82, stdin);
while((c = getchar()) != EOF && c != '\n')
printed[toupper(c)]++;
fgets(line, 82, stdin);
while ((c = getchar()) != EOF && c != '\n')
printed[toupper(c)]++;
for(char *p = line; *p; p++)
{
c = toupper(*p);
if(printed[(int)c] == 0)
{
putchar(c);
printed[(int)c] = -1;
}
}
return 0;
for (char *p = line; *p; p++) {
c = toupper(*p);
if (printed[(int)c] == 0) {
putchar(c);
printed[(int)c] = -1;
}
}
return 0;
}
+15 -16
View File
@@ -3,28 +3,27 @@
int comp(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
return *(int*)a - *(int*)b;
}
int main()
{
int N, p, data[100000];
int max = 0, first, last;
int N, p, data[100000];
int max = 0, first = 0, last = 0;
scanf("%d %d", &N, &p); /* read */
for(int i = 0; i < N; i++)
scanf("%d", data + i);
scanf("%d %d", &N, &p); /* read */
for (int i = 0; i < N; i++)
scanf("%d", data + i);
qsort(data, N, sizeof(int), comp); /* sort */
qsort(data, N, sizeof(int), comp); /* sort */
for(first = 0, last = 0; last < N && max < N - first; first++) /* find */
{
while(last < N && data[last] <= 1L * data[first] * p)
last++;
if(max < last - first)
max = last - first;
}
printf("%d", max);
for (; last < N && max < N - first; first++) { /* find */
while (last < N && data[last] <= 1L * data[first] * p)
last++;
if (max < last - first)
max = last - first;
}
printf("%d", max);
return 0;
return 0;
}
+18 -19
View File
@@ -2,27 +2,26 @@
int main()
{
int N;
int weight[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char ZtoM[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
char ID[19];
int N;
int weight[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char ZtoM[] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
char ID[19];
scanf("%d", &N);
int d, sum, count = 0; /* index, weighted sum and count for legal IDs */
for(int i = 0; i < N; i++)
{
scanf("%s", ID);
scanf("%d", &N);
int d, sum, count = 0; /* index, weighted sum and count for legal IDs */
for (int i = 0; i < N; i++) {
scanf("%s", ID);
for(d = 0, sum = 0; d < 17 && ID[d] >= '0' && ID[d] <= '9'; d++)
sum += (ID[d] - '0') * weight[d];
for (d = 0, sum = 0; d < 17 && ID[d] >= '0' && ID[d] <= '9'; d++)
sum += (ID[d] - '0') * weight[d];
if(d == 17 && ID[17] == ZtoM[sum % 11]) /* legal ID */
count++;
else /* illegal ID */
puts(ID);
}
if(count == N)
puts("All passed");
if (d == 17 && ID[17] == ZtoM[sum % 11]) /* legal ID */
count++;
else /* illegal ID */
puts(ID);
}
if (count == N)
puts("All passed");
return 0;
return 0;
}
+12 -13
View File
@@ -2,21 +2,20 @@
int main()
{
int N, iSchool, score, imax = 0;
scanf("%d", &N);
int N, iSchool, score, imax = 0;
scanf("%d", &N);
int schools[100000] = {0};
for(int i = 0; i < N; i++)
{
scanf("%d %d", &iSchool, &score);
schools[iSchool - 1] += score;
}
int schools[100000] = {0};
for (int i = 0; i < N; i++) {
scanf("%d %d", &iSchool, &score);
schools[iSchool - 1] += score;
}
for(int i = 0; i < N; i++)
if(schools[i] > schools[imax])
imax = i;
for (int i = 0; i < N; i++)
if (schools[i] > schools[imax])
imax = i;
printf("%d %d", imax + 1, schools[imax]);
printf("%d %d", imax + 1, schools[imax]);
return 0;
return 0;
}
+8 -8
View File
@@ -3,15 +3,15 @@
int main()
{
char c;
int bad[128] = {0}; /* record keys are broken or not */
char c;
int bad[128] = {0}; /* record keys are broken or not */
while((c = getchar()) != '\n') /* read broken keys */
bad[toupper(c)] = 1;
while ((c = getchar()) != '\n') /* read broken keys */
bad[toupper(c)] = 1;
while((c = getchar()) != EOF && c != '\n') /* read string and print */
if(!bad[toupper(c)] && !(isupper(c) && bad['+']))
putchar(c);
while ((c = getchar()) != EOF && c != '\n') /* read string and print */
if (!bad[toupper(c)] && !(isupper(c) && bad['+']))
putchar(c);
return 0;
return 0;
}
+62 -39
View File
@@ -3,57 +3,80 @@
/* Both parameters take positive value */
long calcgcd(long a, long b)
{
long r;
while((r = a % b))
{
a = b;
b = r;
}
return b;
long r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
/* print a fraction number, giving the numerator and dominator */
void printfrac(long n, long d)
{
if(d == 0) { printf("Inf"); return; }
if (d == 0) {
printf("Inf");
return;
}
/* record the sign and make them positive */
int inegative = 1;
if(n < 0) { n = -n; inegative *= -1; }
if(d < 0) { d = -d; inegative *= -1; }
/* record the sign and make them positive */
int inegative = 1;
if (n < 0) {
n = -n;
inegative *= -1;
}
if (d < 0) {
d = -d;
inegative *= -1;
}
/* reduce the fraction */
long gcd = calcgcd(n, d);
n /= gcd;
d /= gcd;
/* reduce the fraction */
long gcd = calcgcd(n, d);
n /= gcd;
d /= gcd;
/* print */
if(inegative == -1) printf("(-");
if(n / d && n % d) printf("%ld %ld/%ld", n / d, n % d, d); /* mixed fractions */
else if(n % d) printf("%ld/%ld", n % d, d); /* proper fractions */
else printf("%ld", n / d); /* integers */
if(inegative == -1) printf(")");
/* print */
if (inegative == -1)
printf("(-");
if (n / d && n % d) /* mixed fractions */
printf("%ld %ld/%ld", n / d, n % d, d);
else if (n % d) /* proper fractions */
printf("%ld/%ld", n % d, d);
else /* integers */
printf("%ld", n / d);
if (inegative == -1)
printf(")");
}
int main()
{
long a1, b1, a2, b2;
scanf("%ld/%ld %ld/%ld", &a1, &b1, &a2, &b2);
long a1, b1, a2, b2;
scanf("%ld/%ld %ld/%ld", &a1, &b1, &a2, &b2);
char op[4] = {'+', '-', '*', '/'};
for(int i = 0; i < 4; i++)
{
printfrac(a1, b1); printf(" %c ", op[i]);
printfrac(a2, b2); printf(" = ");
switch(op[i])
{
case '+': printfrac(a1 * b2 + a2 * b1, b1 * b2); break;
case '-': printfrac(a1 * b2 - a2 * b1, b1 * b2); break;
case '*': printfrac(a1 * a2, b1 * b2); break;
case '/': printfrac(a1 * b2, b1 * a2); break;
}
printf("\n");
}
char op[4] = {'+', '-', '*', '/'};
for (int i = 0; i < 4; i++) {
printfrac(a1, b1);
printf(" %c ", op[i]);
printfrac(a2, b2);
printf(" = ");
switch(op[i]) {
case '+':
printfrac(a1 * b2 + a2 * b1, b1 * b2);
break;
case '-':
printfrac(a1 * b2 - a2 * b1, b1 * b2);
break;
case '*':
printfrac(a1 * a2, b1 * b2);
break;
case '/':
printfrac(a1 * b2, b1 * a2);
break;
}
printf("\n");
}
return 0;
return 0;
}
+27 -29
View File
@@ -3,41 +3,39 @@
int comp(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
return *(int*)a - *(int*)b;
}
int main()
{
int N, origin[100], halfsort[100], i, j, length;
scanf("%d", &N);
for(int i = 0; i < N; i++) scanf("%d", origin + i);
for(int i = 0; i < N; i++) scanf("%d", halfsort + i);
int N, origin[100], halfsort[100], i, j, length;
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%d", origin + i);
for (int i = 0; i < N; i++)
scanf("%d", halfsort + i);
/* if it is insertion sort, return sorted length if yes, zero otherwise */
for(i = 0; i < N - 1 && halfsort[i] <= halfsort[i + 1]; i++) ;
for(length = ++i; i < N && halfsort[i] == origin[i]; i++) ;
length = i == N ? length + 1 : 0;
/* if it is insertion sort, return sorted length if yes, zero otherwise */
for (i = 0; i < N - 1 && halfsort[i] <= halfsort[i + 1]; i++) ;
for (length = ++i; i < N && halfsort[i] == origin[i]; i++) ;
length = i == N ? length + 1 : 0;
if(length) /* insertion sort */
{
puts("Insertion Sort");
qsort(origin, length, sizeof(int), comp);
}
else /* merge sort, operate on the original array */
{
puts("Merge Sort");
for(length = 1, i = 0; i < N && length <= N; length *= 2)
{
/* i == N means identical, also breaks the outer 'for' loop */
for(i = 0; i < N && origin[i] == halfsort[i]; i++) ;
for(j = 0; j < N / length; j++)
qsort(origin + j * length, length, sizeof(int), comp);
qsort(origin + j * length, N % length, sizeof(int), comp);
}
}
if (length) { /* insertion sort */
puts("Insertion Sort");
qsort(origin, length, sizeof(int), comp);
} else { /* merge sort, operate on the original array */
puts("Merge Sort");
for (length = 1, i = 0; i < N && length <= N; length *= 2) {
/* i == N means identical, also breaks the outer 'for' loop */
for (i = 0; i < N && origin[i] == halfsort[i]; i++) ;
for (j = 0; j < N / length; j++)
qsort(origin + j * length, length, sizeof(int), comp);
qsort(origin + j * length, N % length, sizeof(int), comp);
}
}
for(int i = 0; i < N; i++)
printf("%d%c", origin[i], i == N - 1 ? '\n' : ' ');
for (int i = 0; i < N; i++)
printf("%d%c", origin[i], i == N - 1 ? '\n' : ' ');
return 0;
return 0;
}
+13 -15
View File
@@ -2,21 +2,19 @@
int main()
{
int N;
char c;
scanf("%d %c", &N, &c);
int N;
char c;
scanf("%d %c", &N, &c);
for(int i = 0; i < (N + 1) / 2; i++)
{
for(int j = 0; j < N; j++)
{
if(i == 0 || i == (N - 1) / 2 || j == 0 || j == N - 1)
putchar(c);
else
putchar(' ');
}
putchar('\n');
}
for (int i = 0; i < (N + 1) / 2; i++) {
for (int j = 0; j < N; j++) {
if (i == 0 || i == (N - 1) / 2 || j == 0 || j == N - 1)
putchar(c);
else
putchar(' ');
}
putchar('\n');
}
return 0;
return 0;
}
+12 -9
View File
@@ -2,16 +2,19 @@
int main()
{
int Galleon, Sickle, Knut, P, A, change;
int Galleon, Sickle, Knut, P, A, change;
scanf("%d.%d.%d", &Galleon, &Sickle, &Knut);
P = (Galleon * 17 + Sickle) * 29 + Knut;
scanf("%d.%d.%d", &Galleon, &Sickle, &Knut);
A = (Galleon * 17 + Sickle) * 29 + Knut;
scanf("%d.%d.%d", &Galleon, &Sickle, &Knut);
P = (Galleon * 17 + Sickle) * 29 + Knut;
scanf("%d.%d.%d", &Galleon, &Sickle, &Knut);
A = (Galleon * 17 + Sickle) * 29 + Knut;
change = A - P;
if(change < 0) { change = -change; putchar('-'); }
printf("%d.%d.%d", change / (17 * 29), change / 29 % 17, change % 29);
change = A - P;
if (change < 0) {
change = -change;
putchar('-');
}
printf("%d.%d.%d", change / (17 * 29), change / 29 % 17, change % 29);
return 0;
return 0;
}
+12 -14
View File
@@ -2,21 +2,19 @@
int main()
{
int N, M, score, count[101] = {0};
int N, M, score, count[101] = {0};
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%d", &score);
count[score]++;
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &score);
count[score]++;
}
scanf("%d", &M);
for(int i = 0; i < M; i++)
{
scanf("%d", &score);
printf("%d%c", count[score], i == M - 1 ? '\n' : ' ');
}
scanf("%d", &M);
for (int i = 0; i < M; i++) {
scanf("%d", &score);
printf("%d%c", count[score], i == M - 1 ? '\n' : ' ');
}
return 0;
return 0;
}
+18 -13
View File
@@ -2,20 +2,25 @@
int main()
{
char c;
int record[128] = {0}; /* all ASCII characters */
while((c = getchar()) != '\n') record[(int)c]++;
while((c = getchar()) != EOF && c != '\n') record[(int)c]--;
char c;
int record[128] = {0}; /* all ASCII characters */
while ((c = getchar()) != '\n')
record[(int)c]++;
while ((c = getchar()) != EOF && c != '\n')
record[(int)c]--;
int more = 0, less = 0;
for(int i = 0; i < 128; i++)
{
if(record[i] > 0) more += record[i];
if(record[i] < 0) less -= record[i];
}
int more = 0, less = 0;
for (int i = 0; i < 128; i++) {
if (record[i] > 0)
more += record[i];
if (record[i] < 0)
less -= record[i];
}
if(less) printf("No %d", less);
else printf("Yes %d", more);
if (less)
printf("No %d", less);
else
printf("Yes %d", more);
return 0;
return 0;
}
+12 -10
View File
@@ -4,16 +4,18 @@
int main()
{
int P = 0, PA = 0, PAT = 0;
char c;
int P = 0, PA = 0, PAT = 0;
char c;
while((c = getchar()) != EOF && c != '\n')
{
if(c == 'P') P++;
if(c == 'A') PA = (PA + P) % LIM;
if(c == 'T') PAT = (PAT + PA) % LIM;
}
printf("%d", PAT);
while ((c = getchar()) != EOF && c != '\n') {
if (c == 'P')
P++;
if (c == 'A')
PA = (PA + P) % LIM;
if (c == 'T')
PAT = (PAT + PA) % LIM;
}
printf("%d", PAT);
return 0;
return 0;
}
+18 -20
View File
@@ -2,31 +2,29 @@
#include <stdlib.h>
typedef struct student{
char ID[17];
int test_seat;
int exam_seat;
char ID[17];
int test_seat;
int exam_seat;
}Student, *pStudent;
int main()
{
int N, M, test;
Student students[1001] = {0}; /* N >= 1000 */
pStudent sp[1001] = {0};
int N, M, test;
Student students[1001] = {0}; /* N >= 1000 */
pStudent sp[1001] = {0};
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
pStudent s = students + i; /* fill in the ith in the array */
scanf("%s %d %d", s->ID, &s->test_seat, &s->exam_seat);
sp[s->test_seat] = s; /* set index same as test number */
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
pStudent s = students + i; /* fill in the ith in the array */
scanf("%s %d %d", s->ID, &s->test_seat, &s->exam_seat);
sp[s->test_seat] = s; /* set index same as test number */
}
scanf("%d", &M);
for(int i = 0; i < M; i ++)
{
scanf("%d", &test);
printf("%s %d\n", sp[test]->ID, sp[test]->exam_seat);
}
scanf("%d", &M);
for (int i = 0; i < M; i ++) {
scanf("%d", &test);
printf("%s %d\n", sp[test]->ID, sp[test]->exam_seat);
}
return 0;
return 0;
}
+11 -11
View File
@@ -3,19 +3,19 @@
int main()
{
char c;
int count[26] = {0}, max = 25;
char c;
int count[26] = {0}, max = 25;
while((c = getchar()) != EOF && c != '\n')
if(isalpha(c))
count[tolower(c) - 'a']++;
while ((c = getchar()) != EOF && c != '\n')
if (isalpha(c))
count[tolower(c) - 'a']++;
/* find forward from end in case there are multiple maximums */
for(int i = 25; i >= 0; i--)
if(count[i] >= count[max])
max = i;
/* find forward from end in case there are multiple maximums */
for (int i = 25; i >= 0; i--)
if (count[i] >= count[max])
max = i;
printf("%c %d", max + 'a', count[max]);
printf("%c %d", max + 'a', count[max]);
return 0;
return 0;
}
+11 -11
View File
@@ -2,18 +2,18 @@
int main()
{
char c, *str = "PATest"; /* use as index for count[] */
int i, flag = 1, count[128] = {0}; /* for each ASCII char */
char c, *str = "PATest"; /* use as index for count[] */
int i, flag = 1, count[128] = {0}; /* for each ASCII char */
/* Read and count numbers for every character */
while((c = getchar()) != EOF && c != '\n')
count[(int)c]++;
/* Read and count numbers for every character */
while ((c = getchar()) != EOF && c != '\n')
count[(int)c]++;
/* Print any character in "PATest" if it is still left */
while(flag)
for(i = 0, flag = 0; i < 6; i++)
if(count[(int)str[i]]-- > 0) /* Check the number left */
putchar(str[i]), flag = 1;
/* Print any character in "PATest" if it is still left */
while (flag)
for (i = 0, flag = 0; i < 6; i++)
if (count[(int)str[i]]-- > 0) /* Check the number left */
putchar(str[i]), flag = 1;
return 0;
return 0;
}
+33 -37
View File
@@ -3,51 +3,47 @@
#include <string.h>
char *units[] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly",
"aug", "sep", "oct", "nov", "dec"};
"aug", "sep", "oct", "nov", "dec"};
char *tens[] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo",
"syy", "lok", "mer", "jou"};
"syy", "lok", "mer", "jou"};
int Mars2Earth(char *s)
{
if(s)
{
for(int i = 0; i < 13; i++) /* units digits */
if(strcmp(s, units[i]) == 0)
return i;
for(int i = 1; i < 13; i++) /* tens digits */
if(strcmp(s, tens[i - 1]) == 0)
return i * 13;
}
return 0;
if (s) {
for (int i = 0; i < 13; i++) /* units digits */
if (strcmp(s, units[i]) == 0)
return i;
for (int i = 1; i < 13; i++) /* tens digits */
if (strcmp(s, tens[i - 1]) == 0)
return i * 13;
}
return 0;
}
int main()
{
int N, m;
char line[11];
int N, m;
char line[11];
fgets(line, 11, stdin);
sscanf(line, "%d", &N);
for(int i = 0; i < N; i++)
{
fgets(line, 11, stdin);
if(isdigit(line[0])) /* Earth number */
{
sscanf(line, "%d", &m);
if(m / 13 && m % 13)
printf("%s %s\n", tens[m / 13 - 1], units[m % 13]);
if(m / 13 && m % 13 == 0)
printf("%s\n", tens[m / 13 - 1]);
if(m / 13 == 0)
printf("%s\n", units[m % 13]);
}
if(isalpha(line[0])) /* Mars number */
{
m = Mars2Earth(strtok(line, " \n")); /* higher digit */
m += Mars2Earth(strtok(NULL, " \n")); /* lower digit */
printf("%d\n", m);
}
}
fgets(line, 11, stdin);
sscanf(line, "%d", &N);
for (int i = 0; i < N; i++) {
fgets(line, 11, stdin);
if (isdigit(line[0])) { /* Earth number */
sscanf(line, "%d", &m);
if (m / 13 && m % 13)
printf("%s %s\n", tens[m / 13 - 1], units[m % 13]);
if (m / 13 && m % 13 == 0)
printf("%s\n", tens[m / 13 - 1]);
if (m / 13 == 0)
printf("%s\n", units[m % 13]);
}
if (isalpha(line[0])) { /* Mars number */
m = Mars2Earth(strtok(line, " \n")); /* higher digit */
m += Mars2Earth(strtok(NULL, " \n")); /* lower digit */
printf("%d\n", m);
}
}
return 0;
return 0;
}
+22 -23
View File
@@ -2,32 +2,31 @@
int main()
{
int N, count = 0;
int array[100000], lmax[100000], rmin[100000];
int N, count = 0;
int array[100000], lmax[100000], rmin[100000];
scanf("%d", &N);
for(int i = 0; i < N; i++) scanf("%d", array + i);
scanf("%d", &N);
for (int i = 0; i < N; i++) scanf("%d", array + i);
/* Find the largest on one's left and the smallest on the right */
for(int i = 0, max = i; i < N; i++)
lmax[i] = array[i] >= array[max] ? array[max = i] : array[max];
for(int i = N - 1, min = i; i >= 0; i--)
rmin[i] = array[i] <= array[min] ? array[min = i] : array[min];
/* Find the largest on one's left and the smallest on the right */
for (int i = 0, max = i; i < N; i++)
lmax[i] = array[i] >= array[max] ? array[max = i] : array[max];
for (int i = N - 1, min = i; i >= 0; i--)
rmin[i] = array[i] <= array[min] ? array[min = i] : array[min];
/* A element is the largest on its left and the smallest on its right,
* it is probably a pivot */
for(int i = 0; i < N; i++)
{
if(array[i] == lmax[i] && array[i] == rmin[i])
count++;
else
array[i] = 0;
}
/* A element is the largest on its left and the smallest on its right,
* it is probably a pivot */
for (int i = 0; i < N; i++) {
if (array[i] == lmax[i] && array[i] == rmin[i])
count++;
else
array[i] = 0;
}
printf("%d\n", count);
for(int i = 0; i < N && count; i++) if(array[i])
printf("%d%c", array[i], --count ? ' ' : '\0');
printf("\n");
printf("%d\n", count);
for (int i = 0; i < N && count; i++) if (array[i])
printf("%d%c", array[i], --count ? ' ' : '\0');
printf("\n");
return 0;
return 0;
}
+10 -11
View File
@@ -2,17 +2,16 @@
int main()
{
int N, A1, A2, B1, B2;
int A = 0, B = 0;
int N, A1, A2, B1, B2;
int A = 0, B = 0;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%d %d %d %d", &A1, &A2, &B1, &B2);
if(A2 == A1 + B1 && B2 != A1 + B1) B++;
if(A2 != A1 + B1 && B2 == A1 + B1) A++;
}
printf("%d %d", A, B);
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d %d %d %d", &A1, &A2, &B1, &B2);
if (A2 == A1 + B1 && B2 != A1 + B1) B++;
if (A2 != A1 + B1 && B2 == A1 + B1) A++;
}
printf("%d %d", A, B);
return 0;
return 0;
}
+11 -12
View File
@@ -2,18 +2,17 @@
int main()
{
int N, team, member, score, highest = 0, teams[1000] = {0};
int N, team, member, score, highest = 0, teams[1000] = {0};
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%d-%d %d", &team, &member, &score);
teams[team - 1] += score;
}
for(int i = 0; i < 1000; i++)
if(teams[i] > teams[highest])
highest = i;
printf("%d %d", highest + 1, teams[highest]);
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d-%d %d", &team, &member, &score);
teams[team - 1] += score;
}
for (int i = 0; i < 1000; i++)
if (teams[i] > teams[highest])
highest = i;
printf("%d %d", highest + 1, teams[highest]);
return 0;
return 0;
}
+16 -17
View File
@@ -3,25 +3,24 @@
int main()
{
char A[101], B[101];
char encrypt[] = "0123456789JQK";
char A[101], B[101];
char encrypt[] = "0123456789JQK";
scanf("%s %s", A, B);
int lenA = strlen(A);
int lenB = strlen(B);
int maxlen = lenA > lenB ? lenA : lenB;
int a, b;
scanf("%s %s", A, B);
int lenA = strlen(A);
int lenB = strlen(B);
int maxlen = lenA > lenB ? lenA : lenB;
int a, b;
for(int i = 0; i < maxlen; i++)
{
a = lenA + i - maxlen < 0 ? 0 : A[lenA + i - maxlen] - '0';
b = lenB + i - maxlen < 0 ? 0 : B[lenB + i - maxlen] - '0';
for (int i = 0; i < maxlen; i++) {
a = lenA + i - maxlen < 0 ? 0 : A[lenA + i - maxlen] - '0';
b = lenB + i - maxlen < 0 ? 0 : B[lenB + i - maxlen] - '0';
if((maxlen - i) % 2)
putchar(encrypt[(a + b) % 13]);
else
putchar('0' + (b - a < 0 ? b - a + 10 : b - a));
}
if ((maxlen - i) % 2)
putchar(encrypt[(a + b) % 13]);
else
putchar('0' + (b - a < 0 ? b - a + 10 : b - a));
}
return 0;
return 0;
}
+10 -11
View File
@@ -2,17 +2,16 @@
int main()
{
int N;
double ai, sum = 0;
int N;
double ai, sum = 0;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%lf", &ai);
/* ai is put at the beginning to avoid overflow */
sum += ai * (i + 1) * (N - i);
}
printf("%.2lf", sum);
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%lf", &ai);
/* ai is put at the beginning to avoid overflow */
sum += ai * (i + 1) * (N - i);
}
printf("%.2lf", sum);
return 0;
return 0;
}
+30 -31
View File
@@ -3,49 +3,48 @@
int cmp(const void *a, const void *b)
{
return *(int*)b - *(int*)a;
return *(int*)b - *(int*)a;
}
int main()
{
int N, m, n;
int array[10000] = {0}, matrix[10000] = {0};
int N, m, n;
int array[10000] = {0}, matrix[10000] = {0};
scanf("%d", &N);
for(int i = 0; i < N; i++)
scanf("%d", array + i);
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%d", array + i);
qsort(array, N, sizeof(int), cmp);
qsort(array, N, sizeof(int), cmp);
/* determine m and n */
for(m = 1; !(m * m >= N && N % m == 0); m++) ;
n = N / m;
/* determine m and n */
for (m = 1; !(m * m >= N && N % m == 0); m++) ;
n = N / m;
int x = -1, y = 0, index = 0;
int horizontal = n, virtical = m;
int x = -1, y = 0, index = 0;
int horizontal = n, virtical = m;
while(horizontal > 0 && virtical > 0)
{
for(int i = 0; i < horizontal && virtical > 0; i++) /* toward right */
matrix[y * n + ++x] = array[index++];
virtical--;
while (horizontal > 0 && virtical > 0) {
for (int i = 0; i < horizontal && virtical > 0; i++) /* toward right */
matrix[y * n + ++x] = array[index++];
virtical--;
for(int i = 0; i < virtical && horizontal > 0; i++) /* toward bottom */
matrix[++y * n + x] = array[index++];
horizontal--;
for (int i = 0; i < virtical && horizontal > 0; i++) /* toward bottom */
matrix[++y * n + x] = array[index++];
horizontal--;
for(int i = 0; i < horizontal && virtical > 0; i++) /* toward left */
matrix[y * n + --x] = array[index++];
virtical--;
for (int i = 0; i < horizontal && virtical > 0; i++) /* toward left */
matrix[y * n + --x] = array[index++];
virtical--;
for(int i = 0; i < virtical && horizontal > 0; i++) /* toward top */
matrix[--y * n + x] = array[index++];
horizontal--;
}
for (int i = 0; i < virtical && horizontal > 0; i++) /* toward top */
matrix[--y * n + x] = array[index++];
horizontal--;
}
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
printf("%d%c", matrix[i * n + j], j == n - 1 ? '\n' : ' ');
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
printf("%d%c", matrix[i * n + j], j == n - 1 ? '\n' : ' ');
return 0;
return 0;
}
+11 -9
View File
@@ -3,17 +3,19 @@
int main()
{
double R1, P1, R2, P2; /* float type won't pass, not sure why */
double A, B;
double R1, P1, R2, P2; /* float type won't pass, not sure why */
double A, B;
scanf("%lf %lf %lf %lf", &R1, &P1, &R2, &P2);
A = R1 * R2 * cos(P1 + P2); /* doesn't matter how you calculate */
B = R1 * R2 * sin(P1 + P2);
scanf("%lf %lf %lf %lf", &R1, &P1, &R2, &P2);
A = R1 * R2 * cos(P1 + P2); /* doesn't matter how you calculate */
B = R1 * R2 * sin(P1 + P2);
if(A < 0 && A > -0.005) A = 0; /* Rounding */
if(B < 0 && B > -0.005) B = 0;
if (A < 0 && A > -0.005)
A = 0; /* Rounding */
if (B < 0 && B > -0.005)
B = 0;
printf("%.2lf%+.2lfi", A, B);
printf("%.2lf%+.2lfi", A, B);
return 0;
return 0;
}
+19 -20
View File
@@ -5,27 +5,26 @@
int main()
{
int N, m[5];
char c, symbols[3][10][5] = {0};
int N, m[5];
char c, symbols[3][10][5] = {0};
for(int symbol = 0; symbol < 3; symbol++)
for(int index = 0; (c = getchar()) != '\n'; )
if(c == '[') scanf("%[^]]", symbols[symbol][index++]);
for (int symbol = 0; symbol < 3; symbol++)
for (int index = 0; (c = getchar()) != '\n'; )
if (c == '[') scanf("%[^]]", symbols[symbol][index++]);
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
for(int i = 0; i < 5; i++) scanf("%d", m + i);
if(m[0] > 0 && m[0] <= 10 && *symbols[0][--m[0]]
&& m[1] > 0 && m[1] <= 10 && *symbols[1][--m[1]]
&& m[2] > 0 && m[2] <= 10 && *symbols[2][--m[2]]
&& m[3] > 0 && m[3] <= 10 && *symbols[1][--m[3]]
&& m[4] > 0 && m[4] <= 10 && *symbols[0][--m[4]])
printf("%s(%s%s%s)%s\n", symbols[0][m[0]], symbols[1][m[1]],
symbols[2][m[2]], symbols[1][m[3]], symbols[0][m[4]]);
else
puts("Are you kidding me? @\\/@");
}
scanf("%d", &N);
for (int i = 0; i < N; i++) {
for (int i = 0; i < 5; i++) scanf("%d", m + i);
if (m[0] > 0 && m[0] <= 10 && *symbols[0][--m[0]]
&& m[1] > 0 && m[1] <= 10 && *symbols[1][--m[1]]
&& m[2] > 0 && m[2] <= 10 && *symbols[2][--m[2]]
&& m[3] > 0 && m[3] <= 10 && *symbols[1][--m[3]]
&& m[4] > 0 && m[4] <= 10 && *symbols[0][--m[4]])
printf("%s(%s%s%s)%s\n", symbols[0][m[0]], symbols[1][m[1]],
symbols[2][m[2]], symbols[1][m[3]], symbols[0][m[4]]);
else
puts("Are you kidding me? @\\/@");
}
return 0;
return 0;
}
+19 -18
View File
@@ -2,24 +2,25 @@
int main()
{
int N, D, K;
int empty = 0, pempty = 0, lower;
float e, E;
int N, D, K;
int empty = 0, pempty = 0, lower;
float e, E;
scanf("%d %f %d", &N, &e, &D);
for(int i = 0; i < N; i++)
{
lower = 0;
scanf("%d", &K);
for(int j = 0; j < K; j++)
{
scanf("%f", &E);
if(E < e) lower++;
}
if(lower > K / 2 && K > D) empty++;
else if(lower > K / 2) pempty++;
}
printf("%.1f%% %.1f%%", 100.0 * pempty / N, 100.0 * empty / N);
scanf("%d %f %d", &N, &e, &D);
for (int i = 0; i < N; i++) {
lower = 0;
scanf("%d", &K);
for (int j = 0; j < K; j++) {
scanf("%f", &E);
if (E < e)
lower++;
}
if (lower > K / 2 && K > D)
empty++;
else if (lower > K / 2)
pempty++;
}
printf("%.1f%% %.1f%%", 100.0 * pempty / N, 100.0 * empty / N);
return 0;
return 0;
}
+30 -31
View File
@@ -5,40 +5,39 @@
int main()
{
int count = 0, N;
double f, sum = 0;
/* Maxium scenario: -1000.00. So just need to read 8 chars(+ '\0' = 9) */
char s[9], *pEnd, *pDot, c;
int count = 0, N;
double f, sum = 0;
/* Maxium scenario: -1000.00. So just need to read 8 chars(+ '\0' = 9) */
char s[9], *pEnd, *pDot, c;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%8s", s); /* Just read up to 8 chars */
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%8s", s); /* Just read up to 8 chars */
c = ungetc(getchar(), stdin); /* Read next char and push back */
f = strtod(s, &pEnd); /* pEnd -> converted floating number */
pDot = strchr(s, '.'); /* pDot -> (first) decimal point */
c = ungetc(getchar(), stdin); /* Read next char and push back */
f = strtod(s, &pEnd); /* pEnd -> converted floating number */
pDot = strchr(s, '.'); /* pDot -> (first) decimal point */
if((!isspace(c) && c != EOF) /* string too long */
|| *pEnd /* not floating number */
|| (f > 1000 || f < -1000) /* out of range */
|| (pDot && pDot - s < strlen(s) - 3)) /* precision too high */
{
printf("ERROR: %s", s);
/* this can avoid overflow (we don't know how long input is) */
while(!isspace(c = getchar()) && c != EOF) putchar(c);
printf(" is not a legal number\n");
}
else /* legel number */
{
count++;
sum += f;
}
}
if ((!isspace(c) && c != EOF) /* string too long */
|| *pEnd /* not floating number */
|| (f > 1000 || f < -1000) /* out of range */
|| (pDot && pDot - s < strlen(s) - 3)) { /* precision too high */
printf("ERROR: %s", s);
/* this can avoid overflow (we don't know how long input is) */
while (!isspace(c = getchar()) && c != EOF) putchar(c);
printf(" is not a legal number\n");
} else { /* legel number */
count++;
sum += f;
}
}
if(count == 0) printf("The average of 0 numbers is Undefined\n");
if(count == 1) printf("The average of 1 number is %.2lf", sum);
if(count >= 2) printf("The average of %d numbers is %.2lf", count, sum / count);
if (count == 0)
printf("The average of 0 numbers is Undefined\n");
if (count == 1)
printf("The average of 1 number is %.2lf", sum);
if (count >= 2)
printf("The average of %d numbers is %.2lf", count, sum / count);
return 0;
return 0;
}
+27 -28
View File
@@ -3,17 +3,17 @@
#include <string.h>
typedef struct student{
char name[9];
int height;
char name[9];
int height;
}student, *Student;
int cmp(const void *a, const void *b)
{
Student s1 = *(Student*)a;
Student s2 = *(Student*)b;
if(s1->height != s2->height)
return s2->height - s1->height;
return strcmp(s1->name, s2->name);
Student s1 = *(Student*)a;
Student s2 = *(Student*)b;
if (s1->height != s2->height)
return s2->height - s1->height;
return strcmp(s1->name, s2->name);
}
/**
@@ -22,33 +22,32 @@ int cmp(const void *a, const void *b)
**/
Student *printrow(Student *s, int num)
{
for(int i = num / 2 * 2 - 1; i > 0; i -= 2) /* the left half */
printf("%s ", s[i]->name);
for(int i = 0; i < num; i += 2) /* the right half */
printf("%s%c", s[i]->name, i + 2 < num ? ' ' : '\n');
return s + num;
for (int i = num / 2 * 2 - 1; i > 0; i -= 2) /* the left half */
printf("%s ", s[i]->name);
for (int i = 0; i < num; i += 2) /* the right half */
printf("%s%c", s[i]->name, i + 2 < num ? ' ' : '\n');
return s + num;
}
int main()
{
/* read and sort data */
int N, K;
student students[10000] = {0};
Student sp[10000] = {0}, *p = sp;
/* read and sort data */
int N, K;
student students[10000] = {0};
Student sp[10000] = {0}, *p = sp;
scanf("%d %d", &N, &K);
for(int i = 0; i < N; i++)
{
sp[i] = students + i;
scanf("%s %d", sp[i]->name, &sp[i]->height);
}
scanf("%d %d", &N, &K);
for (int i = 0; i < N; i++) {
sp[i] = students + i;
scanf("%s %d", sp[i]->name, &sp[i]->height);
}
qsort(sp, N, sizeof(Student), cmp);
qsort(sp, N, sizeof(Student), cmp);
/* print */
p = printrow(p, N - N / K * (K - 1)); /* the last row */
while(p < sp + N)
p = printrow(p, N / K);
/* print */
p = printrow(p, N - N / K * (K - 1)); /* the last row */
while (p < sp + N)
p = printrow(p, N / K);
return 0;
return 0;
}
+8 -9
View File
@@ -2,15 +2,14 @@
int main()
{
int N, a, sum = 0;
int N, a, sum = 0;
scanf("%d", &N);
for(int i = 0; i < N; i++)
{
scanf("%d", &a);
sum += a;
}
printf("%d", sum * (N - 1) * 11);
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &a);
sum += a;
}
printf("%d", sum * (N - 1) * 11);
return 0;
return 0;
}
+9 -9
View File
@@ -3,17 +3,17 @@
int main()
{
char c;
int sum = 0, count[2] = {0};
char c;
int sum = 0, count[2] = {0};
while((c = getchar()) != EOF && c != '\n')
if(isalpha(c))
sum += tolower(c) - 'a' + 1;
while ((c = getchar()) != EOF && c != '\n')
if (isalpha(c))
sum += tolower(c) - 'a' + 1;
for(; sum; sum >>= 1)
count[sum & 1]++;
for (; sum; sum >>= 1)
count[sum & 1]++;
printf("%d %d", count[0], count[1]);
printf("%d %d", count[0], count[1]);
return 0;
return 0;
}

Some files were not shown because too many files have changed in this diff Show More