8.4综合测试总结
B4073 [CSP-X 2018] 快递费用
题目描述
某快递公司按邮寄物品的重量收费,收费标准如下:
重量在
超过
根据目的地的不同,加收的费用是不一样的。快递公司划分了五个目的地区域:
-
区域
1 :每超重500 克加收4 元; -
区域
2 :每超重500 克加收6 元; -
区域
3 :每超重500 克加收9 元; -
区域
4 :每超重500 克加收10 元; -
区域
5 :每超重500 克加收17 元。
给出物品的重量
正确代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int dx[] = {0, 4, 6, 9, 10, 17};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int w, n;
cin >> w >> n;
if (w <= 500) cout << 20 << endl;
else {
int s = (w - 500) / 500;
if ((w - 500) % 500 != 0) s++;
cout << 20 + dx[n] * s;
}
return 0;
}
T638889 石头布没有剪刀
题目描述
你和对手都只有两种出拳方式:石头(
输入格式
一个字符串
输出格式
输出能得到的最大分数。
正确代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> s;
int cnt = 0, len = s.size();
for (int i = 0; i < len; i++)
if (s[i] == 'p') cnt++;
int x = len / 2;
cout << x - cnt;
return 0;
}
T644252 篝火问题
题目描述
在无限广阔的二维网格中,坐标
在时刻
给定一个由 N、W、S、E 组成且长度为
- 风吹过时,当前存在的所有烟雾将按以下规则移动:
- 若
S 的第t 个字符为N,则单元格(r, c) 的烟雾移动到(r-1, c) 。 - 若
S 的第t 个字符为W,则单元格(r, c) 的烟雾移动到(r, c-1) 。 - 若
S 的第t 个字符为S,则单元格(r, c) 的烟雾移动到(r+1, c) 。 - 若
S 的第t 个字符为E,则单元格(r, c) 的烟雾移动到(r, c+1) 。
- 若
- 若此时单元格
(0, 0) 没有烟雾,则在该位置生成新的烟雾。
落落的去位于坐标
对于每个整数
输入格式
第一行输入三个整数
输出格式
将答案作为由 0 和 1 组成的字符串输出。
输出字符串的第
- 若时刻
t+0.5 时单元格(R, C) 存在烟雾,则为1 - 若时刻
t+0.5 时单元格(R, C) 不存在烟雾,则为0
输入输出样例 #1
输入 #1
6 -2 1
NNEEWS
输出 #1
001010
输入输出样例 #2
输入 #2
10 1 2
NEESESWEES
输出 #2
0001101011
输入输出样例 #3
输入 #3
20 -1 -2
WWNNWSWEWNSWWENSNWWN
输出 #3
00100111111000101111
说明/提示
对于
样例解释 1
在时刻 001010。
图示中以篝火所在的单元格
- 若
r < 0 ,则单元格(r, c) 位于上方|r| 格 - 若
r \geq 0 ,则单元格(r, c) 位于下方r 格 - 若
c < 0 ,则单元格(r, c) 位于左侧|c| 格 - 若
c \geq 0 ,则单元格(r, c) 位于右侧c 格
各时刻的网格状态如下:
- 时刻
0.5 : - 时刻
1.5 : - 时刻
2.5 : - 时刻
3.5 : - 时刻
4.5 : - 时刻
5.5 : - 时刻
6.5 :
错误代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 2e5;
int n, r, c;
string s;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> r >> c >> s;
int x = r, y = c, len = s.size();
for (int i = 0; i < len; i++) {
if (s[i] == 'N') x++;
else if (s[i] == 'W') y++;
else if (s[i] == 'S') x--;
else if (s[i] == 'E') y--;
if (x == 0 && y == 0) cout << 1;
else cout << 0;
}
return 0;
}
错误原因
思路错误
正确思路
- 我们可以想到如果直接处理烟雾, 会非常麻烦
- 我们可以考虑一定人和篝火
- 我们只要篝火移动的路径, 如果人走到了篝火走过的地方, 就输出
1 , 否则输出0 - 我们可以用一个二维
map 记录篝火走过的地方。 定义方法:map<int, map<int, bool> > mp;或map<pair<int, int>, bool> mp; - 伪代码如下:
#include<bits/stdc++.h> #define int long long using namespace std; int n, r, c;//r, c表示人的位置 string s; map<int, map<int, bool> > mp;//用做标记 signed main() { //输入 int x = 0, y = 0;//篝火位置 //标记篝火的位置, mp[0][0] = true; for () { //如果是N, 人和篝火都往下走, 行号 + 1 //如果是W, 人和篝火都往右走, 列号 + 1 //如果是S, 人和篝火都往上走, 行号 - 1 //如果是E, 人和篝火都往左走, 列号 - 1 //标记 //输出 } return 0; }
正确代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 2e5;
int n, r, c;
string s;
map<int, map<int, bool> > mp;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> r >> c >> s;
int x = 0, y = 0, len = s.size();
mp[0][0] = true;
for (int i = 0; i < len; i++) {
if (s[i] == 'N') x++, r++;
else if (s[i] == 'W') y++, c++;
else if (s[i] == 'S') x--, r--;
else if (s[i] == 'E') y--, c--;
mp[x][y] = true;
cout << mp[r][c];
}
return 0;
}
T639146 落落的去的数学问题3
题目描述
对于正整数
- 将
x, y 的十进制表示分别作为字符串解释,并按顺序连接,得到字符串z 。将z 作为十进制整数解释,其值即为f(x, y) 。
例如,
给定一个长度为
错误代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 998244353;
int m[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000};
int a[1000007], ans, sum, cnt;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++) {
int x = a[j], s = 0;
while (x) {x /= 10; s++;}
ans += (a[i] * m[s] + a[j]) % mod;
}
cout << ans % mod;
return 0;
}
错误原因
思路错误
正确思路
- 首先我先介绍一些函数:
-
to_string, 他的作用是把数字变成字符串举例 :
string sn = to_string (356345); -
.size(), 他的作用是求出数字的位数举例 :
int len = sn.size() -
stoi, 他的作用是把字符串变成int 类型举例 :
int x = stoi ("423423"); -
atoll, 他的作用是把字符串变成long long 类型举例 :
long long y = stoll("323423");这些函数的复杂度都是
O(n)
-
我们可以很轻松想出暴力解, 代码如下:
for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { ll x = stoll(to_string(a[i]) + to_string(a[j])); ans = (ans % mod + x % mod) % mod; } } cout << ans % mod; -
我们可以得到一个结论:
f(a_i, a_j) = a_i * 10的a_j次方 + a_j -
对应每个
a_i ,(a[1] + a[2] + a[3] + ... + a[j - 1]) * 10的a[j]次方 + (j - 1) *a[j] -
核心代码如下:
for (int j = 1; j <= n; j++) { int len = to_string(a[j]).size(); ans += (((a[1] + a[2] + ...... + a[j - 1]) * (long(pow(10, len)) % mod)) % mod + ((j - 1) * a[j]) % mod) % mod; }
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 998244353;
int m[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000};
int a[1000007], ans, cnt;
int sum[10000007];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = (sum[i - 1] + a[i]) % mod;
}
for (int j = 1; j <= n; j++) {
int len = to_string(a[j]).size();
ans += ((sum[j - 1] * (long(pow(10, len)) % mod)) % mod + ((j - 1) * a[j]) % mod) % mod;
}
cout << ans % mod;
return 0;
}
P1100 高低位交换
题目描述
给出一个小于
例如,数
正确代码
#include<bits/stdc++.h>
using namespace std;
int main() {
unsigned int n;
cin >> n;
cout << (n >> 16) + (n << 16);
return 0;
}