1. 程式人生 > 實用技巧 >11.14 補題

11.14 補題

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); } } }
View Code

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; } } } }
View Code

大佬程式碼:

#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;
}
View Code