#include<iostream>#include<vector>#include<algorithm>usingnamespace std;intvecsum(vector<int> vec){
int sum =0;for(auto i : vec){
sum += i;}return sum;}intmain(){
int count;
cin >> count;
vector<vector<int>>vec(count,vector<int>(5,0));
vector<int>res(count,0);for(int i =0; i < count; i++){
for(int j =0; j <5; j++){
cin >> vec[i][j];}}bool flag =false;for(int p =0; p < count; p++){
flag =false;for(int i =0; i <3; i++){
for(int j = i +1; j <4; j++){
for(int k = j +1; k <5; k++){
if((vec[p][i]+vec[p][j]+vec[p][k])%10==0){
res[p]=(vecsum(vec[p])-(vec[p][i]+vec[p][j]+vec[p][k]))%10;
flag =true;break;}}if(flag)break;}if(flag)break;else res[p]=-1;}}for(auto i : res) cout << i << endl;system("pause");return0;}
B题目:
#include<iostream>#include<vector>#include<algorithm>usingnamespace std;intmain(){
int count, step;
cin >> count >> step;
vector<int>vec(count,0);for(int i =0; i < count; i++) cin >> vec[i];sort(vec.begin(),vec.end());if(vec[vec.size()-1]- vec[0]< step){
cout <<0;return0;}int res =1;int preCur =0;for(unsignedint i =1; i < vec.size(); i++){
if(vec[i]- vec[preCur]>= step){
res++;
preCur = i;}}
cout << res;system("pause");return0;}
C题目:
#include<iostream>#include<algorithm>#include<vector>usingnamespace std;intmain(){
int n;
cin >> n;
vector<vector<int>>arr(n +1,vector(3,0));
vector<int>res(n +1,0);// 从 1 开始计数for(int i =1; i <= n; i++){
cin >> arr[i][0]>> arr[i][1]>>arr[i][2];}int p =0;for(int i =1; i <= n; i++){
int dao;
dao = res[p]+ arr[p][1]< arr[i][0]? arr[i][0]- res[p]- arr[p][1]:0;// 当前来的人和上一个人之间是否有空闲
res[i]= res[p]+ arr[p][1]- arr[i][0]> arr[i][2]?-1: res[p]+ arr[p][1]+ dao;// 超时为 -1 ,不超时计算if(res[i]!=-1) p = i;// 上一个打上饭同学的下标}for(int i =1; i <= n ; i++) cout << res[i]<<" ";system("pause");return0;}
D题目:
#include<iostream>#include<vector>usingnamespace std;// 使用的是LeetCode中对树的定义struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;TreeNode():val(0),left(nullptr),right(nullptr){
}TreeNode(int x):val(x),left(nullptr),right(nullptr){
}TreeNode(int x, TreeNode *left, TreeNode *right):val(x),left(left),right(right){
}};intmain(){
int count;// 输入数量
cin >> count;
vector<int>vec(count +1,0);// 0 作为假根,从 1 开始计数
vector<int>res(count +1,0);for(int i =1; i <= count; i++) cin >> vec[i];
TreeNode* source =newTreeNode(0);// 搞一个“0”的假根
TreeNode* father = source;
TreeNode* root =newTreeNode(vec[1]);
father->right = root;for(int n =2; n <= count; n++){
// 每次都去遍历一次已经建好的树
TreeNode* t =newTreeNode(vec[n]);
TreeNode* cur = root;
father = source;int breech =0;while(true){
if(!cur)break;// 如果当前位置没有元素,则vec[n]应该放在这个地方if(vec[n]> cur->val){
// 进入右子树
father = cur;
cur = cur->right;
breech =2;// 分支标志}else{
// 进入左子树
father = cur;
cur = cur->left;
breech =1;}}if(breech ==1) father->left = t;if(breech ==2) father->right = t;
res[t->val]= father->val;// 顺便记录父节点信息}for(int i =1; i <= count; i++) cout << res[i]<<" ";//system("pause"); return0;}