zoukankan      html  css  js  c++  java
  • b_pat_模拟题(多项式相乘 / 洗牌 / 最短距离 / 填充螺旋矩阵)

    A+B for Polynomials

    #include<bits/stdc++.h>
    using namespace std;
    const int N=2005;
    double A[N],B[N],C[N];//C为结果数组
    //同指数,指数不变,系数相加,即C[i]+=A[i]+B[i]
    int main() {
        std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        int n,m;
        int x; double y;
        cin>>n; for (int i=0; i<n; i++) cin>>x>>y, A[x]+=y;
        cin>>m; for (int i=0; i<m; i++) cin>>x>>y, B[x]+=y;
    
        for (int i=0; i<N; i++) C[i]=A[i]+B[i];
        int cnt=0;
        for (int i=0; i<N; i++) if (C[i]) cnt++;
        cout<<cnt;
        for (int i=N-1; i>=0; i--) if (C[i]){
            printf(" %d %.1f", i, C[i]);
        }
        return 0;
    }
    

    Product of Polynomials

    注:系数大的在前,小的在后

    #include<bits/stdc++.h>
    using namespace std;
    const int N=2005;
    double A[N],B[N],C[N];//C为结果数组
    //指数相加,系数相乘,即C[i+j]+=A[i]*B[j]
    int main() {
        std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        int n,m;
        int x; double y;
        cin>>n; for (int i=0; i<n; i++) cin>>x>>y, A[x]+=y;
        cin>>m; for (int i=0; i<m; i++) cin>>x>>y, B[x]+=y;
    
        for (int i=0; i<=1000; i++)
        for (int j=0; j<=1000; j++) if (A[i]&&B[j]) {
            C[i+j]+=A[i]*B[j];
        }
        int cnt=0;
        for (int i=0; i<N; i++) if (C[i])
            cnt++;
        cout << cnt;
        for (int i=N; i>=0; i--) if (C[i]) {
            printf(" %d %.1f", i, C[i]);
        }
        return 0;
    }
    

    Shuffling Machine

    洗牌
    void *memcpy(void *destin, void *source, unsigned n);

    #include<bits/stdc++.h>
    using namespace std;
    const int N=55;
    int A[N], B[N], C[N];
    int main() {
        int k; cin>>k;
        for (int i=1; i<N; i++) A[i]=i-1;   //第一行(0~13)在m数组中的下标全都是0(x/13=0,x∈[0,12]),所以要i-1,i∈[1,N)
        int x, p=1;
        while (~scanf("%d", &x)) C[p++]=x;
        
        for (int i=0; i<k; i++) {
            for (int j=1; j<p; j++) {
                B[C[j]]=A[j];
            }
            memcpy(A, B, sizeof A);
        }
        char m[5]={'S', 'H', 'C', 'D', 'J'};
        printf("%c%d", m[A[1]/13], A[1]%13+1);
        for (int i=2; i<p; i++) {
            printf(" %c%d", m[A[i]/13], A[i]%13+1);
        }
        return 0;
    }
    

    Shortest Distance


    破换成链+前缀和

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N=2e5+5;
    int main() {
        std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        int n; cin>>n;
        ll s[N];
        for (int i=1; i<=n; i++) cin>>s[i], s[i+n]=s[i];
        for (int i=0; i<2*n; i++) s[i+1]+=s[i];
    
        int m,a,b; cin>>m;
        for (int i=0; i<m; i++) {
            if (i) cout<<'\n';
            cin>>a>>b; 
            if (a>b) swap(a,b);
            a--, b--;
            cout << min(s[b]-s[a], s[a+n]-s[b]);
        }
        return 0;
    }
    

    Spiral Matrix

    题目大意:给一个序列,将其放入一个R减C的值尽量小的矩阵中,且R≥C(要求螺旋式地降序放入)
    21/25分代码,有两个样例过不了...

    #include<bits/stdc++.h>
    using namespace std;
    bool cmp(const int a, const int b){
        return a>b;
    }
    int main() {
        std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        int N; cin>>N;
        int A[N+5]; for (int i=1; i<=N; i++) cin>>A[i];
        int n,m;
        for (int i=sqrt(N); i>0; i--) if (N%i==0) {
            m=i, n=N/i;
            break;
        }
        sort(A+1, A+N+1, cmp);
        int c=1, i=0, j=-1, g[n+5][m+5], st[n+5][m+5]; memset(st, false, sizeof st);
        while (c<N) {
            while (j+1<m && !st[i][j+1]) g[i][++j]=A[c++], st[i][j]=1;
            while (i+1<n && !st[i+1][j]) g[++i][j]=A[c++], st[i][j]=1;     
            while (j>0 && !st[i][j-1])   g[i][--j]=A[c++], st[i][j]=1;
            while (i>0 && !st[i-1][j])   g[--i][j]=A[c++], st[i][j]=1;
        }
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                if (j==m-1) cout << g[i][j];
                else cout<<g[i][j]<<' ';
            }
            if (i!=n-1)
                cout<<'\n';
        }
        return 0;
    }
    

    The Black Hole of Numbers

    给定数字 N 如果不足四位,则补充前导 0 至四位为止。
    输出格式
    如果 N 的四位数字都相同,则输出一行 N - N = 0000。
    否则,每行输出一个操作步骤,直到出现 6174 作为差值产生为止。

    思路
    do while+补全

    //字符串数字互转(浮点数也可)
    string s= "123.32";
    stringstream ss;  
    ss<<s;                  
    ss>>a;
    
    #include<bits/stdc++.h>
    using namespace std;
    void complete(string& s) {
        if (s.size()==4) return;
        int d=4-s.size();
        for (int i=0; i<d; i++) s="0"+s;
    }
    int main() {
        int n; scanf("%d", &n);
        string s=to_string(n), zero="0000";
        complete(s);
        do {
            string a=s, b=s;
            sort(a.rbegin(), a.rend()), sort(b.begin(), b.end());
            if (a==b) {
                printf("%s - %s = %s\n", a.c_str(), b.c_str(), zero.c_str());
                break;
            }
            n=stoi(a)-stoi(b); 
            s=to_string(n);
            complete(s);
            printf("%s - %s = %s\n", a.c_str(), b.c_str(), s.c_str());
        }  while (n!=6174);
        return 0;
    }
    
  • 相关阅读:
    redis特性,使用场景
    docker 界面话管理工具Portainer
    解决streamsets jdbc全量模式数据重复问题
    clickhouse 行列转换
    clickHouse 常用命令
    mysqldump导出数据
    oracle 迁移到clickhouse 45亿条数据
    clickHouse 导入/删除/更新数据
    clickhouse Mysql 数据类型对比
    maven 的使用
  • 原文地址:https://www.cnblogs.com/wdt1/p/13674972.html
Copyright © 2011-2022 走看看