CF EDU16
A. King Moves
思路
一共只有三种情况
- 在角上, 答案为3
- 在边上, 答案为5
- 在中间, 答案为8
代码
#include<bits/stdc++.h>
using namespace std;
char s[2];
int main()
{
cin >> s[0] >> s[1];
if(s[0] - 'a' == 0 || s[0] - 'a' == 7)
{
if(s[1] - '0' == 1 || s[1] - '0' == 8)
{
cout << 3 << endl;
}
else
{
cout << 5 << endl;
}
}
else if(s[1] - '0' == 1 || s[1] - '0' == 8)
{
cout << 5 << endl;
}
else
{
cout << 8 << endl;
}
return 0;
}
大佬
采用了更简便的赋值方法
int a=0, b=0;
if(c=='a' || c=='h') a=1;
if(d=='1' || d=='8') b=1;
if(a+b==2) cout<<3;
else if(a+b==1) cout<<5;
else cout<<8;
B. Optimal Point on a Line
思路
显而易见, 中位数到所有点的距离最小。
这是肯定的, 举个例子, 设有三个数
a
1
,
a
1
+
b
,
a
1
+
c
a_1, a_1+b, a_1+c
a1,a1+b,a1+c
那么中位数距离为b + ( c - b) = c.
然而在两边的数距离却为b+c;
代码
#include<bits/stdc++.h>
using namespace std;
int n;
int a[300010];
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
}
sort(a+1, a+n+1);
if(n % 2 == 1)
{
n = n / 2 + 1;
}
else
{
n = n / 2;
}
cout << a[n] << endl;
return 0;
}
大佬
其实不用排序, 直接n_thelement 它不香吗?QAQ
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <queue>
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
using namespace std;
int n;
int a[500000];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", a + i);
int x = (n - 1) / 2;
nth_element(a, a + x, a + n);
cout << a[x] << "\n";
return 0;
}
C. Magic Odd Square
思路
很容易就能发现, 要求行,列, 对角线都为奇数个奇数,那么, 显然, 一个这样的图形是合法的。
菱形里面填奇数, 剩下地方填偶数即可
代码
#include<bits/stdc++.h>
using namespace std;
int n;
int ans[50][50];
int main()
{
int odd = 1;
int even = 2;
cin >> n;
int l = (n+1) / 2;
int r = (n+1) / 2;
for(int i = 1; i <= n / 2; i++)
{
for(int j = l; j <= r; j++)
{
ans[i][j] = odd;
odd += 2;
}
l--; r++;
}
if(n % 2 == 1)
{
for(int i = l; i <= r; i++)
{
ans[n / 2 + 1][i] = odd;
odd += 2;
}
}
l = (n+1) / 2;
r = (n+1) / 2;
for(int i = n; i > n - (n / 2); i--)
{
for(int j = l; j <= r; j++)
{
ans[i][j] = odd;
odd += 2;
}
l--, r++;
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(ans[i][j] == 0)
{
ans[i][j] = even;
even += 2;
}
}
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cout << ans[i][j] << " ";
}
cout << endl;
}
return 0;
}
dalao
先预处理出来哪些位置填奇数, 最后在重新遍历一遍填数值;
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <queue>
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
using namespace std;
int n0 = 2;
int n1 = 1;
int en[100][100];
int g0() {
n0 += 2;
return n0 - 2;
}
int g1() {
n1 += 2;
return n1 - 2;
}
int n;
int main() {
cin >> n;
int now = 1;
for (int i = 0; i < n; ++i) {
int x = now / 2;
int st = n / 2;
for (int j = st - x; j <= st + x; ++j)
en[i][j] = 1;
if (i * 2 + 1 < n)
now += 2;
else
now -= 2;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (en[i][j])
printf("%d ", g1());
else
printf("%d ", g0());
}
printf("\n");
}
return 0;
}