7-2 就是按照大佬的思路就是先从第一个座位开始坐,然后按照规则加上去,先把每一个队伍的编号都存起来,在最后输出:
#include <cstdio> #include <algorithm> #include <iomanip> #include <iostream> #include <cmath> #include <string> #include <vector> #include <set> #include <queue> #include <cstring> #include <stack> #include <cassert> #include <map> #include <stdlib.h> #include <ctype.h> #define cl(x) memset(x,0,sizeof(x)) #define rep(i,a,b) for(i=a;i<=b;i++) #define drep(i,a,b) for(i=a;i>=b;i--) #define em(x) emplace(x) #define emb(x) emplace_back(x) #define emf(x) emplace_front(x) #define fi first #define se second #define pb push_back #define de(x) cerr<<#x<<" = "<<x<<endl #define __i __int128 using namespace std; //using namespace __gnu_pbds; typedef long long LL; typedef pair<int, int> pii; typedef pair<LL, LL> pLL; const int N = 1.1e5, M = 1.5e6; LL ar[N], br[N]; LL n, m, k; struct p{ vector<int>q; int cnt ; }node[N]; void answer() { cin >> n; int sum = 0; for(int i =1; i <= n; i++){ cin >>ar[i]; node[i].cnt = ar[i] * 10; sum += ar[i] * 10; } int num = 1; while(sum --){ for(int i = 1; i <= n; i++){ // cout << i <<endl; if(node[i].q.size() != node[i].cnt){ if(node[i].q.size() != 0 && node[i].q.back() == num - 1 ){ node[i].q.pb(num + 1); num += 2; } else node[i].q.pb(num ++); } } } for(int i = 1; i <= n; i++){ cout << "#" << i << endl; for(int j = 0; j < node[i].q.size(); j ++){ if(j % 10 != 0) cout << " ";if(j % 10 == 0 && j != 0)cout << endl; cout << node[i].q[j]; }cout << endl; } return; } int main() { // ios::sync_with_stdio(0); // cin.tie(0), cout.tie(0); // //LL t ; // //cin >>t; // while(t--){ answer(); // } return 0; }
7-6 思路是对的,主要就错在我用int类型存输出的时候还要加上前导0:
#include<bits/stdc++.h> using namespace std; #define LL long long int vis[1000500]; int main() { int t; cin>>t; while(t--) { int v; cin>>v; for(int i=0;i<v;i++) { int b; cin>>b; if(v!=1) vis[b]=1; } } int m; cin>>m; vector<int>p; p.clear(); for(int i=0;i<m;i++) { int v; cin>>v; if(vis[v]==0)p.push_back(v); vis[v]=1; } if(p.size()==0)cout<<"No one is handsome"<<endl; else { printf("%05d",p[0]); for(int i=1;i<p.size();i++) { printf(" %05d",p[i]); } } }
7-12 有点难懂,先附上代码
#include<bits/stdc++.h> using namespace std; int zhong[1000],qian[1000]; struct node{ int data; struct node *left,*right; }; struct node *creat(int q,int z,int n){ //创建树 struct node *T; int i; if(n<=0){ T=NULL; } else{ T=(struct node *)malloc(sizeof(struct node)); T->data=qian[q]; for(i=0;zhong[i+z]!=qian[q];i++); T->left=creat(q+1,z,i); T->right=creat(q+i+1,z+i+1,n-i-1); } return T; } struct node *change(struct node *T){ //转换树 struct node *t; if(T){ if(T->left!=NULL || T->right!=NULL){ t=T->left; T->left=T->right; T->right=t; } change(T->left); change(T->right); } return T; } void cengci(struct node *T,int n){ //层次输出树 struct node *q[100],*p; int f=0,r=0,cnt=0; if(T){ q[r++]=T; while(f!=r){ p=q[f++]; printf("%d",p->data); cnt++; if(cnt!=n){ cout<<" "; } else cout<<endl; if(p->left!=NULL) q[r++]=p->left; if(p->right!=NULL) q[r++]=p->right; } } } int main(){ struct node *T; int i,n; cin>>n; for(i=0;i<n;i++){ cin>>zhong[i]; } for(i=0;i<n;i++){ cin>>qian[i]; } T=creat(0,0,n); T=change(T); cengci(T,n); }
7-13 感觉用树状数组做比较简单,层序遍历直接输出就可以,但是自己做完有些点没过,别人过的代码:
#include<bits/stdc++.h> using namespace std; int t[20]; int num,i; void bulid(int pos) { if(t[pos]==0) { t[pos]=num; } else { if(num>t[pos]) bulid(pos*2); else bulid(pos*2+1); } } int main() { int N; cin>>N; for(int i=1;i<=N;i++) //while(N--) 是因为N在下面还要用,这样会改变N的值,出现过多次这样的错误,请注意。 { cin>>num; bulid(1); } int flag=1; for(int i=1,count=1;count<=N;i++) { if(t[i]==0)//因为读入的时候,数组内以所有的空间都是存储完全的,但若是建立完成后,数组不再存储完全,则说明不是完全二叉树 flag=0; else { cout<<t[i]; if(count++!=N) cout<<" "; } } cout<<endl; if(flag) cout<<"YES"<<endl; else cout<<"NO"<<endl; return 0; }
浙公网安备 33010602011771号