USTCOJ 1361-1363題目比較簡單,就不多說了。其中值得留意的是gets函數會讀入'\r'(如果有的話),處理方法詳見1362。
1361開燈問題:http://acm.ustc.edu.cn/ustcoj/problem.php?id=1361
#include <stdio.h>
#include <string.h>
void reverseFlag( char* flag )
{
*flag = (*flag) ? 0 : 1;
}
main()
{
int n, k;
while (scanf("%d%d", &n, &k) , n + k)
{
int i;
char flag[1001];
memset(flag, 1, n + 1);
for (i = 2; i <= k; i++)
{
int t = i;
while (t <= n )
{
reverseFlag(flag+t);
t += i;
}
}
printf("1");
for (i = 2; i <= n; i++)
if (flag[i])
printf(" %d", i);
printf("\n");
}
return 0;
}
1362 單詞求值:http://acm.ustc.edu.cn/ustcoj/problem.php?id=1362
#include <stdio.h>
#include <string.h>
//由於在線評測系統的輸入是重定向到文件的,
//當輸入文件中換行是"\r\n"時,gets函數會讀入'\r'
void strip(char *str)
{
char *p;
if ((p = strchr(str, '\r')) != NULL)
*p = '\0';
}
main()
{
char str[260] = {0};
//使用gets函數讀入帶空格的字符串
while (gets(str+1), str[1] != '#')
{
strip(str);
int i, sum = 0;
for (i = 1; str[i]; i++)
{
if (str[i] == ' ')
continue;
else
sum += i * (str[i] - 'A' + 1);
}
printf("%d\n", sum);
}
return 0;
}
1363 幸福配對:http://acm.ustc.edu.cn/ustcoj/problem.php?id=1363
#include <stdio.h>
main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n, a, b, c;
scanf("%d", &n);
a = b = c = 0;
while (n--)
{
int t;
scanf("%d", &t);
switch (t % 3)
{
case 0: a++; break;
case 1: b++; break;
case 2: c++; break;
default: break;
}
}
a /= 2;
b = (b > c) ? c : b;
printf("%d\n", a + b);
}
return 0;
}