Pat刷题第一周

pat刷题(第一周)

第一题【PAT B1001】害死人不偿命的(3n+1)猜想

https://pintia.cn/problem-sets/994805260223102976/problems/994805325918486528

#include<iostream>
using namespace std;
int main(){
    int n,step=0;
    cin>>n;
    while(n!=1){
        if(n%2==0) n=n>>1;
        else n=(3*n+1)/2;
        step++;
    }
    cout<<step<<endl;
    return 0;
}

第二题【PAT B1032】挖掘机技术哪家强

https://link.zhihu.com/?target=https%3A//pintia.cn/problem-sets/994805260223102976/problems/994805289432236032

#include<bits/stdc++.h>

using namespace std;
int main(){
    map<int,int> mp;
    int n=0;
    cin>>n;
    while(n--){
        int key;int value;
        cin>>key>>value;
        mp[key]+=value;
    }
    int max=-888888;
    int id=-888888;
    for(auto v: mp){
        if(v.second >max){
            id=v.first;
            max= v.second;
        }
    }
    cout<<id<<" "<<max<<endl;
    return 0;
}

第三题【PAT B1011】A+B 和 C

https://pintia.cn/problem-sets/994805260223102976/problems/994805312417021952

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

int main()
{

int n;
int cnt=1;//用于计数
cin>>n;
long long int A,B,C;
while(n--){
    cin>>A>>B>>C;
    if((A+B-C)>0){
        cout<<"Case #"<<cnt<<": true"<<endl;
    }else{
           cout<<"Case #"<<cnt<<": false"<<endl;
    }
    cnt++;
}
    return 0;
}

第四题【PAT B1016】部分A+B

https://pintia.cn/problem-sets/994805260223102976/problems/994805306310115328

#include <iostream>
#include<utility>
#include<string>
using namespace std;
int CountNum(int X, int DX){
  int Count = 0, PX = 0;
  while(X){
    if(X%10==DX) PX=PX*10+DX;
    X/=10;

  }
  return PX;
}
int main()
{


  int A, DA, B, DB, PA, PB;
    cin >> A >> DA >> B >> DB;
    cout << CountNum(A, DA) + CountNum(B,DB) << endl;
    return 0;
}

第五题【PAT B1026】程序运行时间)

https://pintia.cn/problem-sets/994805260223102976/problems/994805295203598336

//#include<bits/stdc++.h>
#include <iostream>
#include<utility>
#include<string>
#include <math.h>
#include<iomanip>
using namespace std;

int main()
{


 int c1,c2;
 cin>>c1>>c2;
 int cnt=round((c2-c1)/100.0);
 int hh,mm,ss;
 hh=cnt/3600;
 mm=(cnt%3600)/60;
 ss=cnt-hh*3600-mm*60;
 cout<<setw(2)<<setfill('0') <<hh<<":"<<setw(2)<<setfill('0') <<mm<<":"<<setw(2)<<setfill('0')<<ss<<endl;
    return 0;
}

第六题【PAT B1046】划拳

https://pintia.cn/problem-sets/994805260223102976/problems/994805277847568384

题目最后输出给的有问题,应该输出的是甲 乙 而不是乙 甲

#include <iostream>
#include<utility>
#include<string>
#include <math.h>
#include<iomanip>
using namespace std;

int main()
{

int n;

//A代表甲喝的杯数 B代表乙喝的杯数
int A=0,B=0;
int i=0;
cin>>n;
while(n--){
        //AT 代表甲喊  BT代表乙喊   AH代表甲划  BH代表乙划
    int A1,B1,A2,B2;
    cin>>A1>>A2>>B1>>B2;
    if(((A1+B1)==A2) &&((A1+B1)!=B2)){
        A++;
    }
    if(((A1+B1)!=A2) &&((A1+B1)==B2)){
        B++;
    }

}
cout<<A<<" "<<B<<endl;
    return 0;
}

第七题【PAT B1008】数组元素循环右移问题

https://pintia.cn/problem-sets/994805260223102976/problems/994805316250615808

使用队列的方法来操作

using namespace std;

int main()
{

int n,m;
cin>>n>>m;
deque <int>res(n);

for (int i = 0;i<n;i++) cin >> res[i];
for(int i=0;i<m;i++){
    res.push_front(res[n-1]);
    res.pop_back();
}
for(int i=0;i<n;i++){
    cout << (i == 0 ? "" : " ");
		cout << res[i];
}
return 0;

}

第八题【PAT B1012】数字分类

本题参考的别人的:

思路:

开始没用flagx计数,然后就部分错误,后来想了想,发现几个A等于0的情况不一定就是N,还有的是真的算出0来。比如说A1,如果只有一个数字,而且是0输入,那么A1=0;比如说A2,如果有两个数字6、6输入,那么A2=6-6=0。

所以需要flagx来计数,看看到底是没有这一类的数据,还是算出了0值。

https://pintia.cn/problem-sets/994805260223102976/problems/994805311146147840

#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
    int N, A1=0, A2=0, A3=0, A4=0, A5=0;
    int tmp, flag=1, flag1=0, flag2=0, flag3=0, flag4=0, flag5=0;
    cin >> N;
    while(N--){
        cin >> tmp;
        if(tmp%10==0) {A1 += tmp; flag1++;}
        if(tmp%5==1){A2 += flag*tmp; flag *= -1; flag2++;}
        if(tmp%5==2) {A3++; flag3++;}
        if(tmp%5==3) {A4 += tmp; flag4++;}
        if(tmp%5==4 && tmp>A5) {A5 = tmp; flag5++;}
    }
    if(!A1 && !flag1) cout << "N";
    else cout << A1;
    if(!A2 && !flag2) cout << " N";
    else cout << " " << A2;
    if(!A3 && !flag3) cout << " N";
    else cout << " " << A3;
    if(!A4 && !flag4) cout << " N";
    else printf(" %.1f", (double)A4/(double)flag4);
    if(!A5 && !flag5) cout << " N";
    else cout << " " << A5;
    return 0;
}

第九题【数素数】

#include<bits/stdc++.h>
using namespace std;
bool isPrime(int n){
    for ( int i=2; i<=(int)sqrt(n); i++ ){//如果n被i整除,则返回false
        if(n%i==0){
            return false;
            break;
        }
    }
    return true;    // 反之则返回true
}

#include<stdio.h>
int main()
{
	int m, n, pri[10000] ; //(因为2~1000素数太多,放在博客太冗余)pri初始化自写百度,也就是2~10000中的所有素数
	cin>>m>>n;
	for (int i = m - 1; i < n - 1; i++)
	{
		if ((i - m + 1) % 10 == 9)
		cout<<pri[i]<<endl;
		else
			cout<<pri[i]<<" ";
	}
	cout<<pri[n-1];
	return 0;
}

第十题 【福尔摩斯的约会】

https://www.nowcoder.com/pat/6/problem/4040

注意点:

  • 对于临界点的等号不要少
  • 记得if里面的break不能丢,否则会有问题
#include<bits/stdc++.h>
using namespace std;
vector<string> week ={"MON","TUE","WED","THU","FRI","SAT","SUN"};
int main()
{
    string a1,a2,b1,b2;
    cin>>a1>>a2>>b1>>b2;
    int flag=false;
    int day;
    for(day=0;a1[day]&&a2[day];day++){
        if(a1[day]==a2[day] &&a1[day]>='A' &&a1[day]<='G'){
            cout<<week[a1[day]-'A']<<" ";
            break;
        }
    }
    int hh;
    for(hh=day+1;a1[hh]&&a2[hh];hh++){
        if(a1[hh]==a2[hh]){
            if(a1[hh]>='A' &&a1[hh]<='N'){

                    printf("%02d",a1[hh]-'A'+10);
                    break;
            }
            if(isdigit(a1[hh])){
                  printf("%02d",a1[hh]-'0');
                   break;

            }
        }
    }
    int ss;
    for(ss=0;b1[ss]&&b2[ss];ss++){
        if(b1[ss]==b2[ss] &&isalpha(b1[ss])){
              printf(":%02d",ss);
              break;
        }
    }

	return 0;
}

第十一题【查验身份证】

https://www.nowcoder.com/pat/6/problem/4057

#include<bits/stdc++.h>
using namespace std;
vector<int> weight ={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
//计算权重
    int returnWeight(vector<int> &weight,string arr){
         int sum=0;
        for(int i=0;i<17;i++){
                sum+=(arr[i]-'0')*weight[i];

        }
        return sum%11;
    }
    //判断前17个是否为数字
    bool Isdigit(string str){

      for(int i=0;i<17;i++){
            if(!isdigit(str[i])){
                return false;
            }

         }
         return true;
    }
int main()
{
    int n;
    cin>>n;
    //输出All pass or other
    int flag=1;
    while(n--){
        string str;
        cin>>str;
        vector<int> arr(17);
         for(int i=0;i<17;i++){
            arr[i]=str[i]-'0';
         }
         //计算加权平均和
         int sum=returnWeight(weight,str);
      char M[11]={'1','0','x','9','8','7','6','5','4','3','2'};



       if(!(Isdigit(str)==1 &&M[sum]==str[17])){
        flag=0;
            cout<<str<<endl;
       }



    }
    if(flag)
    {
        cout<<"All passed"<<endl;
    }


	return 0;
}

第十二题【个位数统计】

https://www.nowcoder.com/pat/6/problem/4047

#include<bits/stdc++.h>
using namespace std;

int main()
{
    map<int,int> mp;
    string str;
    cin>>str;
    for(int i=0;i<str.size();i++){
        mp[str[i]-'0']++;
    }
    for(auto v:mp){
        if(v.second!=0){
            cout<<v.first<<":"<<v.second<<endl;
        }
    }

	return 0;
}

第十三题【A除以B 】

https://www.nowcoder.com/pat/6/problem/4043

本题参考的答案

基本思想:

是手算除法的过程,比如100/2: 等价于1/2, 商为0, 余数为1, 商为0的情况下不能输输出,然后余数和下一位: 即0, 组合为110+0=10, 就变成10/2, 商为5余数为0,此时输出商数.然后0和下一位0组合为010+0=0, 0/2余数为0, 此时运算完毕。

链接:https://www.nowcoder.com/questionTerminal/25c3ae17bc99425b99542802ee882377
来源:牛客网

#include <iostream>
 
int main()
 
{
 
using namespace std;
 
 
 
string A;
 
char Q;
 
int B, R = 0;
 
int current;
 
 
 
cin >> A >> B;
 
 
 
for(int i = 0; i < A.length(); i++)
 
{
 
// 当前的被除数
 
current = R * 10 + (A[i] - '0');
 
// 除数
 
Q = current / B + '0';
 
// 余数
 
R = current % B;
 
 
 
// 当除数以0开头时,则不打印
 
if(i == 0 && Q == '0')
 
continue;
 
else
 
cout << Q;
 
}
 
cout << ' ';
 
cout << R;
 
 
 
return 0;
 
}

第十四题【剪刀石头布】

https://www.nowcoder.com/pat/6/problem/4044

#include <iostream>
using namespace std;

int main()
{
    int N;
    cin >> N;
    char tmp1, tmp2;
    int win=0, equ=0, lose=0;
    int C1=0, C2=0, B1=0, B2=0, J1=0, J2=0;
    while(N--){
        cin >> tmp1 >> tmp2;
        if(tmp1=='C' && tmp2=='C') {equ++;}
        else if(tmp1=='C' && tmp2=='J') {win++;C1++;}
        else if(tmp1=='C' && tmp2=='B') {lose++;B2++;}
        else if(tmp1=='J' && tmp2=='C') {lose++;C2++;}
        else if(tmp1=='J' && tmp2=='J') {equ++;}
        else if(tmp1=='J' && tmp2=='B') {win++;J1++;}
        else if(tmp1=='B' && tmp2=='C') {win++;B1++;}
        else if(tmp1=='B' && tmp2=='J') {lose++;J2++;}
        else if(tmp1=='B' && tmp2=='B') {equ++;}
    }
    cout << win << ' ' << equ << ' ' << lose << endl;
    cout << lose << ' ' << equ << ' ' << win << endl;
    if(B1>=C1 && B1>=J1) cout << "B ";
    else if(C1>=J1) cout << "C ";
    else cout << "J ";
    if(B2>=C2 && B2>=J2) cout << 'B';
    else if(C2>=J2) cout << 'C';
    else cout << 'J';
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

William_Tao(攻城狮)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值