PAT备考——甲级入门模拟

目录

一、简单模拟

1.大数据量时数据预处理能力(数据边输入边处理,边处理边输出)

2.vector的排序方法:

3.结构体构造函数的使用:

 4.c++各数据类型的表达范围

5.int 和 string 相互转换:

二、查找元素

1.时间的比较

2.快速从大到小从小到大排序

三、图形输出

四、日期处理(暂无)

五、进制转换(简单)

六、字符串处理——后面做字符串专题


一、简单模拟

这类主要包括经典题目:

  • A1002多项式相加
  • A1009多项式相乘
  • A1065超出long long范围的整数加减判断
  • A1042洗牌机
  • A1046简单的最短路径

这些题目分值以20居多,少数25(1002和1009)。考点分布在:

1.大数据量时数据预处理能力(数据边输入边处理,边处理边输出)

2.vector的排序方法:

struct poly{
    int e;
    double c;
    poly(int _e, double _c) : e(_e), c(_c) {}
};
vector<poly> p;
sort(p.begin(), p.end(), comp);

3.结构体构造函数的使用:

vector<poly> poly1;
poly1.push_back(poly(e,c));

 4.c++各数据类型的表达范围

类型范围
char1 个字节-128 到 127 或者 0 到 255
unsigned char1 个字节0 到 255
signed char1 个字节-128 到 127
int4 个字节-2147483648 到 2147483647(大概是±2E+10),也就是2^31
unsigned int4 个字节0 到 4294967295
signed int4 个字节-2147483648 到 2147483647
short int2 个字节-32768 到 32767
unsigned short int2 个字节0 到 65,535
signed short int2 个字节-32768 到 32767
long int8 个字节[-2^63, +2^63)
signed long int8 个字节同上
unsigned long int8 个字节0 to 18,446,744,073,709,551,615
float4 个字节+/- 3.4e +/- 38 (~7 个数字)
double8 个字节+/- 1.7e +/- 308 (~15 个数字)
long double16 个字节+/- 1.7e +/- 308 (~15 个数字)
wchar_t2 或 4 个字节1 个宽字符

以及超出表达范围后的处理:溢出后正负符号相反。

5.int 和 string 相互转换:

//int转string直接上c++11的to_string()
string a;
int b = 10;
a = to_string(b);
cout<<a;

//string转int
//*******1.stoi**********//
// stoi example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stoi
 
int main ()
{
  std::string str_dec = "2001, A Space Odyssey";//带英文字母的
  std::string str_hex = "40c3";                //十六进制的
  std::string str_bin = "-10010110001";        //二进制的
  std::string str_auto = "0x7f";                //自动的
 
  std::string::size_type sz;   // alias of size_t※没整明白
 
  int i_dec = std::stoi (str_dec,&sz);//带英文字母的
  int i_hex = std::stoi (str_hex,nullptr,16);//十六进制的
  int i_bin = std::stoi (str_bin,nullptr,2); //二进制的
  int i_auto = std::stoi (str_auto,nullptr,0); //自动的---用0
 
  std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n";
  std::cout << str_hex << ": " << i_hex << '\n';
  std::cout << str_bin << ": " << i_bin << '\n';
  std::cout << str_auto << ": " << i_auto << '\n';
 
  return 0;
}

二、查找元素

包括题目:

  • A1006机房的签入签出问题
  • A1011体育彩票胜平负问题
  • A1036男生最低分和女生最高分问题

考点体现在:

1.时间的比较

就是以格式  "hour:minute:second"  的形式怎么比较的问题。

方法一:转换成六位整数进行比较

scanf("%d:%d:%d",&h, &m, &s);

int intToTime(int h, int m, int s){
    int time = h*10000 + m*100 + s;
    return time;
}

方法二:拼接成字符串,在转换成六位整数比较(用上面的stoi) 

2.快速从大到小从小到大排序

//题目A1036 Boys vs Girls
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

const int maxn = 100;

struct stu{
    string name;
    char gender;
    string id;
    int score;
};

int n;
vector<stu> male, female;

bool comp1(stu a, stu b){
    return a.score<b.score;
}

bool comp2(stu a, stu b){
    return a.score>b.score;
}

int main()
{
    cin>>n;
    stu s, mMin, fMax;
    for(int i=0; i<n; i++){
        cin>>s.name>>s.gender>>s.id>>s.score;
        if(s.gender=='M') male.push_back(s);
        else female.push_back(s);
    }

    sort(male.begin(), male.end(), comp1);
    sort(female.begin(), female.end(), comp2);

    if(male.size()!=0) mMin=male[0];
    if(female.size()!=0) fMax=female[0];

    bool flag=true;

    if(female.size()!=0) cout<<fMax.name<<" "<<fMax.id<<endl;
    else {
        cout<<"Absent"<<endl;
        flag = false;
    }

    if(male.size()!=0) cout<<mMin.name<<" "<<mMin.id<<endl;
    else {
        cout<<"Absent"<<endl;
        flag = false;
    }

    if(flag) cout<<fMax.score-mMin.score;
    else cout<<"NA";

    return 0;
}

三、图形输出

考题有一个A1031,主要考察二维字符数组的驾驭能力。

二维字符串数组初始化不能用fill?

for(int i=0; i<n1; i++){
    for(int j=0; j<n2; j++){
        output[i][j]=' ';
    }
}

主要难点在序号容易乱

A1031采用矩阵输出方法实现的代码:

 

#include <iostream>
#include <string>
#include <cstring>
#include <vector>

using namespace std;
const int maxn=15;

int main()
{
    string input;
    char output[maxn][maxn];
    cin>>input;
    int n=input.size();
    int n1,n2,n3;
    n1=n3=(n+2)/3;
    n2=n-2*n1+2;

    for(int i=0; i<n1; i++){
        for(int j=0; j<n2; j++){
            output[i][j]=' ';
        }
    }

    int pos=0;
    //左
    for(int i=0; i<n1; i++){
        output[i][0]=input[pos++];
    }
    //下
    for(int i=0; i<n2-2; i++){
        output[n1-1][i+1]=input[pos++];
    }
    //上
    for(int i=n3-1; i>=0; i--){
        output[i][n2-1]=input[pos++];
    }
    for(int i=0; i<n1; i++){
        for(int j=0; j<n2; j++){
            cout<<output[i][j];
        }
        cout<<endl;
    }

    return 0;
}

四、日期处理(暂无)

五、进制转换(简单)

六、字符串处理——后面做字符串专题

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值