likmin / pat-first-division Goto Github PK
View Code? Open in Web Editor NEWPAT甲级题目代码(部分)
PAT甲级题目代码(部分)
#include<iostream>
#include<vector>
using namespace std;
bool isSort(vector<int> v) {
for (int i = 0; i < v.size() - 1; i++) {
if (v[i] < v[i + 1])continue;
else return false;
}
return true;
}
int main()
{
int N, zero;//zero记录0的下标
cin >> N;
vector<int> v(N),D(N);
for (int i = 0; i < N; i++) {
cin >> v[i];
D[v[i]]=i;
if (v[i] == 0)zero = i;
}
int min = 0;
int j = 1;
while (!isSort(v)) {
int i;
if (zero== 0) {//此时v还不是有序序列,但是0的下标为0,所以找下一个最小的没有对号入座的
while (v[j] == j)
j++;
i = D[j];
}
else
i = D[zero];
swap(v[zero], v[i]);//交换0和i位置
swap(D[v[zero]], D[v[i]]);//交换0和i的下标位置
zero = i;
min++;
}
printf("%d", min);
return 0;
}
```
A = stoll(s.substr(0, s.size() / 2));
cout << s.substr(0, s.size() / 2) << endl;
B = stoll(s.substr(s.size() / 2+1, s.size()));
cout << s.substr(s.size() / 2 , s.size()-1) << endl;
如果s=1234,那么A=0,B=12,不科学啊?直接输出 s.substr(0, s.size() / 2) 和s.substr(s.size() / 2 , s.size()-1)都很正确。
还有,如果s为两位数,程序就会崩溃
插入排列:
插入排列从头开始以此插入前面的的有序队列,并依次进行排列。
堆排列:
堆排列分为大顶堆和 小顶堆,利用二叉树的性质,未排好序大顶堆的父节点都要比其两个孩子节点的值都要大,对用一个一维数组A来表示,i表示父节点,则A[i]>A[2i]&&A[i]>A[2i+1]。
那么如何进行堆排列呢?
首先将最顶端的元素放到未排好序元素的最后一个元素交换。
其次按照大顶堆的定义重新调整顺序
在一个数分成A,B后没有考虑到如果A*B等于0的时候
今天在做1116.Come on! Let‘s C,我设立了两个数组,rank用于记录名次,checked用于记录是否查询过,第一次写时,我把rank的每一个值用函数memset设置成了-1,如果rank[k]==-1说明没有排名,输出Are You kidding me ?但是第二个测试点过不去,看了别人的答案后索性把rank数组中的每一个值改为0,判断条件也改了,居然通过了,,,,,
Are you kidding me????!!!!
超时了,但好歹我调试了一段时间,可不能扔了。写下以纪念
`
#include
#include<string.h>
#include
using namespace std;
struct node {
char name[9];
int age, net_worth;
}P[100010];
bool cmp(node a, node b) {
return a.age < b.age;
}
bool cmp1(node a, node b) {
if (a.net_worth != b.net_worth)
return a.net_worth > b.net_worth;
if (a.age != b.age)
return a.age < b.age;
if (strcmp(a.name, b.name) < 0)
return true;
return false;
}
int main()
{
int N, K;
scanf("%d %d", &N, &K);
for (int i = 0; i < N; i++) {
scanf("%s %d %d", P[i].name, &P[i].age, &P[i].net_worth);
}
sort(P, P + N, cmp);
for (int i = 1; i <=K; i++) {
int M, Amin, Amax;
scanf("%d %d %d", &M, &Amin, &Amax);
int start = 0,end=N-1;
while (start<N&&P[start].age < Amin)
start++;
while (end>=0&&P[end].age > Amax)
end--;
printf("Case #%d:\n", i);
if (start > end)printf("None");
else {
sort(P + start, P + end+1, cmp1);
M = (M > end - start) ? end - start+1: M;
for (int j = start; j <start+M; j++) {
printf("%s %d %d\n", P[j].name, P[j].age, P[j].net_worth);
}
}
sort(P, P + N, cmp);
}
return 0;
}
`
测试点3过不去,输出格式不对,知道为什么吗?最后没回车
思路是这样的,
第一次循环输入,并找到当前节点前面的最大值。
第二次循环找到当前节点右边的最小值
第三次循环,输出主元
`#include
#include
#include
using namespace std;
int main()
{
int N,a[10010],LeftMax[100010],RightMin[100010];//LeftMax数组用于记录当前左边最大数,RightMin数组用于记录当前右边最小数
scanf("%d", &N);
int big=-1,small=9999999999;
for (int i = 1; i <=N; i++) {
scanf("%d", &a[i]);
if (i == 1) {
LeftMax[1] = a[1] - 1;
continue;
}
if (a[i] > big) {
LeftMax[i + 1] = a[i];
big = a[i];
}
else LeftMax[i + 1] = LeftMax[i];
}
RightMin[N] = a[N]+1;
for (int i = N ; i >= 1; i--) {
if (a[i] < small) {
RightMin[i - 1] = a[i];
small = a[i];
}
else RightMin[i - 1] = RightMin[i];
}
vector<int> v;
for (int i = 1; i <=N ; i++) {
if (a[i]<=RightMin[i]&&a[i]>=LeftMax[i] ) {
v.push_back(a[i]);
}
}
printf("%d\n",v.size());
sort(v.begin(), v.end());
int flag = 1;
for (int i = 0; i < v.size(); i++) {
if (flag) {
printf("%d", v[i]);
flag = 0;
}
else {
printf(" %d", v[i]);
}
}
printf("\n");
return 0;
}`
留意一下隔天打电话的情况
题目要求C=1,按学号递增排名,如果C=2,按姓名的非递增排名,如果C=3按成绩的非递归排名。
如果几个同学的姓名和成绩相同,必须按学号递增排名。
按照这个思路,cmp函数应该这样写
`
bool cmp(node a, node b) {
if (C == 1)return a.ID < b.ID;
else if (C == 2) {
if(strcmp(a.name, b.name))
return strcmp(a.name, b.name) < 0;
else return a.ID < b.ID;
}
else if (C == 3) {
if(a.Grade != b.Grade)
return a.Grade < b.Grade;
else return a.ID < b.ID;
}
}
而柳婼学姐这样写,
int cmp1(NODE a, NODE b) {
if(c == 1)
return a.no < b.no;
else if(c == 2)
return strcmp(a.name, b.name) <= 0;
else
return a.score <= b.score;
}
`
为什么呢??如果姓名一样返回真,没有按学号排序啊??
没有考虑几个成绩并列的的情况
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.