6 条题解
-
2
#include<bits/stdc++.h> using namespace std; int n,s; char a[1010][2]; int main(){ cin>>n; if(n0){ cout<<s; }else{ for(int i=1;i<=n;i++){ cin>>a[i][0]>>a[i][1]; } for(int i=1;i<=n;i++){ if(a[i][0]!='*'){ int y=a[i][0]-'0'; if(a[y][0]'' && a[y][1]==''){//判断是否是叶子结点 s=s+(a[i][0]-'0'); } } } cout<<s; } }
-
1
#include<bits/stdc++.h> using namespace std; int n; struct{ char l,r; }a[1001];//定义每个节点的左节点和右节点 int ans; int s; int main(){ cin>>n; for(int i=1;i<=n;++i){ cin>>a[i].l>>a[i].r;//输入 } for(int i=1;i<=n;++i){ if(a[i].l!='*'){ s=a[i].l-'0'; if(a[s].l=='*'&&a[s].r=='*'){//判断 ans+=s;//加到ans里面 } } } cout<<ans;//输出 exit(0); } -
0
#include<bits/stdc++.h> using namespace std; char ch,c1; int s,f,a[1000002][2]; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>ch>>c1; if(ch=='*'&&c1=='*'){ a[i][1]=1; } if(ch!='*'){ int t=ch-48; a[t][0]=t; } } for(int i=1;i<=n;i++){ if(a[i][0]&&a[i][1]){ s+=a[i][0]; } } cout<<s; return 0; } -
0
很多人很膨胀哈,用AI? 切,以下是本题正解
#include<bits/stdc++.h> using namespace std; char ch,c1; int s,f,a[1000002][2]; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>ch>>c1; if(ch=='*'&&c1=='*'){ a[i][1]=1; } if(ch!='*'){ int t=ch-48; a[t][0]=t; } } for(int i=1;i<=n;i++){ if(a[i][0]&&a[i][1]){ s+=a[i][0]; } } cout<<s; return 0; } -
-11
#include <iostream> #include <vector> #include <string> #include <sstream> #include <utility> // 用于pair using namespace std; int sumOfLeftLeaves() { // 读取总节点数 string line; getline(cin, line); int n = atoi(line.c_str()); // C++03没有stoi,使用atoi if (n == 0) { return 0; } // 存储每个节点的左右子节点信息 // C++03中vector内的空格不能省略,且需显式指定模板参数 vector<pair<string, string> > children(n + 1); for (int i = 1; i <= n; ++i) { getline(cin, line); istringstream iss(line); string left, right; iss >> left >> right; // C++03需要使用make_pair创建pair对象 children[i] = make_pair(left, right); } // 计算左叶子之和 int total = 0; // 遍历所有可能的父节点 for (int parent = 1; parent <= n; ++parent) { string leftChild = children[parent].first; // 检查是否有左子节点 if (leftChild != "*") { int leftChildNum = atoi(leftChild.c_str()); // 检查左子节点是否为叶子节点 if (children[leftChildNum].first == "*" && children[leftChildNum].second == "*") { total += leftChildNum; } } } return total; } int main() { cout << sumOfLeftLeaves() << endl; return 0; } ```
- 1
信息
- ID
- 2819
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- 递交数
- 70
- 已通过
- 19
- 上传者