PTA 练习(C++)

文章包含了C++编程中的几个实例,包括矩阵乘法、计算倒数第N个字符串、基于条件的逻辑判断(吃鱼吃肉决策)等,展示了基础的编程技巧和算法应用。
摘要由CSDN通过智能技术生成

L1-048 矩阵A乘以B

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int r1,c1,r2,c2;
    int sum=0;
    int a[200][200];
    int b[200][200];
    cin >> r1 >> c1;
    for(int i=0;i<r1;i++){
        for(int j=0;j<c1;j++){
            scanf("%d",&a[i][j]);
        }
    }
    cin >> r2 >> c2;
    for(int i=0;i<r2;i++){
        for(int j=0;j<c2;j++){
            scanf("%d",&b[i][j]);
        }
    }
    if(c1==r2){
        cout << r1 << " " << c2 << endl;
        for(int i=0;i<r1;i++){
            for(int j=0;j<c2;j++){
                sum=0;
                for(int k=0;k<c1;k++){
                sum +=a[i][k]*b[k][j];
                }
                cout << sum;
                if(j<c2-1){
                    cout << " ";
                }
            }
             cout << endl;
        }
    }else if(c1!=r2){
        printf("Error: %d != %d\n",c1,r2);
    }
    return 0;
}

L1-050 倒数第N个字符串

//26进制
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int l,n;
    cin >> l >> n;
    int m=pow(26,l);//最大值
    char str[l];
    n = m-n;//转为正序
    for(int i=0;i<l;i++){
        str[i]=n%26+'a';//提取各位数并转换为字符型
        n/=26;
    }
    for(int i=l-1;i>=0;i--){
        cout << str[i];//倒序输出每位字符
    }
    cout << endl;
    return 0;
}

L1-063 吃鱼还是吃肉

#include <iostream>
#include <string>
using namespace std;

struct number 
{
    int n1;
    int n2;
    int n3;
}p[20];

int main()
{
    int n;
    cin >> n ;
    for(int i=0;i<n;i++){
        cin >> p[i].n1 >> p[i].n2>>p[i].n3;
    }
    for(int i=0;i<n;i++){
        if(p[i].n1==1){
            if(p[i].n2>130){
                printf("ni li hai! ");
            }else if(p[i].n2<130){
                printf("duo chi yu! ");
            }else if(p[i].n2==130){
              printf("wan mei! ");
            }
            if(p[i].n3>27){
                printf("shao chi rou!\n");
        }else if(p[i].n3<27){
                printf("duo chi rou!\n");
        }else if(p[i].n3==27){
                printf("wan mei!\n");
        }
    }else if(p[i].n1==0){
           if(p[i].n2>129){
                printf("ni li hai! ");
            }else if(p[i].n2<129){
                printf("duo chi yu! ");
            }else if(p[i].n2==129){
               printf("wan mei! ");
            }
            if(p[i].n3>25){
                 printf("shao chi rou!\n");
        }else if(p[i].n3<25){
                printf("duo chi rou!\n");
        }else if(p[i].n3==25){
                printf("wan mei!\n");
        }   
    }
    }
    return 0;
}

L1-065 嫑废话上代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    cout << "Talk is cheap. Show me the code."<<endl;
    return 0;
}


L1-066 猫是液体

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n1,n2,n3;
    cin >> n1 >> n2 >> n3;
    cout << n1*n2*n3 << endl;
    return 0;
}


L1-067 洛希极限

#include <bits/stdc++.h>
using namespace std;
int main()
{
    double a,b,c,x=0;
    cin >> a >> b >> c;
    if(b==0){
     x = a*2.455;
    }else if(b==1){
        x=a*1.26;
    }
    if(x>c){
        printf("%.2f T_T\n",x);
    }else if(x<=c){
        printf("%.2f ^_^\n",x);
    }
    return 0;
}

L1-068 调和平均

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    double a[20000]={0};
    double sum=0;
    cin >> n;
    getchar();
    for(int i=0;i<n;i++){
        cin >> a[i];
    }
    for(int i=0;i<n;i++){
        sum += 1/(n*a[i]);
    }
    sum = 1/sum;
    printf("%.2f\n",sum);
    return 0;
}

L1-069 胎压监测

#include <bits/stdc++.h>
using namespace std;
int Max(int a,int b,int c,int d)
{
    int n1 = a>b?a:b;
    int n2 = n1>c?n1:c;
    int n3 = n2>d?n2:d;
    return n3;
}
int main()
{
    int x,y;
    int a[5]={0};
    int sum=0;
    int j=0;
    for(int i=0;i<4;i++){
       cin >> a[i]; 
    }
    cin >> x>> y;
    int max_num=Max(a[0],a[1],a[2],a[3]);//选出最大值
    for(int i=0;i<4;i++){
        if(a[i]<x||abs(a[i]-max_num)>y){
            sum++;
            j=i;
         } 
    }
    if(sum==0)printf("Normal\n");
    if(sum==1)printf("Warning: please check #%d!\n",j+1);
    if(sum>=2)printf("Warning: please check all the tires!\n");
    return 0;
}

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值