1.题目链接https://blog.youkuaiyun.com/allisonshing/article/details/104213959
#include<cstdio>
#include<string>
#include<iostream>
#include<vector>
#include <algorithm>//fill()在里面
#include <cmath>
using namespace std;
//20春季PAT A1
const int INF=1e9;
const int maxn=110;
bool isPrime(int x){
//判断素数:记住
if(x==0||x==1) return false;
if(x==2)return true;
int y=(int)sqrt(x*1.0);//double sqrt(double);
for(int i=2;i<=y;i++){
if(x%i==0)return false;
}
return true;
}
int main(){
// int date;
// scanf("%d",&date);
//每次去掉开头的一个字母得到的子字符串也是素数
//将数字转换为int数组;或者输入的是string转换成num
string date;
cin>>date;
int num=stoi(date);
// cout<<num<<endl;
int len=date.length();
int flag=1;
for(int i=0;i<len;i++){
// cout<<date[0]<<endl;
string str=date.substr(i,len);
cout<<str;
int num=stoi(str);
// cout<<num;
if(isPrime(num)){
cout<<" Yes"<<endl;
}else{
cout<<" No"<<endl;
flag=0;//只要有一个不符合则是0
}
}
if(flag==1){
cout<<"All Prime!"<<endl;
}
return 0;
}
2.
#include<cstdio>
#include<string>
#include<iostream>
#include<vector>
#include <algorithm>//fill()在里面
#include <cmath>
#include<unordered_set> //注意加上头文件#include<unordered_set>
#include<set>
#include <cstdlib>//貌似是free,malloc等函数的头文件
using namespace std;
//20春季PAT A2
//给出n个player的m轮的数字,(下标1-n)
//从1th人开始给数字 ,淘汰后的选手在之后的游戏局中该选手给出的数字忽视
//输的人或者赢的人不止一个时,按照下标从小到大输出
//出局:输入重复的数字,或者不是正确的答案
const int INF=1e9;
const int maxn=110;
//s保存正确的数字,oid保存出局的选手
unordered_set<int>s,oid;//set自动排序比较耗时,unset由于无序所以查找的快
int data[13][105];
//是否正确
bool judge(int x){
//容器使用迭代器的方式进行循环
//迭代器循环变量申明
unordered_set<int>::iterator it=s.begin();//相当于指针
for(;it!=s.end();it++){
if(s.find(*it+x)!=s.end()){
//set的find方法:如果找不到则返回s.end
//将参数和s中存在的每个数相加之后的数再去s里查找。差值的查找基本都用这种思路
//不需要计算所有的正确值:当前的数字一定是当前所加入数字的差值:所以只要找到差值和其余的值相加= 其余值中的另一个就行
return true;//查重在main中做
}
}
return false;
}
int main(){
int a,b;
scanf("%d %d",&a,&b);
s.insert(a);
s.insert(b);
int n,m;
scanf("%d %d",&n,&m);//n:players;m:rounds
//n:[2,10] m:[2,103]
//存储初始数据使用二维数组?:数据都是正数
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&data[i][j]);//人的下标和比赛轮数的下标都从1开始
}
}
//每次对列处理 j=1=>j=m
for(int j=1;j<=m;j++){
for(int i=1;i<=n;i++){//已经按下标的顺序输出了
if(oid.find(i)!=oid.end())continue;
int x=data[i][j];
if(s.find(x)!=s.end()||!judge(x)){
//如果重复/或者不是当前数字差值--->该玩家出局
oid.insert(i);
printf("Round #%d: %d is out\n",j,i);
}else{
s.insert(x);
}
}
}
//计算赢家 :只需要查找oid里没有的玩家
bool flag=false;//没有赢家
for(int i=1;i<=n;i++){
if(oid.find(i)==oid.end()){
//没找到
if(!flag){
//第一次找到的时候flag还是false
printf("Winner(s): %d",i);
}else{
printf(" %d",i);
}
flag=true;
}
}
if(!flag)printf("No winner.");
return 0;
}
3.A3(过了测试用例,没测试,考试的时候ac过了但是不知道在官网上的哪里找我的代码。。。)
方法一:
#include<cstdio>
#include<string>
#include<iostream>
#include<vector>
#include <algorithm>//fill()在里面
#include <cmath>
#include<unordered_set>
#include<set>
#include <cstdlib>//貌似是free,malloc等函数的头文件
using namespace std;
//20春季PAT A3
//动物园有k种动物:分到n个区域:相邻的两个区域不能有相同的动物
//任务:辨别给出的分配方案是否可行
//输入:n(节点数:区域数(0,500]),r(边数[0+&)),k(动物种类) :n和k从下标1开始
//分析:无向图
const int INF=1e9; //fill(map[0],map[0]+maxn*maxn,INF);
const int maxn=510;
int map[maxn][maxn];//存储边关系:右边则是1,无边是0
int plan[maxn];
set<int> visit;//保存已经输入过的种类数:利用去重的性质
int main(){
int n,r,k;
scanf("%d%d%d",&n,&r,&k);
int begin,end;
while(r--){
scanf("%d%d",&begin,&end);
map[begin][end]=1;
map[end][begin]=1;
}
int m;
scanf("%d",&m);
while(m--){
//m个计划:每个计划给出种类的n个索引
int count=0;//对种类的计数
for(int i=1;i<=n;i++){
//下标从1开始
scanf("%d",&plan[i]);
visit.insert(plan[i]);//插入物种
}
count=visit.size();
if(count<k){
printf("Error: Too few species.\n");
}else if(count>k){
printf("Error: Too many species.\n");
}else{
//继续判断相邻的区域是否有相同的动物
//遍历map?:可以在遍历的时候减少遍历数
int flag=1;//为了防止打印no多次
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(map[i][j]==1){
//存在边的时候判断两个区域的动物
if(plan[i]==plan[j]){
flag=0;//说明不符合要输出No
}
}
}
}
if(flag==1){
printf("Yes \n");
}else{
printf("No\n");
}
}
//清空visit
visit.clear();
}
return 0;
}
方法二:
#include<cstdio>
#include<string>
#include<iostream>
#include<vector>
#include <algorithm>//fill()在里面
#include <cmath>
#include<unordered_set>
#include<set>
#include <cstdlib>//貌似是free,malloc等函数的头文件
using namespace std;
//20春季PAT A3
int plan[maxn];
set<int> visit;//保存已经输入过的种类数:利用去重的性质
struct node{
int a,b;//头尾节点
}temp;
vector<node> map;
int main(){
int n,r,k;
scanf("%d%d%d",&n,&r,&k);
while(r--){
scanf("%d%d",&temp.a,&temp.b);
// map.insert(temp);//vector的插入是push_back
map.push_back(temp);
}
int m;
scanf("%d",&m);
while(m--){
//m个计划:每个计划给出种类的n个索引
int count=0;//对种类的计数
for(int i=1;i<=n;i++){
//下标从1开始:没用变长数组:用变长数组也行 vector<int> plan(n+1);
//新的C标准好像可以用变量初始化数组长度
scanf("%d",&plan[i]);
visit.insert(plan[i]);//插入物种
}
count=visit.size();
if(count<k){
printf("Error: Too few species.\n");
}else if(count>k){
printf("Error: Too many species.\n");
}else{
//可以直接在最开始使用结构只存储边,这样能减少遍历的时间以及二维数组的空间复杂度
int len=map.size();
int flag=1;
for(int i=0;i<len;i++){
int x=map[i].a;
int y=map[i].b;
if(plan[x]==plan[y]){
flag=0;
break;
}
}
if(flag==1){
printf("Yes \n");
}else{
printf("No\n");
}
}
//清空visit
visit.clear();
}
return 0;
}
4.A4
#include<cstdio>
#include<string>
#include<iostream>
#include<vector>
#include <algorithm>//fill()在里面
#include <cmath>
#include<queue>
#include<unordered_set>
#include<set>
#include <cstdlib>//貌似是free,malloc等函数的头文件
using namespace std;
//20春季PAT A4 (模拟题:挺难的)
//输入数据太大-->外部排序?置换算法 :使用有限的内存产生一系列的有序记录(每一次记录的大小就是内存的大小)
//最简单的策略:每次读取尽可能多的记录入内存:将这一部分排序,再将排序后的结果写回磁带里
//排序采用升序:内存中只要输出了一个数字,立马可以再输进来一个数字
//如果下一个数字比输出的数字大,则可包含在当前的run里,一遇到比输出数字小的数字则进入下一个run
const int INF=1e9; //fill(map[0],map[0]+maxn*maxn,INF);
const int maxn=510;
int main(){
//思路:使用队列:考试的时候很无语的忘记队列的用法了
int n,m;
scanf("%d%d",&n,&m);
//n其实还蛮大的,所以直接开数组不太合适,优先考虑用vector
vector<int> data;
int temp;
int n1=n;
while(n1--){//这里n变化了
scanf("%d",&temp);
data.push_back(temp);
}
// cout<<data.size()<<endl;data如果设定了长度再往里面填东西好会出问题
//优先队列的用法!!
priority_queue<int,vector<int>,greater<int> > q;//模拟内存的排序
//greater表示升序排序less表示降序
//priority_queue< type数据类型, container实现优先队列的底层容器, function >
//分析:要用多少个vector来存放run?run事先不知道
//设两个vector :一个当前的,一个是保存下一个run的
vector<int> current,next;
int index=0,count=0;
//初始数据输入
for(;index<m;index++)q.push(data[index]);//为什么没有函数提示
while(count!=n){
//count计算output的数 ,每次处理一个数
int top=q.top();//队列的顶部元素:优先队列已经实现升序排序
// cout<<"top="<<top<<endl;
current.push_back(top);
q.pop();//删除队首元素
count++;//输出+1
// cout<<"index="<<index<<"n="<<n<<endl;
if(index<n){//这时如果data中还有数据,继续输入下一个数字
//下一个输入的数字和当前输出的数字比较
if(data[index]>=top){//不小于
q.push(data[index++]);
}else{
//放到下一个run里
next.push_back(data[index++]);
}
}
//如果本轮的数处理结束:q为空
if(q.empty()){
//输出本轮的run:然后再清空current里面的数字来存储下一轮的输出
for(int i=0;i<current.size();i++){
//输出格式要求:
if(i!=0)printf(" ");
printf("%d",current[i]);
}
printf("\n");
//current清空+将下一轮暂存的数据放入q中排序+next清空
current.clear();
for(int i=0;i<next.size();i++){
//下一轮的全部放进去??因为下一轮的next得存下一个数据
q.push(next[i]);
}
next.clear();
}
}
return 0;
}
博客提及题目链接,还讲述了A3过了测试用例,考试时ac但不知官网何处找代码,同时列出A3的两种方法,还提到了A4。
767

被折叠的 条评论
为什么被折叠?



