PTA 实验春.docx

上传人:b****5 文档编号:6529071 上传时间:2023-01-07 格式:DOCX 页数:45 大小:354.90KB
下载 相关 举报
PTA 实验春.docx_第1页
第1页 / 共45页
PTA 实验春.docx_第2页
第2页 / 共45页
PTA 实验春.docx_第3页
第3页 / 共45页
PTA 实验春.docx_第4页
第4页 / 共45页
PTA 实验春.docx_第5页
第5页 / 共45页
点击查看更多>>
下载资源
资源描述

PTA 实验春.docx

《PTA 实验春.docx》由会员分享,可在线阅读,更多相关《PTA 实验春.docx(45页珍藏版)》请在冰豆网上搜索。

PTA 实验春.docx

PTA实验春

A.装箱问题模拟(20)2

B.表达式转换(25)7

C.家谱处理(30)19

D.航空公司VIP客户查询(25)23

E.社交网络图中结点的“重要性”计算(30)25

F.奥运排行榜(25)29

 

A.装箱问题模拟(20)

时间限制

400ms

内存限制

65536kB

代码长度限制

8000B

判题程序

Standard

假设有N项物品,大小分别为s1,s2,…,si,…,sN,其中si为满足1<=si<=100的整数。

要把这些物品装入到容量为100的一批箱子(序号1~N)中。

装箱方法是:

对每项物品,顺序扫描箱子,把该物品放入足以能够容下它的第一个箱子中。

请写一个程序模拟这种装箱过程,并输出每个物品所在的箱子序号,以及放置全部物品所需的箱子数目。

输入格式说明:

输入第1行给出物品个数N(<=1000),第2行给出N个正整数si(1<=si<=100,表示第i项物品的大小)。

输出格式说明:

按照输入顺序输出每个物品的大小及其所在的箱子序号,每个物品占1行,最后一行输出所需的箱子数目。

样例输入与输出:

序号

输入

输出

1

86070809030401020

6017028039043014051012025

2

61009080706050

10019028037046055066

3

12

211

#include

usingnamespacestd;

intmain(){

intN;

cin>>N;

int*No=newint[N];//存放N个物品各自的大小

int*Box=newint[N];//存放箱子的剩余容积,初始100

for(inti=0;i

{

inttemp1;

cin>>temp1;

*(No+i)=temp1;

*(Box+i)=100;

}

intboxno=0;

for(intii=0;ii

{

for(intj=0;j

{

if(*(Box+j)-*(No+ii)>=0)//判断当前箱子(剩余的容积)是否能够存放第ii+1个物品

{

cout<<*(No+ii)<<""<

if(boxno<(j+1))boxno=j+1;//返回最终用到的箱子序号,即需要的箱子个数。

*(Box+j)=*(Box+j)-*(No+ii);//记下当前箱子存放完毕后剩余的容积

break;//跳出对当前箱子的循环检查,继续下一个物品

}

//不能存放则检查下一个箱子。

}

}

cout<

delete[]No;

delete[]Box;

return0;

}

B.表达式转换(25)

时间限制

400ms

内存限制

65536kB

代码长度限制

8000B

判题程序

Standard

算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。

日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。

请设计程序将中缀表达式转换为后缀表达式。

输入格式说明:

输入在一行中给出不含空格的中缀表达式,可包含+、-、*、\以及左右括号(),表达式不超过20个字符。

输出格式说明:

在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。

样例输入与输出:

序号

输入

输出

1

2+3*(7-4)+8/4

2374-*+84/+

2

((2+3)*4-(8+2))/5

23+4*82+-5/

3

1314+25.5*12

131425.512*+

4

-2*(+3)

-23*

5

123

123

#include

#include

#include

#include

usingnamespacestd;

boolisOper(charc)

//判断是否为操作符

{

if((c=='+')||(c=='-')||(c=='*')||(c=='/')||(c=='(')||(c==')'))

returntrue;

returnfalse;

}

boolisHigh(chartop_op,charInfixExp_op)

//判断操作符的优先级

//top_op为栈顶操作符

//InfixExp_op为当前读入操作符

//如果栈顶操作符优先级高,则弹出栈顶操作符

//如果栈顶操作符优先级低,则压入当前读入操作符

{

if((top_op=='+')&&(InfixExp_op=='+'))returntrue;

if((top_op=='+')&&(InfixExp_op=='-'))returntrue;

if((top_op=='-')&&(InfixExp_op=='+'))returntrue;

if((top_op=='-')&&(InfixExp_op=='-'))returntrue;

if((top_op=='*')&&(InfixExp_op=='+'))returntrue;

if((top_op=='*')&&(InfixExp_op=='-'))returntrue;

if((top_op=='*')&&(InfixExp_op=='*'))returntrue;

if((top_op=='*')&&(InfixExp_op=='/'))returntrue;

if((top_op=='/')&&(InfixExp_op=='+'))returntrue;

if((top_op=='/')&&(InfixExp_op=='-'))returntrue;

if((top_op=='/')&&(InfixExp_op=='*'))returntrue;

if((top_op=='/')&&(InfixExp_op=='/'))returntrue;

if(InfixExp_op==')')returntrue;

returnfalse;

}

voidinput(vector*InfixExp)

{

charc;

cin>>c;

while(c!

='\n')//判断回车结束输入

{

InfixExp->push_back(c);

cin.get(c);

}

}

//voidoutput(vector*postfixExp)//输出原表达式

//{

//vector:

:

iteratorpostfixExp_it;//后缀表达式迭代器

//for(postfixExp_it=postfixExp->begin();postfixExp_it!

=postfixExp->end();postfixExp_it++)

//{

//if(isOper(*postfixExp_it))cout<<""<<*postfixExp_it<<"";

//elsecout<<*postfixExp_it;

//}

//cout<

//}

voidoutput2(vector*postfixExp)

//不输出括号

//如果表达式中括号不配对

//则可能有多余的括号未弹出

{

vector:

:

iteratorpostfixExp_it;//后缀表达式迭代器

for(postfixExp_it=postfixExp->begin();postfixExp_it!

=postfixExp->end();postfixExp_it++)

{

if((*postfixExp_it!

='(')&&(*postfixExp_it!

=')'))

if(isOper(*postfixExp_it))cout<<""<<*postfixExp_it<<"";

elsecout<<*postfixExp_it;

}

cout<

}

intmain()

{

stackmystack;

vectorInfixExp;//中缀表达式

vector:

:

iteratorInfixExp_it;//中缀表达式迭代器

vectorpostfixExp;//后缀表达式

//do{

//cout<<"请输入一个表达式,回车结束输入。

"<

cout<

input(&InfixExp);

//output(&InfixExp);//输入表达式

for(InfixExp_it=InfixExp.begin();InfixExp_it!

=InfixExp.end();InfixExp_it++)

{

if(!

isOper(*InfixExp_it))

//操作数,直接输出

postfixExp.push_back(*InfixExp_it);

else

//操作符

{

postfixExp.push_back('');//区分数字用的空格

if(mystack.empty())

//栈为空,压入操作符

mystack.push(*InfixExp_it);

elseif(isHigh(mystack.top(),*InfixExp_it))

//栈顶操作符优先,比如栈顶为*,当前操作符为+,则弹出*

{

if(*InfixExp_it!

=')')

//非闭括号

//弹出栈中操作符直到栈顶操作数优先级低于当前读入操作数

//压入当前读入操作符

{

do

{

postfixExp.push_back(mystack.top());

mystack.pop();

}while((!

mystack.empty())&&(isHigh(mystack.top(),*InfixExp_it)));

mystack.push(*InfixExp_it);

}

else

//闭括号

{

while((!

mystack.empty())&&(mystack.top()!

='('))

//弹出直到开括号

{

postfixExp.push_back(mystack.top());

mystack.pop();

}

if((!

mystack.empty())&&(mystack.top()=='('))

mystack.pop();

//弹出开括号

}

}

elseif(!

isHigh(mystack.top(),*InfixExp_it))

//中缀表达式中操作符优先

//比如栈顶为+,而当前读入*

{

mystack.push(*InfixExp_it);

//压入当前读入操作符

}

}

}

while(!

mystack.empty())

//把栈中剩余的操作符依次弹出

{

postfixExp.push_back(mystack.top());

mystack.pop();

}

output2(&postfixExp);

while(!

mystack.empty())

mystack.pop();

InfixExp.clear();

postfixExp.clear();

//清空栈、中缀表达式和后缀表达式

//}while(true);

return0;

}

带符号的数字运算有误噢。

另:

本地测试能过,PAT单机版也能过,网络版里通不过。

修正版,能通过PAT,测试可用。

PS:

这个是网上下载的,,下载地址附上

 

#include

#include

#include

usingnamespacestd;

stacks;

intJudge(chara)

{

if((a=='+')||(a=='-')||(a=='*')||(a=='/')||(a=='(')||(a==')'))return1;

return0;

}

intJudge_(charb,chara)

{

if(b=='+'||b=='-')

if((a=='+')||(a=='-')||(a=='*')||(a=='/')||(a=='('))return1;

return0;

}

intJudge_jjcc(chara)

{

if((a=='+')||(a=='-')||(a=='*')||(a=='/'))return1;

return0;

}

intJudge_jj(chara)

{

if((a=='+')||(a=='-'))return1;

return0;

}

intpremer(chara,charb)

{

switch(a)

{

case'+':

{

switch(b)

{

case'+':

return0;break;

case'-':

return0;break;

case'*':

return0;break;

case'/':

return0;break;

case'(':

return0;break;

}

}break;

case'-':

{

switch(b)

{

case'+':

return0;break;

case'-':

return0;break;

case'*':

return0;break;

case'/':

return0;break;

case'(':

return0;break;

}

}break;

case'*':

{

switch(b)

{

case'+':

return1;break;

case'-':

return1;break;

case'*':

return0;break;

case'/':

return0;break;

case'(':

return0;break;

}

}break;

case'/':

{

switch(b)

{

case'+':

return1;break;

case'-':

return1;break;

case'*':

return0;break;

case'/':

return0;break;

case'(':

return0;break;

}

}break;

case'(':

{

switch(b)

{

case'+':

return0;break;

case'-':

return0;break;

case'*':

return0;break;

case'/':

return0;break;

case'(':

return0;break;

}

}break;

default:

break;

}

}

intmain()

{

chara[30];

scanf("%s",a);

intlen=strlen(a);

intcon_flag=0;

intop_flag=1;

boolfirst=false;

for(inti=0;i

{

if(Judge(a[i])&&(!

(i==0&&Judge_jj(a[i])))&&(!

Judge_(a[i],a[i-1])))//正常符号

{

if(a[i]==')')

{

while(s.top()!

='(')

{

chartemp=s.top();

s.pop();

cout<<""<

}

s.pop();

while(!

s.empty()&&s.top()!

='(')

{

chartemp_t=s.top();

s.pop();

cout<<""<

}

}

elseif(!

s.empty()&&(a[i]!

='('))

{

if(premer(s.top(),a[i]))con_flag=1;

elsecon_flag=0;

while(con_flag)//压入的操作符优先级要大于顶部元素

{

chartemp2=s.top();

cout<<""<

s.pop();

if(!

s.empty())

{

if(premer(s.top(),a[i]))con_flag=1;

elsecon_flag=0;

}

elsecon_flag=0;

}

s.push(a[i]);

}

elses.push(a[i]);

if(first==false&&Judge_jjcc(a[i]))

{

first=true;cout<<"";

}

 

}

elseif(a[i]=='+'||a[i]=='-')//表示数据的正负

{

if(a[i]=='-')op_flag*=-1;//是-号后面的数字乘以-1;

//否则忽略

first=true;

}

else

{if(i==0)

{

cout<

}

else

{

if(op_flag==-1){

cout<<'-'<

op_flag=1;

}

elsecout<

first=false;//遇到数字

}

}

}

while(!

s.empty())

{

cout<<""<

s.pop();

}

//printf("%s",a);

system("pause");

return0;

}

另外再提供一个,未经测试的,代码就不贴了,自己下载测试。

C.家谱处理(30)

时间限制

400ms

内存限制

65536kB

代码长度限制

8000B

判题程序

Standard

人类学研究对于家族很感兴趣,于是研究人员搜集了一些家族的家谱进行研究。

实验中,使用计算机处理家谱。

为了实现这个目的,研究人员将家谱转换为文本文件。

下面为家谱文本文件的实例:

JohnRobertFrankAndrewNancyDavid

家谱文本文件中,每一行包含一个人的名字。

第一行中的名字是这个家族最早的祖先。

家谱仅包含最早祖先的后代,而他们的丈夫或妻子不出现在家谱中。

每个人的子女比父母多缩进2个空格。

以上述家谱文本文件为例,John这个家族最早的祖先,他有两个子女Robert和Nancy,Robert有两个子女Frank和Andrew,Nancy只有一个子女David。

在实验中,研究人员还收集了家庭文件,并提取了家谱中有关两个人关系的陈述语句。

下面为家谱中关系的陈述语句实例:

JohnistheparentofRobertRobertisasiblingofNancyDavidisadescendantofRobert

研究人员需要判断每个陈述语句是真还是假,请编写程序帮助研究人员判断。

输入格式说明:

输入首先给出2个正整数N(2<=N<=100)和M(<=100),其中N为家谱中名字的数量,M为家谱中陈述语句的数量,输入的每行不超过70个字符。

名字的字符串由不超过10个英文字母组成。

在家谱中的第一行给出的名字前没有缩进空格。

家谱中的其他名字至少缩进2个空格,即他们是家谱中最早祖先(第一行给出的名字)的后代,且如果家谱中一个名字前缩进k个空格,则下一行中名字至多缩进k+2个空格。

在一个家谱中同样的名字不会出现两次,且家谱中没有出现的名字不会出现在陈述语句中。

每句陈述语句格式如下,其中X和Y为家谱中的不同名字:

XisachildofYXistheparentofYXisasiblingofYXisadescendantofYXisanancestorofY

输出格式说明:

对于测试用例中的每句陈述语句,在一行中输出True,如果陈述为真,或False,如果陈述为假。

样例输入与输出:

序号

输入

输出

1

65JohnRobertFrankAndrewNancyDavidRobertisachildofJohnRobertisanancestorofAndrewRobertisasiblingofNancyNancyistheparentofFrankJohnisadescendantofAndrew

TrueTrueTrueFalseFalse

2

21abcabcdefghijabcdefghijisachildofabc

True

3

45ABCDAisachildofDDistheparentofCCisasiblingofBDisadescendantofBBisanancestorofC

FalseFalseFalseTrueTrue

#家谱处理

PAT4-05.家谱处理-1

#include

#include

#include

#include

#include

usingnamespacestd;

structnode

{

stringname;

intlevel,pre;

node(){}

node(strings,inta,intb):

pre(b),level(a),name(s){}

};

deque>tr(101);

unordered_map

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1