A. Seats
这个题很惭愧,RE了七回。下面是我的解法:
#include <iostream>
using namespace std;
string s;
int main()
{
int n = 0; int cnt = 0;
cin>>n>>s;
for(int i = 1; i < n - 1; i++){
if(s[i-1] == '#' && s[i] == '.' && s[i+1] == '#') cnt++;
}
cout<<cnt<<endl;
return 0;
}
然后2024.10.14看了一下,官方和我思路一样。这里参考人家最初的官方题解的话,就得把中间s[i-1]&&s[i]&&s[i+1]的if部分换成
ans += s.substr(i - 1, 3) == "#.#";
我都不知道<iostream>库里有这么个玩意儿,substr(a,b),从a开始,取a及其以后b个字符。比如str = abcdefg,那么substr() = abcdefg,而substr(0,3) = abc,同时substr(5,5) = fg,b超过的部分直接忽略了。
我为啥RE七回呢?因为我画蛇添足地写了个for循环,循环里面是cin>>s[i],给自己气笑了都。
B. Traveling Takahashi Problem
最没有难度的一集。高桥同志(啥同志啊atcoder里还有南朝伪政权旗帜呢,恼)从(0,0)走点数,走到最后一个点后别忘了再走回(0,0),最后算路程总数。下面是我提交的结果:
#include <iostream>
#include <cmath>
int main()
{
int n = 0; double a, b, c, d, temp;
a = b = c = d = temp = 0;
double x, y;
x = y = 0;
std::cin>>n;
double arr[n + 1] = {0};
for(int i = 0; i < n; i++){
scanf("%lf %lf", &c, &d);
x = pow((a - c),2); y = pow((b - d),2);
temp = sqrt(x + y);
arr[i] = temp;
a = c; b = d;
}
c = d = 0;
x = pow((a - c),2); y = pow((b - d),2);
temp = sqrt(x + y);
arr[n] = temp;
a = c; b = d;
temp = 0;
for(int i = 0; i <= n; i++){
temp += arr[i];
}
printf("%lf",temp);
return 0;
}
然而看了看官解,人家咋就这么简单清晰明了呢?
#include<stdio.h>
#include<math.h>
int main(){
int n;
scanf("%d", &n);
double ans = 0;
int crrx = 0;
int crry = 0;
for(int i=0; i<n; i++){
int x, y;
scanf("%d%d", &x, &y);
ans += hypot(x-crrx, y-crry);
crrx = x;
crry = y;
}
ans += hypot(crrx, crry);
printf("%.10f\n", ans);
}
是hypot()!我加了hypot()!
这个函数只能在c++17及以上版本才能用,它就是专门来算两数或三数平方之和的平方根的,换句话说就是点(x,y)或者(x,y,z)到原点的距离。hypot()数据类型是double,还有其他数据类型:hypotf()是float,hypotl()是long double。
不是你们怎么都知道那么多函数,就我一个人在这手搓计算器;什么时候才能把这几个库吃透啊!什么时候才能把stl弄懂啊!
C. Spiral Rotation(不用看了,没写)
看不懂,完全看不懂,我连题都没读明白。
摆了,跟D题一块儿摆了,以后再补,如果我还能想起来的话。顺便说一句,我是真不会用vector,吃了好几次亏了。
最后瞎说两句:atcoder better 不翻译题面!那我要你干嘛?我还不如直接读日语呢,好歹能看懂汉字。
2024.11.20记:破案了,我没用开发者模式。