pat 乙级 1011~1020

文章目录

1011
1012
1013
1014
1015
1016
1017
1018
1019
1020


1011

参考代码:

#include <iostream>
using namespace std;
int main() {
 int n;
 scanf("%d", &n);
 for (int i = 0; i < n; i++) {
 long long int a, b, c;
 scanf("%lld%lld%lld", &a, &b, &c);
 printf("Case #%d: %s\n", i + 1, a + b > c ? "true" : "false");
 }
 return 0; }

1012

解题思路:

1.将输入的数字存放到对5求余对应下标的二维数组中

2.计算A1–A5
1.A2:就是下标为偶数的加 - 下标为奇数的和

3.输出,用printf简单

参考代码:

/*
    解题思路:
    1.将输入的数字存放到对5求余对应下标的二维数组中
    2.计算A1--A5
            1.A2:就是下标为偶数的加 - 下标为奇数的和
    3.输出,用printf简单
*/
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int n,digit,A1=0,A2=0,A3=0,A5=-1;   //对应的变量注意设置出来
    double A4=0.0;
    cin >> n;
    vector<int> a[5];

    for(int i=0; i<n; i++)
    {
        cin >> digit;
        a[digit%5].push_back(digit);      //这里使用push_back就是将数字压入,自动生成一个二维数组
                                        //注意写法:数组[下标].push_back(要填入的数字);
    }

    for(int i=0; i<5; i++)
        for(int j=0; j<a[i].size(); j++)
        {
            if(i==0 && a[i][j]%2==0)
                A1+=a[i][j];
            if(i==1 && j%2==0)
                A2+=a[i][j];
            if(i==1 && j%2!=0)
                A2-=a[i][j];
            if(i==2)
                A3++;
            if(i==3)
                A4+=a[i][j];
            if(i==4 && a[i][j]>A5)     //一定要是大于A5
                A5=a[i][j];
        }
    for(int i=0; i<5; i++)
    {
        if(i!=0)
            cout << " ";
        if(i==0 && A1==0 || i!=0 && a[i].size()==0)
        {
            cout << "N";
            continue;
        }
        if(i==0)
            printf("%d",A1);
        if(i==1)
            printf("%d", A2);  //计算出结果为何不直接输出值?因为每一次输出都有条件限制,对于不存在的数输出N,
                            //为了保证思路的理解,就设置成循环,如果不满足条件就输出N,否则按照对应的值输出
        if(i==2)
            printf("%d",A3);
        if(i==3)
            printf("%.1f",A4/a[i].size());
        if(i==4)
            printf("%d",A5);
    }
    return 0;
}

注意事项:

1.==不是=
2.对于某个点一直过不去,重新敲一遍

1013

解题思路:

将从第M个素数到第N个素数放到数组中

参考代码:

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

bool isprime(int a)
{
    for(int i=2; i*i<=a; i++)
        if(a%i == 0)
            return false;
    return true;
}

int main()
{
    int M,N,num=2,cnt=0;
    cin >> M >> N;
    vector<int> v;
    /*
        1.将从第N个到第M素数压栈到数组v中
        2.对于以空格分隔,但行末不得有多余空格,在设置时可以cnt%10!=1
        3.每10个为1行,如果i%10==0,则输出一个空格
        4.下面两个循环都是一石二鸟:第1个统计素数,又是从第m个到第n个;第2个循环即输出10个1行又末尾没有空格

    */
    while(cnt < N)       //当cnt为n-1时,下面有cnt++,自然将第M个数放进去
    {
        if(isprime(num))
        {
            cnt++;
            if(cnt >= M)
                v.push_back(num);     //将素数压栈从下标为0开始
        }
        num++;          //num从2开始每次递增1,来寻找素数
    }
    cnt=0;
    for(int i=0; i<v.size(); i++)
    {
        cnt++; //cnt从0开始,自增1,所以cnt%10==1
        if(cnt%10 != 1)      //表示遇到每行第1个数,不输出空格后面都输出空格
            cout << " ";
        cout << v[i];
        if(cnt%10 == 0)         //每10个数为1行
            cout << endl;
    }
}



}

知识总结:一行有x个数,然后末尾不能有空格,格式如下:

        5.技巧总结:
            int cnt=0;
            for(int i=0; i<n; i++)
            {
                cnt++;
                if(cnt%x!=1)
                    cout << " ";
                cout << v[i];     //输出v[i]中的数字
                if(cnt%x==0)     //如果是x个数字一行,然后输出一个换行
                    cout << endl;
            }

1014

解题思路:

1.用两次while分别找出a,b字符串中第一个和第二个相同字符
2.同样也用while遍历循环c,d两个字符串

参考代码:

#include<iostream>
#include<string>
using namespace std;
/*
    解题思路:
    1.用两次while分别找出a,b字符串中第一个和第二个相同字符
    2.同样也用while遍历循环c,d两个字符串
*/
int main()
{
    string a,b,c,d;
    char t[2];
    cin >> a >> b >> c >> d;
    int i=0,j=0,e=0;
    //找到第一个相同的字符
    while(i<a.length() && i<b.length())
    {
        if(a[i]==b[i] && ('A'<=a[i] && a[i]<='G'))
        {
            t[0]=a[i];
            break;
        }
        i++;
    }
    i++;

    //找到第二个相同的字符
    while(i<a.length() && i<b.length())
    {
        if(a[i]==b[i])
        {
            if(isdigit(a[i]) || ('A'<=a[i] && a[i]<='N'))  //isdigit(a[i])判断a[i]是否为数字字符
            {
                t[1]=a[i];
                break;
            }
        }
        i++;
    }

    //找到第三个字符
    while(j<c.length() && j<d.length())
    {
        if(c[j]==d[j] && isalpha(c[j]))          //isalpha(c[j])判断c[j]是否为字母
        {
            e=j;
            break;
        }
        j++;
    }


    char sa[7][5]={"MON ","TUE ","WED ","THU ","FRI ","SAT ","SUN "};
    cout << sa[t[0]-'A'];
    int m=isdigit(t[1])? t[1]-'0': t[1]- 'A' + 10;    //切记这里用一个整形变量m来接受条件判断的结果,最后在输出
    printf("%02d:%02d",m,j);       //采用c的方式输出,是为了当数字为1位时,前面有0补充;否则直接输出2位数

    return 0;
}


知识总结:

isdigit(A)   //判断A是否为数字字符
isalpha(A)   //判断A是否为字母

1015

解题思路:

1.用一个结构stu来存放数据信息
2.设置一个排序函数,总分不同按照从大到小排列,总分相同,按照按照德分从大到小排列;德分相同,按照
3.设置1个结构数组,一个临时变量结构用来存放每次的输入;循环中嵌套if判断,将对应的结构压入栈中,也就形成了二维数组
4.最后压栈进行排序后并输出

参考代码:

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
/*
    解题思路:

*/
struct node
{
    int num,de,cai;
};

int cmp(struct node a, struct node b)
{
    if((a.de+a.cai)!=(b.de+b.cai))
        return (a.de+a.cai) > (b.de+b.cai);   //如果总分数不同,就按照最大到小的顺序排列
    else if(a.de!=b.de)                    //如果总分数相同,但德分不同,按照德分从大到小排列
        return a.de>b.de;         //从大到小
    else
        return a.num<b.num;            //从小到大
}


int main()
{
    int low,high,n;
    cin >> n >> low >> high;
    node temp;
    vector<node> v[4];
    int total=n;
    for(int i=0; i<n; i++)
    {
        scanf("%d %d %d", &temp.num,&temp.de, &temp.cai);
        if(temp.de < low || temp.cai < low)
            total--;
        else if(temp.de >= high && temp.cai >= high)
            v[0].push_back(temp);
        else if(temp.de >= high && temp.cai < high)
            v[1].push_back(temp);
        else if(temp.de < high && temp.cai < high && temp.de >= temp.cai)
            v[2].push_back(temp);
        else
            v[3].push_back(temp);

    }
    cout << total << endl;
    for(int i=0; i<4; i++)
    {
        sort(v[i].begin(),v[i].end(),cmp);
        for(int j=0; j<v[i].size(); j++)
            cout << v[i][j].num << " " << v[i][j].de << " " << v[i][j].cai <<endl;
    }
    return 0;

}


1016

解题思路:

1.用string存放要输入的字符串数组,用int来存放其他类型
2.判断是否有相同的字符,并且记录个数,最后通过循环来累加起来
3.两次循环记录相同数字的个数
4.根据相同数字个数是否为0,否就再循环算出对应的值

参考代码:

#include<iostream>
#include<string>
using namespace std;
/*
    解题思路:
    1.用string存放要输入的字符串数组,用int来存放其他类型
    2.判断是否有相同的字符,并且记录个数,最后通过循环来累加起来
*/
int main()
{
    string a,b;
    int a1,b1,num1=0,num2=0,suma=0,sumb=0;    //a1,b1表示要找的数字,num1,num2表示个数,suma,sumb表示和
    cin >> a >> a1 >> b >> b1;
    for(int i=0; i<a.size(); i++)      //记录相同数字的个数
    {
        if(a1 == a[i]-'0')
            num1++;
    }
    for(int i=0; i<b.size(); i++)        //记录相同数字的个数
    {
        if(b1 == b[i]-'0')
            num2++;
    }

    if(num1!=0)  
        suma=a1;   
    if(num2!=0)
        sumb=b1;
    for(int i=1; i<num1; i++)
        suma=suma*10+a1;
    for(int i=1; i<num2; i++)
        sumb=sumb*10+b1;
    cout << suma+sumb;
    return 0;
}


1017

解题思路:

1.模拟手动求余的过程
2.对于1000位的数字,用string;对于输出商,不用变量存储,直接输出

参考代码:

int main()
{
    string s;
    int a,t=0,temp=0;
    cin >> s >> a;
    t=(s[0]-'0')/a;    //t就是商
    /*
        1.对于只有一位的数,直接输出商t
        2.对于2位及2位以上,如17 ,93 这种情况,前者就不输出,后者输出1
    */
    if((t != 0 && s.length() > 1) || s.length() == 1)
        cout << t;
    temp = (s[0]-'0')%a;    //保存余数
    /*
        i从1开始,先计算下一位数的数,输出商,保存余数
    */
    for(int i=1; i<s.length(); i++)   
    {
        t=(temp*10+s[i]-'0')/a;       
        cout << t;
        temp=(temp*10+s[i]-'0')%a;
    }
    cout << " " << temp;
    return 0;
}


1018

解题思路:

1.统计甲赢的次数,乙赢的次数。甲输的次数就是赢的次数,同理,乙也是如此。平的次数就是总数-甲赢的次数-乙赢的次数
2.如何确定赢次数最多的手势?
答:将BCJ默认为012,在统计赢的次数时,记录甲乙赢的时候,对应手势所对应的下标的数组++,最后选出最大的个数,并且记住
下标,最后定义一个字符数组"BCJ"这样刚才统计的下标就可以字符数组相对应

参考代码:

#include<iostream>
using namespace std;
/*
    解题思路:
    1.jiawin  yiwin 分别记录甲赢的次数和已赢的次数
    2.a[]  b[]两个数组分别统计B,J,C赢的次数,默认为012
    3.最后对数组元素比较得出最大的数组下标,同时设计一个变量,也是BJC这样就输出了对应的值
*/
int main()
{
    int jiawin=0,yiwin=0;
    int jia[3]={0};
    int yi[3]={0};
    char s,t;
    int n;
    cin >> n;
    for(int i=0; i<n; i++)
    {
        cin >> s >> t;
        if(s=='B' && t=='C')
        {
            jiawin++;
            jia[0]++;
        }
        else if(s=='C' && t=='J')
        {
            jiawin++;
            jia[1]++;
        }
        else if(s=='J' && t=='B')
        {
            jiawin++;
            jia[2]++;
        }
        else if(s=='C' && t=='B')
        {
            yiwin++;
            yi[0]++;
        }
        else if(s=='J' && t=='C')
        {
            yiwin++;
            yi[1]++;
        }
        else if(s=='B' && t=='J')
        {
            yiwin++;
            yi[2]++;
        }
    }
    cout << jiawin << " " << n-jiawin-yiwin << " " << yiwin << endl << yiwin << " " << n-jiawin-yiwin << " " << jiawin << endl;
    int maxjia=jia[0]>=jia[1]?0:1;    //设计大于等于的目的是:首先字母已经从字母序从小到大排列,当a[0]==a[1]时,结果max1还是0,不然最后max1就指向j
    maxjia=jia[maxjia]>=jia[2]?maxjia:2;

    int maxyi=yi[0]>=yi[1]?0:1;
    maxyi=yi[maxyi]>=yi[2]?maxyi:2;
    char k[4]={"BCJ"};
    cout << k[maxjia] << " " << k[maxyi];
    return 0;
}

注意事项:

BCJ对应012


1019

解题思路:

1.还是用string类来存放输入的数字
2.自定义函数降序排列+结合sort升序排列+字符串转化为数字函数stoi()+数字转化为字符串函数to_string()+对不足四位的数补充0的相关函数s.insert(i,j,k)(题目要求四位输出)+do-while(当先输入0000时,还可以输出)

参考代码:

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(char a, char b)
{
    return a>b;
}
int main()
{
    string s;
    cin >> s;
    s.insert(0,4-s.length(),'0');    //输入的数字不满足4位,则前补充0
    do          //先输入do-while是为了,当输入6174时还能执行一次循环
    {
        string a=s,b=s;
        sort(a.begin(),a.end(),cmp);  //数字以从大到小排列
        sort(b.begin(),b.end());      //默认为从小到大
        int result=stoi(a)-stoi(b);    //stoi将字符串转化为整数
        s=to_string(result);            //将数字转化为字符
        s.insert(0,4-s.length(),'0');
        cout << a << " - " << b << " = " << s << endl;
    }while(s!="6174" && s!="0000");
    return 0;
}

知识总结:

1.对字符串第i个位置开始,插入j个k
s.insert(i,k,j);
2.对字符串进行排序
sort(s.begin(),s.end(),cmp);   //按照cmp的顺序排列
        sort(s.begin(),s.end());         //默认为从小到大排列
3.将字符串转化为整数
stoi(str)   //将字符串转化为整数
4.将数字转化为字符串
to_string(a)          //将数字转化为字符串
 

1020

解题思路:

1.构建一个结点里面有3个结构成员,分别是mount种类库存,price种类总价,unit种类单价

2.自定义cmp,按照单价的降序排列

3.设置循环,先紧着单价最高的使,然后用单价次一级的…这样最后利润一定是最高的

参考代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
/*
    解题思路:
    1.构建一个结点里面有3个结构成员,分别是mount种类库存,price种类总价,unit种类单价
    2.自定义cmp,按照单价的降序排列
    3.设置循环,先紧着单价最高的使,然后用单价次一级的....这样最后利润一定是最高的
*/

struct mooncake
{
    float mount,price,unit;
};
int cmp(mooncake a, mooncake b)
{
    return a.unit > b.unit;
}
int main()
{
    int n,need;
    cin >> n >> need;
    vector<mooncake> a(n); //且记要定义数组长度大小,否则不能连续for输入
    for(int i=0; i<n; i++)
        scanf("%f",&a[i].mount);  //切记,float类型的scanf是%f
    for(int i=0; i<n; i++)
        scanf("%f",&a[i].price);
    for(int i=0; i<n; i++)
        a[i].unit=a[i].price/a[i].mount;

    sort(a.begin(),a.end(),cmp);

    float result=0.0;
    for(int i=0; i<n; i++)
    {
        if(a[i].mount <= need)
            result = result+a[i].price;
        else
        {
            result=result+a[i].unit*need;
            break;
        }
        need=need-a[i].mount;
    }
    printf("%.2f",result);
    return 0;
}

知识总结:

1.对于vector定义的数组连续for输入需要指明数组长度
#include<iostream>
#include<vector>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> a(n),b(n);  //不定以数组长度大小,否则不能连续for输入
    for(int i=0; i<n; i++)
        cin >> a[i];
    for(int i=0; i<n; i++)
        cin >> b[i];
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值