C语言编程PAT乙级学习笔记示例分享

2022-11-13 08:11:12 示例 学习笔记 乙级

1001 害死人不偿命的(3n+1)猜想

#include<iOStream>
#include<stack>
using namespace std;
int main()
{
	int n,count=0;
	cin>>n;
	while(n!=1){
		if(n%2==0)n=n/2;
		else n=(3*n+1)/2;
		count++;
	}
	cout<<count;
	system("pause");
	return 0;
}

1002 写出这个数

#include<iostream>
#include<stack>
using namespace std;
int main()
{
	string str;
	cin>>str;
	int sum=0;
	bool start =true;
	for(int i=0;i<str.length();i++){
		sum+=(str[i]-'0');
	}
	stack<int> sta;
	while(sum!=0){
		sta.push(sum%10);
		sum=sum/10;
	}
	while(!sta.empty()){
		if(start)start=false;
		else cout<<" ";
		int tmp=sta.top();
		sta.pop();
		switch(tmp){
			case 0:cout<<"ling";break;
			case 1:cout<<"yi";break;
			case 2:cout<<"er";break;
			case 3:cout<<"san";break;
			case 4:cout<<"si";break;
			case 5:cout<<"wu";break;
			case 6:cout<<"liu";break;
			case 7:cout<<"qi";break;
			case 8:cout<<"ba";break;
			case 9:cout<<"jiu";break;
		}
	}
	system("pause");
	return 0;
}

1003 我要通过!

//左边a的个数*中间=右边
#include<iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	while(n--){
		string c;
		cin>>c;
		int count1=0,count2=0,count3=0,judge=0;
		bool result=true;
		for(int i=0;i<c.length();i++){
			if(c[i]=='P'||c[i]=='A'||c[i]=='T'){
				if(judge==0&&c[i]=='A')count1++;
				else if(judge==0&&c[i]=='P')judge=1;
				else if(judge==1&&c[i]=='A')count2++;
				else if(judge==1&&c[i]=='T')judge=2;
				else if(judge==2&&c[i]=='A')count3++;
				else result=false;
			}
			else{
				result=false;
				break;
			}
		}
		if(judge!=2||count1*count2!=count3||count2==0)result=false;
		if(result)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}

1004 成绩排名

#include<iostream>
using namespace std;
typedef struct student{
	string name;
	string num;
	int score;
}Student;
int main()
{
	int n;
	cin>>n;
	struct student stu[n];
	for(int i=0;i<n;i++){
		cin>>stu[i].name;
		cin>>stu[i].num;
		cin>>stu[i].score;
	}
	struct student min=stu[0];
	struct student max=stu[0];
	for(int i=1;i<n;i++){
		if(stu[i].score>max.score)max=stu[i];
		if(stu[i].score<min.score)min=stu[i];
	}
	cout<<max.name<<" "<<max.num<<endl;
	cout<<min.name<<" "<<min.num<<endl;
	return 0;
}

1005 继续(3n+1)猜想

#include<iostream>
using namespace std;
int main()
{
	int n;
	int num[101]={0};
	cin>>n;
	for(int i=0;i<n;i++){
		int tmp;
		cin>>tmp;
		num[tmp]=1;
	}
	for(int i=0;i<101;i++){
		if(num[i]==1){
			int temp=i;
			while(temp>1){
				if(temp%2==0)temp/=2;
				else temp=(3*temp+1)/2;
				if(temp!=1&&temp<101)num[temp]=0;
			}
		}
	}
	int flag=1;
	for(int i=100;i>=0;i--){
		if(num[i]==1){
			if(flag==0){
				cout<<" "<<i;
			}
			else{
				flag=0;
				cout<<i;
			}
		}
	}
	return 0;
}

1006 换个格式输出整数

#include<iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a,b,c;
	a=n/100;
	b=n%100/10;
	c=n%10;
	while(a!=0){
		cout<<"B";
		a--;
	}
	while(b!=0){
		cout<<"S";
		b--;
	}
	for(int i=0;i<c;i++){
		cout<<i+1;
	}
	return 0;
}

1007 素数对猜想问题

#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
int main()
{
	int n,i,j;
	cin>>n;
	int count=0;
	vector<int> prime;
	for(int i=2;i<=n;i++){
		for(j=2;j<=sqrt(i);j++){
			if(i%j==0)break;
		}
		if(j>sqrt(i))
			prime.push_back(i);
	}
	for(int i=1;i<prime.size();i++)
		if(prime[i]-prime[i-1]==2)count++;
		cout<<count;
	return 0;
}

1008 数组元素循环右移问题

#include<iostream>
using namespace std;
//change函数
void change(int a[],int l,int r){
	for(int i=l;i<=(l+r)/2;i++){
		int tmp=a[i];
		a[i]=a[l+r-i];
		a[l+r-i]=tmp;
	}
}
int main()
{
	int m,n;
	cin>>m>>n;
	n%=m;//考虑当需要循环的次数超过了数列总数时的情况!!!
	int a[m]={0};
	for(int i=0;i<m;i++){
		cin>>a[i];
	}
	change(a,0,m-1-n);
	change(a,m-n,m-1);
	change(a,0,m-1);
	int count=1;
	for(int i=0;i<m;i++){
		if(count==1){
			cout<<a[i];
			count=0;
		}
		else{
			count=0;
			cout<<" "<<a[i];
		}
	}
}

1009 说反话

#include<iostream>
using namespace std;
int main()
{
    string str[80];
    int i=0,j;
    while(cin>>str[i]){//这个会经常使用,要记住啊。
        i++;
    }
    for(j=i-1;j>=0;j--){
        if(j!=i-1)cout<<" ";
        cout<<str[j];
    }
    return 0;
}

1010 一元多项式求导

#include<iostream>
using namespace std;
int main()
{
	int a[1000];
    int i=0;
    int m,n;
    while(cin>>m>>n){
        a[i]=m*n;
        a[i+1]=n-1;
        if(a[i]==0&&a[i+1]==-1)continue;
        else i+=2;
    }
    for(int j=0;j<i;j++){
        if(j!=0){
            cout<<" ";
        }
        cout<<a[j];
    }
    if(i<1)cout<<"0 0";//注意“零多项式”的指数和系数都是 0,但是表示为0 0.
    return 0;
}

1011 A+B 和 C

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int i=0;
    while(n--){
        i++;
        double a,b,c;//看测试用例,选用double或者float类型
        cin>>a>>b>>c;
        if(a+b>c)cout<<"Case #"<<i<<": true"<<endl;
        else cout<<"Case #"<<i<<": false"<<endl;
    }
    return 0;
}

1012 数字分类

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int n,i;
    cin>>n;
    int a[1001];
    int sum1=0,flag=1,sum2=0,count1=0,count2=0,count3=0,max=0;
    double sum3=0;
    for(i=0;i<n;i++){
        cin>>a[i];
        if(a[i]%2==0&&a[i]%5==0){
            sum1+=a[i];
        }
        else if(a[i]%5==1){
            sum2=sum2+flag*a[i];
            flag=flag*(-1);
            count2++;
        }
        else if(a[i]%5==2){
            count1++;
        }
        else if(a[i]%5==3){
            sum3+=a[i];
            count3++;
        }
        else if(a[i]%5==4){
            if(a[i]>max)max=a[i];
        }
    }
    if(sum1==0)cout<<"N ";
    else cout<<sum1<<" ";
    if(count2==0)cout<<"N ";
    else cout<<sum2<<" ";
    if(count1==0)cout<<"N ";
    else cout<<count1<<" ";
    if(count3==0)cout<<"N ";
    else printf("%.1lf ",sum3/count3);
    if(max==0)cout<<"N";
    else cout<<max;
    return 0;
}

1013 数素数

#include<iostream>
#include<cmath>
using namespace std;
bool isprime(int n){
    int i;
    if(n==2){
        return true;
    }
    else if(n<=1){
        return false;
    }
    else{
        for(i=2;i<=sqrt(n);i++){//要用平方根,如果用n/2找素数,会显示一个错误,表示代码运算量过大。
            if(n%i==0)return false;
        }
        if(i>sqrt(n))return true;
        else return false;
    }
}
int main()
{
    int m,n;
    cin>>m>>n;
    int a[110000]={0};
    int x=1;
    for(int i=1;i<110000;i++){
        if(isprime(i)){
            a[x]=i;
            x++;
        }
    }
    int count=0;
    for(int i=m;i<=n;i++){
        count++;
        count=count%10;
        if(count==1)cout<<a[i];
        else cout<<" "<<a[i];
        if(count==0)cout<<endl;
    }
    return 0;
}

1014 福尔摩斯的约会

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
    string s1,s2,s3,s4;
    cin>>s1>>s2>>s3>>s4;
    int len1=s1.length()<s1.length()?s1.length():s2.length();
    int len2=s3.length()<s4.length()?s3.length():s4.length();
    char weekday,hour;
    int minute,flag=0;
    for(int i=0;i<len1;i++){
        if(s1[i]==s2[i]&&flag==0&&(s1[i]>='A'&&s1[i]<='G')){
            weekday=s1[i];
            flag=1;
            switch(weekday){
                case 'A':cout<<"MON ";break;
                case 'B':cout<<"TUE ";break;
                case 'C':cout<<"WED ";break;
                case 'D':cout<<"THU ";break;
                case 'E':cout<<"FRI ";break;
                case 'F':cout<<"SAT ";break;
                case 'G':cout<<"SUN ";break;
            }
            continue;//必不可少,保证下一步的if语句可以执行。不使用continue的话,可以把两个if语句的执行顺序调换一下。
        }
        if(s1[i]==s2[i]&&flag==1&&((s1[i]>='A' && s1[i]<='N') || (s1[i] >= '0' && s1[i] <= '9'))){
            hour=s1[i];
            break;
        }
    }
    for(int i=0;i<len2;i++){
        if(s3[i]==s4[i]&&isalpha(s3[i])){
            minute=i;
            break;
        }
    }
    if(hour>='0'&&hour<='9'){
        cout<<'0'<<hour;
    }else{
        cout<<hour-'A'+10;
    }
    printf(":%02d", minute);
    return 0;
} 

以上就是C语言编程PAT乙级学习笔记实现示例的详细内容,更多关于C语言PAT乙级的资料请关注其它相关文章!

相关文章