B4073 [CSP-X 2018] 快递费用
题目描述
某快递公司按邮寄物品的重量收费,收费标准如下:
重量在 克以内的,一律 元;
超过 克的,超重的部分按每 克加收费用。超出的重量不足 克的,按 克计算。例如: 克,超重 克,需加收两份费用。
根据目的地的不同,加收的费用是不一样的。快递公司划分了五个目的地区域:
-
区域 :每超重 克加收 元;
-
区域 :每超重 克加收 元;
-
区域 :每超重 克加收 元;
-
区域 :每超重 克加收 元;
-
区域 :每超重 克加收 元。
给出物品的重量 和目的地区域编号 ,请你计算快递费用。
正确代码
#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 组成且长度为 的字符串 ,在时刻 时,将按顺序发生以下现象:
- 风吹过时,当前存在的所有烟雾将按以下规则移动:
- 若 的第 个字符为
N,则单元格 的烟雾移动到 。 - 若 的第 个字符为
W,则单元格 的烟雾移动到 。 - 若 的第 个字符为
S,则单元格 的烟雾移动到 。 - 若 的第 个字符为
E,则单元格 的烟雾移动到 。
- 若 的第 个字符为
- 若此时单元格 没有烟雾,则在该位置生成新的烟雾。
落落的去位于坐标 处。 对于每个整数 ,请判断在时刻 时单元格 是否存在烟雾,并按输出格式要求输出结果。
输入格式
第一行输入三个整数 。
第二行是一行带方向的字符串 。
输出格式
将答案作为由 个 0 和 1 组成的字符串输出。
输出字符串的第 个字符 () 应满足以下规则:
- 若时刻 时单元格 存在烟雾,则为
1 - 若时刻 时单元格 不存在烟雾,则为
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。
图示中以篝火所在的单元格 为基准:
- 若 ,则单元格 位于上方 格
- 若 ,则单元格 位于下方 格
- 若 ,则单元格 位于左侧 格
- 若 ,则单元格 位于右侧 格
各时刻的网格状态如下:
错误代码
#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;
}错误原因
思路错误
正确思路
- 我们可以想到如果直接处理烟雾, 会非常麻烦
- 我们可以考虑一定人和篝火
- 我们只要篝火移动的路径, 如果人走到了篝火走过的地方, 就输出, 否则输出
- 我们可以用一个二维记录篝火走过的地方。 定义方法:
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
题目描述
对于正整数 ,定义 如下:
- 将 的十进制表示分别作为字符串解释,并按顺序连接,得到字符串 。将 作为十进制整数解释,其值即为 。
例如,,。
给定一个长度为 的正整数序列 。请计算下式的值对 取模的结果:
错误代码
#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 x = stoi ("423423"); -
atoll, 他的作用是把字符串变成 类型举例 :
long long y = stoll("323423");这些函数的复杂度都是
- 我们可以很轻松想出暴力解, 代码如下:
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;-
我们可以得到一个结论:
-
对应每个,
-
核心代码如下:
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;
}





