当前位置:网站首页>PAT甲级打卡-1001-1004
PAT甲级打卡-1001-1004
2022-08-02 02:24:00 【juraws】
1001 A+B Format
求a+b,但需要有逗号格式
#include <bits/stdc++.h>
using namespace std;
void print(int x) {
if(x == 0) {
cout << "0" ;
return;
}
string s = "";
bool flag = 0;
if(x < 0) {
flag = 1;
x = -x;
}
int cnt =0;
while(x) {
s =s + (char)('0' + x % 10);
x /= 10;
cnt++;
if(cnt && cnt % 3 ==0) s= s +',';
}
reverse(s.begin(), s.end());
if(s[0] == ',') s = s.substr(1);
if(flag) {
cout << "-";
}
cout << s;
}
signed main() {
ios::sync_with_stdio(false) , cin.tie(nullptr), cout.tie(nullptr);
int a, b;
cin >> a >> b;
int sum = a + b;
print(sum);
}
1002 A+B for Polynomials
给两个多项式,求和,按格式输出
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-7;
map<int, double> mp;
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int n;
while(cin >> n) {
for (int i = 1; i <= n; ++i) {
int k;
double a;
cin >> k >> a;
mp[k] += a;
}
cin >> n;
for (int i = 1; i <= n; ++i) {
int k;
double a;
cin >> k >> a;
mp[k] += a;
}
vector<pair<int, double> > ans;
for (auto[x, y]: mp) {
if(fabs(y) < eps) continue;
ans.push_back({
x, y});
}
reverse(ans.begin(), ans.end());
cout << ans.size();
for (auto tt : ans) {
cout << " " << tt.first << " " << fixed << setprecision(1) << tt.second;
}
cout << endl;
}
return 0;
}
1003 Emergency
我之前写过一道天梯赛的,一样的题不用输出方案 https://blog.csdn.net/qq_39602052/article/details/123804709
这个之前的博客链接有一组样例可以看看
n点m边无向图,点有点权,问从起点到终点的最短路同时获得最大权值,输出最短路条数和最大权值
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 510;
struct edges {
int to, len;
};
vector<edges> g[N];
int a[N];
int cnt[N];
struct node {
int u, dis, val;
bool operator<(const node &right) const {
if (this->dis == right.dis) return this->val > right.val;
return this->dis < right.dis;
}
};
pair<int, int> ans[N];
void dij(int s) {
priority_queue<node, vector<node> > que;
que.push({
s, 0, a[s]});
ans[s].first= 0, ans[s].second = a[s];
while (!que.empty()) {
auto tmp = que.top();
que.pop();
int u = tmp.u;
if (tmp.dis > ans[u].first || (tmp.dis == ans[u].first && tmp.val < ans[u].second)) continue;
for (auto tt : g[u]) {
int to = tt.to;
int len = tt.len;
if (ans[u].first + len < ans[to].first) {
ans[to].first = ans[u].first + len;
ans[to].second = ans[u].second + a[to];
que.push({
to, ans[to].first, ans[to].second});
} else if (ans[u].first + len == ans[to].first && ans[u].second + a[to] > ans[to].second) {
ans[to].second = ans[u].second + a[to];
que.push({
to, ans[to].first, ans[to].second});
}
}
}
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int n, m, s, t;
cin >> n >> m >> s >> t;
for (int i = 0; i <= n; ++i) ans[i].first = inf, ans[i].second = 0;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0, x, y, len; i < m; ++i) {
cin >> x >> y >> len;
g[x].push_back({
y, len});
g[y].push_back({
x, len});
}
dij(s);
cnt[s] = 1;
vector<pair<int,int>> vec;
for(int i =0;i < n; ++i) {
vec.push_back({
i, ans[i].first});
}
sort(vec.begin(), vec.end(),[&](auto x, auto y){
return x.second < y.second;});
for(auto [x, dis] : vec) {
for(auto [to, len] : g[x]) {
if(ans[to].first == ans[x].first+ len) {
cnt[to] += cnt[x];
}
}
}
cout << cnt[t] << " " << ans[t].second;
}
1004 Counting Leaves
给一棵树,问每层的叶子数
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
vector<int> g[N];
int cnt[N];
int mxdep = 0;
void dfs(int x, int p, int dep) {
if (g[x].empty()) cnt[dep]++;
mxdep = max(mxdep, dep);
for (auto to : g[x]) {
if (to == p) continue;
dfs(to, x, dep + 1);
}
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int x, k;
cin >> x >> k;
for (int j = 0; j < k; ++j) {
int y;
cin >> y;
g[x].push_back(y);
}
}
dfs(1, -1, 1);
for (int i = 1; i <= mxdep; ++i) {
cout << cnt[i];
cout << (i == mxdep ? "" : " ");
}
}
边栏推荐
- From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
- esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
- 罗德里格斯公式(Rodrigues‘ Rotation Formula)推导
- 考完PMP学什么?前方软考等着你~
- 个人博客系统项目测试
- Outsourcing worked for three years, it was abolished...
- NAS和私有云盘的区别?1篇文章说清楚
- 【LeetCode每日一题】——654.最大二叉树
- Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
- Simple example of libcurl accessing url saved as file
猜你喜欢

openGauss切换后state状态显示不对

【LeetCode Daily Question】——704. Binary Search

字符串常用方法

The state status is displayed incorrectly after the openGauss switch

Power button 1374. Generate each character string is an odd number

LeetCode brush diary: LCP 03. Machine's adventure

BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
![[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)](/img/ed/ffced88c9d23c20ccf380494051381.jpg)
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
![[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games](/img/8a/07ca69c6dcc22757156cb615e241f8.png)
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games

2022河南青训联赛第(三)场
随机推荐
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
leetcode / anagram in string - some permutation of s1 string is a substring of s2
bool框架::PosInGrid (const简历:关键点kp, int &posX, int诗句)
Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
【LeetCode每日一题】——704.二分查找
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
线程的不同状态
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
个人博客系统项目测试
BioVendor Human Club Cellular Protein (CC16) Elisa Kit Research Fields
BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
AI target segmentation capability for fast video cutout without green screen
永磁同步电机36问(二)——机械量与电物理量如何转化?
【LeetCode每日一题】——654.最大二叉树
Win Go development kit installation configuration, GoLand configuration
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
60 Feature Engineering Operations: Using Custom Aggregate Functions【Favorites】