A - King Moves
題解
簽到題,求空棋盤上King當前位置能走的位置的數量。
代碼
#include <cstdio>
char s[3];
int main(){
while (~scanf("%s", s)) {
int cnt = 0;
if (s[0] == 'a' || s[0] == 'h') ++cnt;
if (s[1] == '1' || s[1] == '8') ++cnt;
if (cnt == 0) printf("8\n");
if (cnt == 1) printf("5\n");
if (cnt == 2) printf("3\n");
}
return 0;
}
B - Vitya in the Countryside
題解
模擬。
給出月亮每一天的大小軌跡和前幾天的月亮軌跡,判斷下一天是變大還是變小。如果給出的資料不足就輸出-1
。
主要就是特判0和15這兩個點。
代碼
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 95;
int data[maxn], num;
int main(){
int n;
while (~scanf("%d", &num)){
memset(data, -1, sizeof(data));
n = num;
while (n--)
scanf("%d", data+(num-n));
if (num == 1){
if (data[1] == 0) {
printf("UP\n");
continue;
}
else if (data[1] == 15){
printf("DOWN\n");
continue;
}
else {
printf("-1\n");
continue;
}
}
int t = data[num-1] - data[num];
if (t > 0){
if (data[num]) {
printf("DOWN\n");
continue;
}
else {
printf("UP\n");
continue;
}
}
else{
if (data[num] != 15){
printf("UP\n");
continue;
}
else{
printf("DOWN\n");
continue;
}
}
}
return 0;
}
C - Optimal Point on a Line
題解
模擬。
給出一列數軸上的數,找出一個數,這個數到所有數的距離總和最小。
這道題很容易想得很復雜。但是其實只要取這一列數的中位數就可以了。
代碼
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 300005;
int coor[maxn], n;
int main(){
scanf("%d", &n);
for (int i=1; i<=n; ++i)
scanf("%d", &coor[i]);
sort(coor+1, coor+n+1);
printf("%d\n", coor[(1+n)/2]);
return 0;
}
D - Anatoly and Cockroaches
題解
模擬。
給一個r
和b
代表的字符串,有兩種操作,一種是交換,一種是換顏色。
求最小的操作次數。
有兩種情況:rbrbrbrbr
brbrbrbrb
統計奇數位置和偶數位置上的r
和b
,分別為xr``xb``yr``yb
。
xr yb
一組,xb yr
一組,交換就是把一組中的兩個同時變成另一組的兩個。所以找出兩組中最大值最小的那一組,那一組的最大值就是答案。
代碼
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
char s[100010];
int n;
int main(){
scanf("%d%s", &n, s);
int xr=0, xb=0, yr=0, yb=0;
for (int i=0; i<n; ++i){
if (i&1){
if (s[i] == 'r') ++xr;
else ++xb;
}
else{
if (s[i] == 'r') ++yr;
else ++yb;
}
}
int ans = min(max(xb, yr), max(xr, yb));
printf("%d\n", ans);
}
E - Magic Odd Square
題解
模擬。
輸入n
,求一個n
階方陣,讓橫縱對角線和都是奇數。
先給中央部分的一個內嵌正方形賦值,從1
開始遍歷奇數順序填寫。然后從第一行第一列開始用偶數填空。
代碼
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 50;
int n;
int map[maxn][maxn];
void init(){
memset(map, 0, sizeof(map));
int mid = (n+1)/2;
int odd = 0;
for (int i = 1; i <= n; ++i){
int k = i <= mid? 2*i-1: 2*(n-i)+1;
for (int j = mid - k/2; j <= mid + k/2; j++){
map[i][j] = 2*(odd++)+1;
}
}
}
void fill(){
int cur = 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if (!map[i][j]){
map[i][j] = 2 * cur++;
}
}
void outp(){
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= n; ++j)
printf("%d ", map[i][j]);
printf("\n");
}
}
int main(){
scanf("%d", &n);
init();
fill();
outp();
}
F - Efim and Strange Grade
題解
模擬。
要求找出將小數四舍五入之后的最大值。
一開始沒有懂題意,后來查了題解試過才知道。
由于位數很大所以需要用字符數組模擬,注意一下對字符數組+1
的時候的特判。在這里WA了三次。
代碼
#include <cstdio>
const int maxn = 200005;
char s[maxn];
int n, t;
void pp(int x){
for (int i = x; i>=0; --i){
if (s[i] == '.') { s[i] = '\0'; continue; }
if (s[i] != '9'){ ++s[i]; break; }
else { s[i] = '0'; continue; }
}
}
int main(){
scanf("%d%d%s", &n, &t, s+1);
s[0] = '0';
int cnt = 0, cur1, cur2 = n;
for (int i=1; i<=n; ++i)
if (s[i] == '.') { cur1 = i; break; }
for (int i=cur1+1; i<=n; ++i)
if (s[i]-'0' > 4){ cur2 = i; break; }
for (int i = cur2; i>cur1 && cnt < t; --i)
if (s[i]-'0' > 4){
pp(i-1); ++cnt; s[i] = '\0';
}
printf("%s\n", s[0]-'0'? s: s+1);
return 0;
}