刷题链接:https://www.patest.cn/contests/pat-a-practise
1001
#include <iostream>
#include <stdio.h> using namespace std;
int temp[];
int main()
{
int a,b,sum,index;
while(scanf("%d%d",&a,&b)!=EOF){
sum=a+b;
if(sum<){
printf("-");
sum=-sum;
}
index=; while(sum>=){
temp[index++]=sum%;
sum=sum/;
}
printf("%d",sum);
for(int i = index-;i >= ;i--){
if(temp[i]>)
printf(",%d",temp[i]);
else if(temp[i]>)
printf(",0%d",temp[i]);
else
printf(",00%d",temp[i]);
}
printf("\n");
}
return ;
}
1001
数据结构初级入门第一题:https://pta.patest.cn/pta/test/17/exam/4/question/260
#include <iostream>
#include <stdio.h> using namespace std; int main()
{
int a,h,ans,i,j,temp;char c;
while(scanf("%d %c",&a,&c)!=EOF){
for(h=;a+>=*h*h;h++);
if(a==)h=,ans=;
else ans=a-*(h-)*(h-)+;
h=(h-)*+;
//printf("h is %d and ans is %d\n",h,ans);
//下面开始输出
temp=h;
for(i=;i<h;i++){
if(i<(h+)/){
for(j=;j<i;j++)printf(" ");
for(j=;j<h-i*;j++)printf("%c",c);
printf("\n");
}else{
for(j=;j<h-i-;j++)printf(" ");
for(j=;j<*i+-h;j++)printf("%c",c);
printf("\n");
}
}
printf("%d\n",ans);
}
return ;
}
打印沙漏
第二题:思路是素数打表法,请注意做表的时候请将空间开到足够大,否则会出现越界错误。同时相对ACM来说PAT的OJ是有bug的,这也就是说明不管题目你是否会写,请得分,很简单的,至少都有分数的,加油。
#include <iostream>
#include <stdio.h>
using namespace std; #define NMAX 100000
int num[NMAX];
int main()
{
int i,j;
//思路:采用素数打表法对素数进行处理
for(i=;i < NMAX-;i++){
num[i]=;
}
for(j=;j < NMAX-;j++){
if(num[j]==)for(int k =*j;k<NMAX-;k=k+j)num[k]=;
}
//上边首先是对表进行初始化 int n,count;
while(scanf("%d",&n)!=EOF){
count=;
for(i=;i<n-;i++){
if(num[i]==&&num[i+]==)count++;
}
printf("%d\n",count);
} return ;
}
第三题:佷常见的一个题目,但是要考虑到特殊的数据集的时候,你会发现你以前不严谨的代码错误百出。还好是PAT对于结果会给你一定的提升,请考虑提高数据集的复杂性,将问题考虑全面
#include <iostream>
#include <stdio.h> using namespace std;
int num[]; void reserve(int *,int,int); int main()
{
int i,n,offset;
while(scanf("%d%d",&n,&offset)!=EOF){
if(n==)continue;
offset%=n;
for(i=;i<n;i++)scanf("%d",&num[i]);
reserve(num,,n-offset-);
reserve(num,n-offset,n-);
reserve(num,,n-); for(i=;i<n;i++)
if(i!=n-)
printf("%d ",num[i]);
else
printf("%d",num[i]);
printf("\n");
}
return ;
} void reserve(int *a,int b,int c){
int temp;
if(b>=c)return;
for(int i=b;i <= b+(c-b)/;i++){
temp=a[i];
a[i]=a[c-(i-b)];
a[c-(i-b)]=temp;
}
}
第四题:这个题真的是靠着PAT的提示给整出来的,这个编译器的是好到爆,希望考试要有这样一个效果就好了
这个题目要特别注意!!!进位问题
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
#define NMAX 100
char num1[NMAX],num2[NMAX];
int demo1[],demo2[]; int main()
{
int i,j,add,count;bool flag;
while(scanf("%s",num1)!=EOF){
flag=true;
for(i=;i<;i++)demo1[i]=demo2[i]=;
for(i=;i<strlen(num1);i++)demo1[num1[i]-'']++; add=count=;
for(i=strlen(num1)-;i>=;i--){
j=num1[i]-'';
j*=;
num2[count++]=(j+add)%+'';
add=(j+add)/;
}
if(add!=)num2[count++]=add%+'';
num2[count]='\0';
for(i=;i<strlen(num2);i++)demo2[num2[i]-'']++; for(i=;i<;i++){
if(demo1[i]!=demo2[i]){
flag=false;
break;
}
}
if(flag)printf("Yes\n");
else printf("No\n"); for(i=count-;i>=;i--)printf("%c",num2[i]);
printf("\n"); }
return ;
}
第五题:终于是通关了,这个题目用的最笨的一种方法搞定的。同时也是因为自己没有理解题意,数据量太大,没法调试,卡壳了好久。参考了网上的代码后修改自己的把题目弄出来了。也是,代码这种东西满分不易,且行且珍惜。
#include <iostream>
#include <stdio.h> using namespace std;
#define MAX 60
#define COUNT 54
int place[MAX];
int temp_num[MAX];
int place_num[MAX]; int main()
{
int n,temp;
char* ans[]={
"TEMP","S1","S2","S3","S4","S5","S6","S7","S8","S9","S10","S11","S12","S13",
"H1","H2","H3","H4","H5","H6","H7","H8","H9","H10","H11","H12","H13",
"C1","C2","C3","C4","C5","C6","C7","C8","C9","C10","C11","C12","C13",
"D1","D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13",
"J1","J2"};
while(scanf("%d",&n)!=EOF){
for(int i =;i <= COUNT;i++){
scanf("%d",&temp_num[i]);
place[i]=place_num[i]=i;
} for(int j=;j <= n;j++){ for(int t=;t<=COUNT;t++){
place_num[temp_num[t]]=place[t];
}
for(int t=;t<=COUNT;t++){
place[t]=place_num[t];
}
} //最后是输出结果 for(int i = ;i<=COUNT;i++)
if(i!=COUNT)printf("%s ",ans[place_num[i]]);
else printf("%s",ans[place_num[i]]);
printf("\n");
} return ;
}
参考代码:
参考代码
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <iostream>
using namespace std;
char f[]={'S','H','C','D','J'};
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int time;
scanf("%d",&time);
int card1[],card2[],i,j;
for(i=;i<=;i++){
card1[i]=i;
}
int order[];
for(i=;i<=;i++){
scanf("%d",&order[i]);
}
for(i=;i<=time;i++){
for(j=;j<=;j++){
card2[order[j]]=card1[j];
}
for(j=;j<=;j++){
card1[j]=card2[j];
}
}
printf("%c%d",f[(card1[]-)/],card1[]%==?:card1[]%);
for(i=;i<=;i++){
printf(" %c%d",f[(card1[i]-)/],card1[i]%==?:card1[i]%);
}
printf("\n");
return ;
}
牛客网二叉搜索树:https://www.nowcoder.com/practice/3d6dd9a58d5246f29f71683346bb8f1b?tpId=40&tqId=21455&tPage=7&rp=7&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking
参考博客:http://www.cnblogs.com/rickhsg/p/3650875.html
自己的题解:
#include <stdio.h>
#include <string>
#include <string.h>
#include <stdlib.h> typedef struct Node{
int key;
struct Node *left;
struct Node *right;
}Node;
///申请一个树的根节点,并将其初始设置为空
Node * Tree=NULL,* p,* temp_p;
char tempa[],tempb[],temp_str[]; int length,i;
///先将对应的二叉树构造出来
void Creat(char* temp){
for(i=;i<strlen(temp);i++){
p=(Node *)malloc(sizeof(Node));
p->key=temp[i]-'';
p->left=p->right=NULL; if(Tree==NULL){
Tree=p;
}else{
temp_p=Tree;
while(){
if(temp_p->key==p->key)break;
else if(temp_p->key<p->key){
if(temp_p->right==NULL){
temp_p->right=p;
break;
}else
temp_p=temp_p->right;
}else
if(temp_p->key>p->key){
if(temp_p->left==NULL){
temp_p->left=p;
break;
}else
temp_p=temp_p->left;
}
}
}
}
} void pre_order(Node* Tree){
///使用前应当将长度设置为0初始化
if(Tree==NULL)return;
else{
temp_str[length++]=(char)(Tree->key+'');
//printf("+%c",temp_str[length-1]);
pre_order(Tree->left);
pre_order(Tree->right);
}
}
int main()
{
int T;
while(scanf("%d",&T)!=EOF){
if(==T)break;
scanf("%s",tempa);
Tree=NULL;
///先将对应的二叉树构造出来
Creat(tempa); ///对构造好的排序二叉树进行前序遍历进行唯一性标识
length=;
pre_order(Tree);
strcpy(tempa,temp_str); ///上边已经将二叉树构造好了,现在只要进行一个判断的工作就ok
while(T--){
scanf("%s",tempb);
Tree=NULL; ///先将对应的二叉树构造出来
Creat(tempb); ///对构造好的排序二叉树进行前序遍历进行唯一性标识
length=;
pre_order(Tree);
strcpy(tempb,temp_str); if(strcmp(tempa,tempb))printf("NO\n");
else printf("YES\n");
}
}
return ;
}
题解思路:https://www.nowcoder.com/questionTerminal/3d6dd9a58d5246f29f71683346bb8f1b