PAT乙级 部分题目解答(仅是记录自己的学习过程,有错误欢迎指正)

这篇博客记录了作者在PAT乙级考试中遇到的部分编程题目的解答,主要使用C/C++语言。博主强调了自主思考、调试以及借鉴他人解决方案的学习过程,并鼓励读者通过这种方式提升编程能力。

 (本次包括所有15分值的题目与个别20分的题目,仅为记录自己的代码方便翻看。当让如果能帮助到你最好了。)

刷题要先有自己的想法与实现,然后debug,还是有通不过的测试用例则可以查看其他同学的实现,是个很好的过程。

加油。

 

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;

int main(){
	char str[81];
	gets(str); //gets() 可以读取空格,但是不会接受换行符;
                   //getline()可以接受换行符,所以getline()之后还需要getchar()来吸收\n 
	int i = 0;
	for(i=strlen(str); i>0; --i){
		if(str[i] == ' '){
			printf("%s ",&str[i+1]);//读取i处空格后的整个单词字符串 
			str[i] = '\0';//讲空格处改为\0,这样之后的字符串输出才能在空格处结束 
		}
	} 
	printf("%s",&str[i]);//以字符串的格式输出,从str[i]的地址开始直到遇到\0 
	return 0;
}

 

#include<iostream>

using namespace std;

int main(){
	int M;
	cin >> M;
	int a;
	for(int i=0; i<M; ++i)
	{
		cin >> a;
		int flag;
		for(int N=1; N<10; ++N)
		{
			flag = 1;
			int rst = N * a * a;
			//cout << rst << endl;
			int t1 = rst;
			int t2 = a;
			while(t2){
				
				int r1 = t2%10;
				int r2 = t1%10;
				if(r1 != r2){
					flag = 0;
				}
				t2 /= 10;
				t1 /= 10; 
			}
			if(flag == 1){
				cout << N << " " << rst << endl;
				break;
			}
		}
		if(flag == 0) {
				cout << "No" << endl;
			}
	}
	return 0;
}

#include<iostream>

using namespace std;

int main(){
	int A,B;
	cin >> A >> B;
	int R = A * B;
	if(R==0){
		cout << "0";
	}
	int flag = 0; 
	int tmp = 0;
	while(R!=0)//容易忽略的情况,当第一个0出现时不输出,
                   //但是输出第一个不为零的数后,之后的零就必须输出 
	{
		tmp = R % 10;
		if((tmp != 0) && (flag == 0)){
			cout << tmp;
			flag = 1;
		}
		else if(flag == 1){
			cout << tmp;
		}
		R = R / 10;
	}
	cout << endl;
	return 0;
}

#include<iostream>
#include<string.h> 
#include<cctype>
//#include<string>

using namespace std;

/*分析:非空字符串,每个字符串以回车结束,但是
        字符串里面可能会有空格,所以不能直接用cin,
		要用getline接收一行字符。在接收完n后要getchar()
		读取一下换行符才能用getline,否则换行符会被读进getline中
*/
int main()
{
	int N;
	//string str[101];
	cin >> N;
	getchar();
	for(int i=0; i<N; ++i){
		string str;
		int flag_n = 0, flag_c = 0, flag_oth = 0;
		//cin >> str;
		//int len = 0;
		//const string temp = str[i];
		
		getline(cin,str);
		int len = str.length();
		if(len < 6){
			cout << "Your password is tai duan le." << endl;
			continue;
		}
		for(int j=0; j<len; ++j)
		{
			if(isdigit(str[j])){
				flag_n = 1;
			}
			else if((str[j]<='z') && (str[j]>='A')){
				flag_c = 1;
			}
			else if(str[j]!='.' && !isalnum(str[j])){
				flag_oth = 1;
			}
		}
		//cout << flag_n << flag_c << flag_oth << endl;
		if(flag_oth == 1){
			cout << "Your password is tai luan le." << endl;
		}
		else if(flag_n == 0){
			cout << "Your password needs shu zi." << endl;
		}
		else if(flag_c == 0){
			cout << "Your password needs zi mu." << endl;
		}
		else{
			cout << "Your password is wan mei." << endl;
		}
	} 
	return 0;
}

#include<iostream>
#include<string.h>
//#include<stdlib.h>
using namespace std;

int main(){
	int N;
	cin >> N;
	char a,b; 
	for(int i=0; i<N; ++i)
	{
		for(int j=0; j<4; ++j)
		{
			scanf("%c-%c ",&a,&b);
			if(a=='A' && b=='T'){
				cout << "1";
			}
			else if (a=='B' && b=='T'){
				cout << "2";
			}
			else if(a=='C' && b=='T'){
				cout << "3";
			}
			else if(a=='D' && b=='T'){
				cout << "4";
			}
			
		}
		
		
		
		/* 
		string str[4] = {0};
		
		for(int j=0; j<8; ++j)
		{
			cin >> str[j];
			if(strcmp("A-T",str[j])){
				cout << "1";
			}
			else if(strcmp("B-T",str[j])){
				cout << "2";
			}
			else if(strcmp("C-T",str[j])){
				cout << "3";
			}
			else if(strcmp("D-T",str[j])){
				cout << "4";
			}
		}*/ 
	/*	char str[15];
		cin >> str;
		printf("%c,%c,%c,%c",str[2],str[6],str[10],str[14]);
		//cout << str[2] << str[6] << str[10] << str[14] << endl;
		if(str[2] == 'T'){
			cout << "1";
		}
		else if(str[6] == 'T'){
			cout << "2";
		}
		else if(str[10] == 'T'){
			cout << "3";
		}
		else if(str[14] == 'T'){
			cout << "4"; 
		}*/
	}
	cout << endl;
	
	//system("pause");
	return 0;
}

#include<iostream>

using namespace std;

int main()
{
	int T,K;
	cin >> T >> K;
	//cout << K << endl;
	for(int i=0; i<K; ++i)
	{
		int n1,b,t,n2;
		cin >> n1 >> b >> t >> n2;
		if(T < t){
			cout << "Not enough tokens.  Total = " << T << "." << endl;
		}
		else if(b==1 && n2>n1){
			T += t;
			cout << "Win " << t << "!  " << "Total = " << T << "." << endl; 
		}
		else if(b==0 && n2<n1){
			T += t;
			cout << "Win " << t << "!  " << "Total = " << T << "." << endl; 
		}
		else{
		   	  T -= t;
			  if(T == 0){
			   cout << "Lose " << t << ".  " << "Total = " << T << "." << endl;	
			   cout << "Game Over." << endl; 
			   return 0;
			  }
			  else{
			  	cout << "Lose " << t << ".  " << "Total = " << T << "." << endl;
			  }
			
		}
	}
	return 0;
} 

#include<iostream>

using namespace std;

int main()
{
	int M,N,A,B,rp;
	int pht[501][501];
	cin >> M >> N >> A >> B >> rp;
	for(int i=0; i<M; ++i)
	{
		for(int j=0; j<N; ++j)
		{
			cin >> pht[i][j];
			if((pht[i][j]>=A) && (pht[i][j]<=B)){
				pht[i][j] = rp;
			}
		}
	}
	int i,j;
	for(i=0; i<M; ++i)
	{
		for (j=0; j<N-1; ++j){
			printf("%03d ",pht[i][j]);
		}
		printf("%03d\n",pht[i][j]);
	}
	return 0;
}

#include<iostream>

using namespace std;

int main(){
	
	int N,M;
	cin >> N >> M;
	int wth[101];
	int ans[101];
	int qust[101][101];
	for(int i=0; i<M; ++i)
	{
		cin >> wth[i];
	} 
	for(int i=0; i<M; ++i)
	{
		cin >> ans[i];
	} 
	for(int i=0; i<N; i++)
	{
		for(int j=0; j<M; ++j)
	    {
			cin >> qust[i][j];
  	   	}
		int sum = 0;
		for(int k=0; k<M; ++k){
			if(qust[i][k]==ans[k]){
				sum += wth[k];
			}
		} 
		cout << sum << endl;
	}
	return 0;
}

#include<iostream>

using namespace std;

int main(){
	int N;
	int a[10];
	cin >> N;
	for(int i=0; i<N; ++i)
	{
		cin >> a[i];
	}
	int sum = 0;
	for(int i=0; i<N; ++i)
	{
		for(int j=0; j<N; ++j)
		{
			if(i!=j){
				sum += ((a[i]*10)+a[j]);
			}
		}
	}
	cout << sum << endl;
	
	return 0;
}

#include<iostream>
#include<math.h>
using namespace std;

/* From 优快云 Hunter
 * 1. C语言有三角函数库,位于 <math.h>
 * 2. C语言的格式化输出保留小数,默认四舍五入,在接近0的负数四舍五入之后不输出0.00,而是-0.00。
 */

int main()
{
	double R1,P1,R2,P2;
	cin >> R1 >> P1 >> R2 >> P2;
	double A1,B1,A2,B2;
	A1 = R1 * cos(P1);
	B1 = R1 * sin(P1);
	A2 = R2 * cos(P2);
	B2 = R2 * sin(P2);
	double A = (A1*A2) - (B1*B2);
	double B = (B1*A2) + (A1*B2);
	if(A<0 && A>-0.005){
		A = 0.00;
	}
	if(B<0 && B>-0.005){
		B = 0.00;
	}
	
	printf("%.2f%+.2fi\n",A,B);
                // %+:有符号,值若>=0,在值前显示加号;若为负,则在值前显示负号
	
	/*if(B<0 && A!=0){
		B = -B;
		printf("%.2f-%.2fi\n",A,B);
	}
	else if(B<0 && A==0){
		B = -B;
		printf("-%.2fi\n",B);
	}
	else if(B==0){
		printf("%f.2\n",A);
	}
	else if (A==0 && B>0){
		printf("%f.2i\n",B);
	}
	else{
		printf("%f.2+%f.2i\n",A,B);
	}*/
	return 0;
} 

#include<iostream>

using namespace std;

int main(){
	
	int N;
	cin >> N;
	int A[101][4];
	int J=0, Y=0;
	for(int i=0; i<N; ++i)
	{
		cin >> A[i][0] >> A[i][1] >> A[i][2] >> A[i][3]; 
		int R = A[i][0] + A[i][2];
		if(R==A[i][1] && R!=A[i][3]){
			++Y;
		}
		else if(R!=A[i][1] && R==A[i][3]){
			++J;
		}
	}
	
	cout << J << " " << Y << endl;
	return 0;
}

#include<iostream>

using namespace std;

typedef struct student
{
	string num;
	int t;
	int r;
}student;

int main(){
	int N;
	cin >> N;
	student str[1001];
	for(int i=0; i<N; ++i)
	{
		cin >> str[i].num >> str[i].t >> str[i].r;
	}
	int M;
	int a[1001];
	cin >> M;
	for(int i=0; i<M; i++)
	{
		cin >> a[i];
	}
	for(int i=0; i<M; ++i)
	{
		for(int j=0; j<N; ++j)
		{
			if(a[i]==(str[j].t)){
				cout << str[j].num << " " << str[j].r<< endl;
			}
		}
	}
		
	return 0;
} 

#include<iostream>

using namespace std;

int main(){
	float n;
	char c;
	int h = 0;
	cin >> n >> c;
	if(n<3 || n>20){
		cout << "input error" << endl;
		return -1;
	}
	int t = (n / 2)*100;
	if((t%100)>=50){
		h = (int)(n/2)+1-2;
	}
	else{
		h = (int)n/2 - 2;
	}
	
	for(int i=0; i<n; ++i){
		cout << c;
	}
	cout << endl;
	for(int i=0; i<h; ++i){
		cout << c;
		for(int j=1; j<(n-1); ++j){
			cout << " ";
		}
		cout << c << endl;
	}
	for(int i=0; i<n; ++i){
		cout << c;
	}
	//cout << endl;
	return 0;
}

#include<iostream>

using namespace std;

int main(){
	int n;
	const int a[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
	const char M[]={'1','0','X','9','8','7','6','5','4','3','2'};
	cin >> n;
	int num = 0;
	for(int i=0; i<n; i++){
		char str[19];
		cin >> str;
		int sum = 0;
		int flag = 0;
		for(int j=0; j<17; ++j){
			int t = str[j]-'0';
			if(t>9 || t<0){
				flag = 1;
				num++;
				break;
			}
			else{
				sum += (t * a[j]);
			}
		}
		int Z = sum % 11;
		int temp = str[17]-'0';
		int comp = M[Z]-'0';
		if(flag ==1 || temp != comp){
			cout << str << endl;
			num++;
		}	
	}
	if(num==0){
		cout << "All passed" << endl;
	}
	return 0;
} 

 

#include<iostream>

using namespace std;

int main()
{
	int a,b,n;
	cin >> a >> b;
	int t = b-a;
	if(t<=0){
		cout << "error" << endl;
		return -1;
	}
	if(t%100 >= 50) 
 	 {
	   	n = t /100 + 1;
 	 }
	else {
		n = t / 100;
	}
	//cout << n << endl;
	int s = n % 60;
	//cout << s << endl;
	n = n / 60;
	int m = n % 60;
	n = n / 60;
	int h = n % 60;
	
	printf("%02d:%02d:%02d\n",h,m,s);
/*	if(h<10){
		cout << "0" << h << ":";
	}
	else{
		cout << h << ":";
	}
	if(m<10){
		cout << "0" << m << ":";
	}
	else{
		cout << m << ":";
	}
	if(s<10){
		cout << "0" << s << endl;
	}
	else{
		cout << s << endl;
	}
	*/
	return 0;
}

 

#include<iostream>
#include<string.h>

using namespace std;

int main()
{
	string n;
	cin >> n;
	int a[10];
	memset(a,0,sizeof(a));//把数组a清零 
	
	for(int i=0; i<n.length(); ++i)
	{
		a[n[i]-'0']++; 
	}
	
	for(int i=0; i<10; ++i)
	{
		if(a[i] != 0)
		{
			cout << i << ":" << a[i] << endl;
		}
	}
	return 0;
}

 

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
	int a,b,c,d;
	cin >> a >> b >> c >> d;
	int numa = 0, numc = 0;
	while(a!=0)
	{
		if(a %10 == b) numa++;
		a = a / 10;
	}
	while(c!=0)
	{
		if(c %10 == d) numc++;
		c = c / 10;
	}
	
	int sum_a = 0, sum_c = 0;
	for (int i=0; i<numa; i++)
	{
		sum_a = sum_a + b * pow(10,i);
	}
	for (int i=0; i<numc; i++)
	{
		sum_c = sum_c + d * pow(10,i);
	}
	
	cout << sum_a + sum_c << endl;
	return 0;
} 

#include<iostream>

using  namespace std;

int main()
{
	int T;
	cin >> T;
	for(int k=1; k<=T; k++)
	{
		long a,b,c;
		cin >> a >> b >> c;
		if(a+b>c)
		{
			cout << "Case #" << k << ": true" << endl;
		}
		else
		{
			cout << "Case #" << k << ": false" <<endl; 
		}
	}
	return 0;
}

 

#include<iostream>

using namespace std;

int main()
{
	int n;
	cin >> n;
	int a = n % 10;
	n = n / 10;
	int b = n %10;
	n = n / 10;
	int c = n % 10;
	for(int i=0; i<c; i++)
	{
		cout << "B" ;
	}
	for(int i=0; i<b; i++)
	{
		cout << "S" ;
	}
	for(int i=1; i<=a; i++)
	{
		cout << i ;
	}
	
	cout << endl;
	return 0;
} 

#include<iostream>

using namespace std;

typedef struct student{
	string name;
	string num;
	int score;
}student; 

int  main()
{
	
	int N;
	cin >> N;
	student S[N];
	int high = 0, low = 100;
	int flag_h = 0, flag_l = 0;
	for(int i=0; i<N; ++i)
	{
		cin >> S[i].name >> S[i].num >> S[i].score;
		if(S[i].score > high){
			high = S[i].score;
			flag_h = i;
		}
		if(S[i].score < low){
			low = S[i].score;
			flag_l = i;
		}
	}
	cout << S[flag_h].name << " " << S[flag_h].num << endl;
	cout << S[flag_l].name << " " << S[flag_l].num << endl; 
	return 0;
} 

#include<stdio.h>
#include<string.h>

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0; i<n; ++i)
    {
        char a[101];
		scanf("%s",a);
        int len = strlen(a);
        //int flag = 0;
        int loc_p =0, loc_t=0, nump=0, isRight=1, numt=0;
        int left=0, center=0, right=0;
        for(int j=0; j<len; ++j)
        {
            if(a[j]=='P')
            {
                loc_p = j;
                nump++;
            }
            else if(a[j]=='T')
            {
                loc_t = j;
                numt++;
            }
            else if(a[j] != 'A')
            {
                isRight = 0;
                break;
            }
        }
        if( nump==1 && numt==1 && isRight ){
			left = loc_p;
        	right = len - loc_t - 1;
        	center = loc_t - loc_p - 1;
       	 	if(left * center == right && center > 0){
         	    printf("YES\n");
         	    continue;
        	}
        }
        printf("NO\n");
    }
    return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
    char a[1000];
    gets(a);
    int sum = 0;
    int n = strlen(a);
    
    for(int i=0; i< n; ++i)
    {
    	a[i] = a[i] - 48;
    	sum += (a[i]%10);
    }
    
    //if(sum<0) return 0;
/*    
    while(n!=0)
    {
        sum += (n%10);
        n /= 10;
    }
    */
    
    int stack[10];
    int p = 0;
    
    while(sum!=0)
    {
        stack[p++] = sum%10;
        sum /= 10;
    }

    int num = 0;

    while(p!=0)
    {
        int k = stack[--p];
        if(num!=0) printf(" ");
        if(num==0) {
            if(k==0) break;
        }
        num++;
        switch(k)
        {
            case 0: printf("ling"); break;
            case 1: printf("yi"); break;
            case 2: printf("er"); break;
            case 3: printf("san"); break;
            case 4: printf("si"); break;
            case 5: printf("wu"); break;
            case 6: printf("liu"); break;
            case 7: printf("qi"); break;
            case 8: printf("ba"); break;
            case 9: printf("jiu"); break;
           // default: printf("error");
        }
    }
    printf("\n");
    return 0;
}

 

#include<stdio.h>

int main(){
    int n;
    scanf ("%d",&n);
    int num = 0;
    while(n!=1)
    {
        if(n%2==0)
        {
            n /= 2;
        }
        else{
            n = (3*n + 1)*0.5;
        }
        num++;
    }

    printf("%d\n",num);
    return 0;
}

 

#include<iostream>

using namespace std;

void sort(int a[], int n)
{
	int flag, tmp;
	for(int i=n-1; i>=1; --i){
		flag = 0;
		for(int j=1; j<=i; ++j){
			if(a[j-1] > a[j]){
				tmp = a[j];
				a[j] = a[j-1];
				a[j-1] = tmp;
				flag = 1;
			}
		}
        if(flag == 0){
				return;
			}
	}
}

int main(){
	int K;
	cin >> K;
	int C[101];
	for(int i=0; i<K; ++i){
		cin >> C[i];
	}
	for(int i=0; i<K; ++i)
	{
		int t = C[i];
		if(t == 0) continue;
		while(t > 1){
			if(t%2 == 0){
				t /= 2;
			}
			else{
				t = (3*t + 1)/2; 
			}
			for(int j=0; j<K; ++j){
				if(t == C[j]){
					C[j] = 0;
		   	        break;
				}    
			}
		}
	}
	sort(C,K);
	//for(int k=0; k<K; ++k)
	//{
	//	cout << C[k];
	//}
	//cout << endl;
	int flag = 1;
	for(int i=K-1; i>=0; --i)
	{
		if(C[i]!=0 && flag==1){
			cout << C[i];
			flag = 0;
		}
		else if(C[i]!=0 && flag==0){
			cout << " " << C[i];
		}
	}
	//cout << endl;
	return 0;
}

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值