标签:完全二叉树 end close lap continue rgb ace swa printf
7-1 阅览室,感觉思路还好,但是写的太繁琐了,借鉴一下过的代码,又写了一遍;,代码:
#include<bits/stdc++.h> using namespace std; int s[1010],w[1010]; int main() { int n; cin>>n; while(n--) { int b,hh,mm,ct=0; char ch; double sum=0; while(scanf("%d %c %d:%d",&b,&ch,&hh,&mm)&&b!=0) { if(ch==‘S‘) { w[b]=hh*60+mm; s[b]=1; } else if(ch==‘E‘&&s[b]==1) { sum+=hh*60+mm-w[b]; s[b]=0; ct++; } } if(ct==0)cout<<"0 0"<<endl; else { printf("%d %.0lf\n",ct,sum*1.0/ct); } } }
7-10 学了一个堆结构,也听懂了,但是做的时候还是有不少问题,从后面插入就能保证完全二叉树,负数的情况就是用字符串转数字的时候没考虑,还有最有有个点没过,看了半天也没看出来,我的代码:
#include<bits/stdc++.h> using namespace std; int tr[10050]; int che(string s) { int res=0; int flag=0; for(int i=0;i<s.size();i++) { if(s[i]>=‘0‘&&s[i]<=‘9‘) { if(i>=1&&s[i-1]==‘-‘)flag=1; res=res*10+s[i]-‘0‘; } } if(flag==1)res=-res; return res; } int vi(int a) { int res=0; while(a!=0) { res++; a=a>>1; } return res; } int main() { int n,m; cin>>n>>m; for(int i=1;i<=n;i++) { int a; cin>>a; if(i==1) { tr[i]=a; continue; } tr[i]=a; int t=i; while(t>1&&(tr[t]<tr[t/2])) { int tem=tr[t]; tr[t]=tr[t/2]; tr[t/2]=tem; t/=2; } } // for(int i=1;i<=n;i++)cout<<tr[i]<<endl; while(m--) { int x; cin>>x; string s; getline(cin,s); if(s[s.size()-1]==‘t‘) { if(tr[1]==x) { cout<<"T"<<endl; } else { cout<<"F"<<endl; } continue; } int y=0; y=che(s); int dx=0,dy=0; for(int j=1;j<=n;j++) { if(x==tr[j])dx=j; if(y==tr[j])dy=j; } if(s[1]==‘a‘) {//cout<<dx<<" "<<dy<<endl; dx=vi(dx); dy=vi(dy); if(dx==dy) { cout<<"T"<<endl; } else { cout<<"F"<<endl; } } if(s[4]==‘t‘) { if(dx==dy/2) { cout<<"T"<<endl; } else { cout<<"F"<<endl; } } if(s[4]==‘a‘) { if(dx/2==dy) { cout<<"T"<<endl; } else { cout<<"F"<<endl; } } } }
大佬代码:
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> PII; const int maxn = 1000 + 5; const double eps = 1e-9; int num[maxn]; int main() { int n, q; cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> num[i]; int tmp = i; while (tmp != 1) { if (num[tmp] < num[tmp / 2]) { swap(num[tmp], num[tmp / 2]); tmp /= 2; } else break; } } getchar(); string str; while (q--) { getline(cin, str); if (str.find("root") != string::npos) { int index = 0; bool fu = str[0] == ‘-‘; for (int i = 0; str[i] != ‘ ‘; i++) { if (str[i] != ‘-‘) { index *= 10; index += str[i] - ‘0‘; } } if (fu) index = -index; if (num[1] == index) cout << "T" << endl; else cout << "F" << endl; } else if (str.find("siblings") != string::npos) { int indexA = 0, indexB = 0, i; bool fuA = str[0] == ‘-‘; for (i = 0; str[i] != ‘ ‘; i++) { if (str[i] != ‘-‘) { indexA *= 10; indexA += str[i] - ‘0‘; } } if (fuA) indexA = -indexA; for (i = i + 1; str[i] != ‘ ‘; i++); bool fuB = str[i + 1] == ‘-‘; for (i = i + 1; str[i] != ‘ ‘; i++) { if (str[i] != ‘-‘) { indexB *= 10; indexB += str[i] - ‘0‘; } } if (fuB) indexB = -indexB; int numA = 0, numB = 0; for (int x = 1; x <= n; x++) if (indexA == num[x]) numA = x; for (int x = 1; x <= n; x++) if (indexB == num[x]) numB = x; if (numA / 2 == numB / 2) cout << "T" << endl; else cout << "F" << endl; } else if (str.find("parent") != string::npos) { int indexA = 0, indexB = 0, i; bool fuA = str[0] == ‘-‘; for (i = 0; str[i] != ‘ ‘; i++) { if (str[i] != ‘-‘) { indexA *= 10; indexA += str[i] - ‘0‘; } } if (fuA) indexA = -indexA; bool fuB = false; int xxx = 1; for (i = str.length() - 1; str[i] != ‘ ‘; i--) { if (str[i] == ‘-‘) fuB = true; else { indexB += (str[i] - ‘0‘) * xxx; xxx *= 10; } } if (fuB) indexB = -indexB; int numA = 0, numB = 0; for (int x = 1; x <= n; x++) if (indexA == num[x]) numA = x; for (int x = 1; x <= n; x++) if (indexB == num[x]) numB = x; if (numA == numB / 2) cout << "T" << endl; else cout << "F" << endl; } else if (str.find("child") != string::npos) { int indexA = 0, indexB = 0, i; bool fuA = str[0] == ‘-‘; for (i = 0; str[i] != ‘ ‘; i++) { if (str[i] != ‘-‘) { indexA *= 10; indexA += str[i] - ‘0‘; } } if (fuA) indexA = -indexA; bool fuB = false; int xxx = 1; for (i = str.length() - 1; str[i] != ‘ ‘; i--) { if (str[i] == ‘-‘) fuB = true; else { indexB += (str[i] - ‘0‘) * xxx; xxx *= 10; } } if (fuB) indexB = -indexB; int numA = 0, numB = 0; for (int x = 1; x <= n; x++) if (indexA == num[x]) numA = x; for (int x = 1; x <= n; x++) if (indexB == num[x]) numB = x; if (numA / 2 == numB) cout << "T" << endl; else cout << "F" << endl; } } return 0; }
标签:完全二叉树 end close lap continue rgb ace swa printf
原文地址:https://www.cnblogs.com/Kingstar1/p/14019095.html