PAT (Basic Level) 1011-1020

目录

1011 A+B和C

1012 数字分类

1013 数素数

1014 福尔摩斯的约会

1015 德才论

1016 部分A+B

1017 A除以B

1018 锤子剪刀布

1019 数字黑洞

1020 月饼


1011 A+B和C

水题,记得变量类型设置为long long

#include<iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;
    long long a,b,c;
    
    for(int i=1;i<=n;i++)
    {
        cin>>a>>b>>c;
        cout<<"Case #"<<i<<": ";
        if(a+b>c)
            cout<<"true"<<endl;
        else
            cout<<"false"<<endl;
    }
}

1012 数字分类

出现不通过的地方主要是第四个数的小数位数问题;

注意:

输出x的i位有效数字:cout<<setprecision(i)<<x;

保留x的i位小数:cout<<fixed<<setprecision(i)<<x;

另外,保留小数时先把x改为double类型。

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

int main()
{
    int n;
    cin>>n;
    int a1=0,a2=0,a3=0,a4=0,a5=-1;
    int c1=0,c2=0,c3=0,c4=0,c5=0;
    int flag=1;
    
    while(n--)
    {
        int temp;
        cin>>temp;
        
        if(temp%5==0&&temp%2==0)
        {
            a1+=temp;
            c1++;
        }
        else if(temp%5==1)
        {
            a2+=temp*flag;
            c2++;
            flag=-flag;//用于实现变号交错相加
        }
        else if(temp%5==2)
        {
            a3++;
            c3++;
        }
        else if(temp%5==3)
        {
            a4+=temp;
            c4++;
        }
        else if(temp%5==4)
        {
            if(temp>a5)
                a5=temp;
            c5++;
        }
    }
    
    if(c1==0)
        cout<<"N";
    else
        cout<<a1;
    if(c2==0)
        cout<<" N";
    else
        cout<<" "<<a2;
    if(c3==0)
        cout<<" N";
    else
        cout<<" "<<a3;
    if(c4==0)
        cout<<" N";
    else
        cout<<" "<<fixed<< setprecision(1)<<a4*(1.0)/c4;
        //printf(" %.1f",a4*(1.0)/c4);//这个也可以用于设置输出位数
    if(c5==0)
        cout<<" N";
    else
        cout<<" "<<a5;
}

1013 数素数

水题。写一个bool型用于判别是否为素数。

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

bool IsPrime(int x)
{
    if(x<2)
        return 0;
    for(int i=2;i<=sqrt(x);i++)
    {
        if(x%i==0)
            return 0;
    }
    
    return 1;
}

int main()
{
    int m,n,counter=0;
    cin>>m>>n;
    
    for(int i=0;counter!=n;i++)
    {
        if(IsPrime(i))
        {
            counter++;
            if(counter>=m&&counter<n)
            {
                if((counter-m+1)%10==0)
                    cout<<i<<endl;
                else
                    cout<<i<<" ";
            }
            else if(counter==n)
                cout<<i;
        }
    }
}


1014 福尔摩斯的约会

(1)主要是理解题意,相同的字符指的是两个字符串相同位置字符相同;

(2)ASCII码对照;

(3)<cctype>中isalpha()函数,类似isdigit(),isalnum()。

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

int main()
{
    string week[7]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
    string s1,s2,s3,s4;
    cin>>s1>>s2>>s3>>s4;
    int counter=0;
    
    for(int i=0;i<s1.length()&&counter<2;i++)
    {
        if(s1[i]==s2[i])
        {
            if(counter==0&&s1[i]>='A'&&s1[i]<='G')
           {
               cout<<week[s1[i]-65];
               counter++;
           }
           else if(counter==1)
           {
               if(s1[i]>='0'&&s1[i]<='9')
               {
                   cout<<" 0"<<s1[i]<<":";
                   break;
               }
               else if(s1[i]>='A'&&s1[i]<='N')
               {
                   cout<<" "<<s1[i]-55<<":";//A代表10,'A'=65,因此减去55即可对应
                   break;
               }
           }
        }  
    }
    
    for(int i=0;i<s3.size();i++)
	{
		if(s3[i]==s4[i] && isalpha(s3[i]))
		{
			if(i<10)
                cout<<"0";
            cout<<i;
		}
	}
}


1015 德才论

写的时候容器内数据跟容器名称都设成s了,debug了好久才找到原因..

(1)怕超时的话第55行endl可以换成’\n’;

(2)这里vector<student> s[4]类似二维数组;

(3)主要还是读题目,慢慢理顺了的话排序规则和容器存储都很明晰;

(4)有的答主说这里要用快速排序,其他时间复杂度为O(n^2)的算法会超时,但我搜了下sort()好像是结合了插入排序和堆排序的快速排序,并且这里用也没有出现问题。

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

struct student
{
    string number;
    int de;
    int cai;
};

bool cmp(student a,student 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.number<b.number;
}

int main()
{
    vector<student> s[4];
    int N,L,H,d,c;
    string number;
    cin>>N>>L>>H;
    int sum=N;
    
    for(int i=0;i<N;i++)
    {
        cin>>number>>d>>c;
        student t={number,d,c};
        
        if(d>=H&&c>=H)//第一类
            s[0].push_back(t);
        else if(d>=H&&c>=L)//第二类
            s[1].push_back(t);
        else if(d>=L&&c>=L&&d>=c)//第三类
            s[2].push_back(t);
        else if(d>=L&&c>=L)//第四类
            s[3].push_back(t);
        else
            sum--;
    }

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

1016 部分A+B

int atoi(const char* str);

其中,参数str是要转换的字符串,返回值是转换后的整数,需要注意的是参数字符串的格式为:

[空格][符号][数字]

其中,空格可以是键盘中的空格字符或者是Tab字符;符号可以是表示正数的“+”,也可以是表示负数的“-”;数字即为数字字符串;

char *c_str();

c_str()返回一个指向正规C字符串的指针常量,内容与string相同,这是为了与C兼容,C中没有string类型,因此把string对象转换为C中的字符串样式;

需要注意的是,c_str()返回的指针需要通过函数来进行操作。

#include<iostream>
using namespace std;

int main()
{
    string s1,s2;
    char a,b;
    cin>>s1>>a>>s2>>b;
    
    string ss1="",ss2="";
    
    for(auto x:s1)
    {
        if(x==a)
            ss1=ss1+a;
    }
    for(auto x:s2)
    {
        if(x==b)
            ss2=ss2+b;
    }
    
    int p1=atoi(ss1.c_str());
    int p2=atoi(ss2.c_str());
    
    cout<<p1+p2;
}

1017 A除以B

大数除法

#include<iostream>
using namespace std;

int main() 
{
	string A, Q="";
	int B, R = 0;
	cin >> A >> B;
	for(char a : A) 
    {
		R = R*10+a-'0';
		Q += R/B + '0';
		R %= B;
    }
	while(!Q.empty() && Q.front() == '0') 
        Q.erase(Q.begin());
    
    if(Q.size()==0)
        cout<<"0"<<" "<<R;
    else 
        cout<<Q<<" "<<R;
} 

1018 锤子剪刀布

水题,主要是用两个长度为三的数组存放两个人三种出拳方法赢的情况。

#include<iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;
    int a[3]={0};//0锤赢,1剪赢,2布赢
    int b[3]={0};
    int draw=0;
    
    while(n--)
    {
        char A,B;
        cin>>A>>B;
        
        if(A=='C'&&B=='J')
            a[0]++;
        else if(A=='C'&&B=='B')
            b[2]++;
        else if(A=='J'&&B=='C')
            b[0]++;
        else if(A=='J'&&B=='B')
            a[1]++;
        else if(A=='B'&&B=='C')
            a[2]++;
        else if(A=='B'&&B=='J')
            b[1]++;
        else 
            draw++;
    }
    
    int sumA=0,sumB=0;
    sumA=a[0]+a[1]+a[2];
    sumB=b[0]+b[1]+b[2];
    
    cout<<sumA<<" "<<draw<<" "<<sumB<<endl;
    cout<<sumB<<" "<<draw<<" "<<sumA<<endl;
    
    int maxA=0,maxB=0;
    maxA=max(max(a[0],a[1]),a[2]);
    maxB=max(max(b[0],b[1]),b[2]);
    
    if(maxA==a[2]) cout<<"B ";      //按字母序输出 
	else if(maxA==a[0]) cout<<"C ";
	else if(maxA==a[1]) cout<<"J ";
	if(maxB==b[2]) cout<<"B";
	else if(maxB==b[0]) cout<<"C";
	else if(maxB==b[1]) cout<<"J";
	return 0;
}

1019 数字黑洞

容易出错的地方主要是:

(1)每次重置s时忘记补0,记得用insert()补0;

(2)如果用while(){} 而不是 do{}while();测试点5的输出会出错(直接输入6174/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.size(),'0');//补充至四位数
    
    do
    {
        string a=s,b=s;
        sort(a.begin(),a.end(),cmp);//降序
        sort(b.begin(),b.end());//升序
        
        int d=stoi(a)-stoi(b);
        s=to_string(d);
        s.insert(0,4-s.size(),'0');
        cout<<a<<" - "<<b<<" = "<<s<<endl;
    }while(s!="0000"&&s!="6174");
}

1020 月饼

(1)总感觉跟算法课上背包问题的一种情况很像,但没那么复杂;

(2)采用结构体存储, 求出每个月饼的性价比, 排序, 按顺序遍历即可;

(3)结构体中用double,不然测试点2报错。

#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;

struct mooncake
{
    double num;
    double price;//单价
};

bool cmp(mooncake a,mooncake b)
{
    return a.price>b.price;
}

int main()
{
    int N,D;
    cin>>N>>D;
    mooncake a[N];
    
    for(int i=0;i<N;i++)
        cin>>a[i].num;
    for(int i=0;i<N;i++)
    {
        cin>>a[i].price;
        a[i].price=a[i].price/a[i].num;//单价
    }
    
    sort(a,a+N,cmp);//降序排序
    int i=0,type=0;
    double sum=0;
    
    while(i++!=D)
    {
        sum+=a[type].price;
        a[type].num--;
        if(a[type].num==0)
            type++;//卖完了换下一种
    }
    cout<<fixed<<setprecision(2)<<sum;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值