SoFunction
Updated on 2025-03-10

Example sharing of C language programming PAT B-level learning notes

1001 The guess that kills people is not worth their lives (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 Write this number

#include<iostream>
#include<stack>
using namespace std;
int main()
{
	string str;
	cin>>str;
	int sum=0;
	bool start =true;
	for(int i=0;i<();i++){
		sum+=(str[i]-'0');
	}
	stack<int> sta;
	while(sum!=0){
		(sum%10);
		sum=sum/10;
	}
	while(!()){
		if(start)start=false;
		else cout<<" ";
		int tmp=();
		();
		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 I want to pass!

//The number of a on the left *middle = right#include&lt;iostream&gt;
using namespace std;
int main()
{
	int n;
	cin&gt;&gt;n;
	while(n--){
		string c;
		cin&gt;&gt;c;
		int count1=0,count2=0,count3=0,judge=0;
		bool result=true;
		for(int i=0;i&lt;();i++){
			if(c[i]=='P'||c[i]=='A'||c[i]=='T'){
				if(judge==0&amp;&amp;c[i]=='A')count1++;
				else if(judge==0&amp;&amp;c[i]=='P')judge=1;
				else if(judge==1&amp;&amp;c[i]=='A')count2++;
				else if(judge==1&amp;&amp;c[i]=='T')judge=2;
				else if(judge==2&amp;&amp;c[i]=='A')count3++;
				else result=false;
			}
			else{
				result=false;
				break;
			}
		}
		if(judge!=2||count1*count2!=count3||count2==0)result=false;
		if(result)cout&lt;&lt;"YES"&lt;&lt;endl;
		else cout&lt;&lt;"NO"&lt;&lt;endl;
	}
	return 0;
}

1004 Ranking

#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=stu[i];
		if(stu[i].score<)min=stu[i];
	}
	cout<<<<" "<<<<endl;
	cout<<<<" "<<<<endl;
	return 0;
}

1005 Continue (3n+1) guess

#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 Change the format to output integers

#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 Prime numbers conjecture problem

#include<iostream>
#include<vector>
#include<>
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<();i++)
		if(prime[i]-prime[i-1]==2)count++;
		cout<<count;
	return 0;
}

1008 The problem of array elements moving right

#include&lt;iostream&gt;
using namespace std;
//change functionvoid change(int a[],int l,int r){
	for(int i=l;i&lt;=(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&gt;&gt;m&gt;&gt;n;
	n%=m;//Consider the situation when the number of times that need to be looped exceeds the total number of sequences!  !  !	int a[m]={0};
	for(int i=0;i&lt;m;i++){
		cin&gt;&gt;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&lt;m;i++){
		if(count==1){
			cout&lt;&lt;a[i];
			count=0;
		}
		else{
			count=0;
			cout&lt;&lt;" "&lt;&lt;a[i];
		}
	}
}

1009 Speak the opposite

#include&lt;iostream&gt;
using namespace std;
int main()
{
    string str[80];
    int i=0,j;
    while(cin&gt;&gt;str[i]){//This will be used frequently, remember.        i++;
    }
    for(j=i-1;j&gt;=0;j--){
        if(j!=i-1)cout&lt;&lt;" ";
        cout&lt;&lt;str[j];
    }
    return 0;
}

1010 Univariate polynomial derivative

#include&lt;iostream&gt;
using namespace std;
int main()
{
	int a[1000];
    int i=0;
    int m,n;
    while(cin&gt;&gt;m&gt;&gt;n){
        a[i]=m*n;
        a[i+1]=n-1;
        if(a[i]==0&amp;&amp;a[i+1]==-1)continue;
        else i+=2;
    }
    for(int j=0;j&lt;i;j++){
        if(j!=0){
            cout&lt;&lt;" ";
        }
        cout&lt;&lt;a[j];
    }
    if(i&lt;1)cout&lt;&lt;"0 0";//Note that the exponents and coefficients of "zero polynomial" are both 0, but are expressed as 0 0.    return 0;
}

1011 A+B and C

#include&lt;iostream&gt;
using namespace std;
int main()
{
    int n;
    cin&gt;&gt;n;
    int i=0;
    while(n--){
        i++;
        double a,b,c;//Follow the test cases and choose double or float type        cin&gt;&gt;a&gt;&gt;b&gt;&gt;c;
        if(a+b&gt;c)cout&lt;&lt;"Case #"&lt;&lt;i&lt;&lt;": true"&lt;&lt;endl;
        else cout&lt;&lt;"Case #"&lt;&lt;i&lt;&lt;": false"&lt;&lt;endl;
    }
    return 0;
}

1012 Digital Classification

#include<iostream>
#include<>
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 Number prime number

#include&lt;iostream&gt;
#include&lt;cmath&gt;
using namespace std;
bool isprime(int n){
    int i;
    if(n==2){
        return true;
    }
    else if(n&lt;=1){
        return false;
    }
    else{
        for(i=2;i&lt;=sqrt(n);i++){// Use square root. If you use n/2 to find prime numbers, an error will be displayed, indicating that the code operation is too large.            if(n%i==0)return false;
        }
        if(i&gt;sqrt(n))return true;
        else return false;
    }
}
int main()
{
    int m,n;
    cin&gt;&gt;m&gt;&gt;n;
    int a[110000]={0};
    int x=1;
    for(int i=1;i&lt;110000;i++){
        if(isprime(i)){
            a[x]=i;
            x++;
        }
    }
    int count=0;
    for(int i=m;i&lt;=n;i++){
        count++;
        count=count%10;
        if(count==1)cout&lt;&lt;a[i];
        else cout&lt;&lt;" "&lt;&lt;a[i];
        if(count==0)cout&lt;&lt;endl;
    }
    return 0;
}

1014 Holmes' Date

#include&lt;iostream&gt;
#include&lt;string&gt;
#include&lt;cstdio&gt;
using namespace std;
int main()
{
    string s1,s2,s3,s4;
    cin&gt;&gt;s1&gt;&gt;s2&gt;&gt;s3&gt;&gt;s4;
    int len1=()&lt;()?():();
    int len2=()&lt;()?():();
    char weekday,hour;
    int minute,flag=0;
    for(int i=0;i&lt;len1;i++){
        if(s1[i]==s2[i]&amp;&amp;flag==0&amp;&amp;(s1[i]&gt;='A'&amp;&amp;s1[i]&lt;='G')){
            weekday=s1[i];
            flag=1;
            switch(weekday){
                case 'A':cout&lt;&lt;"MON ";break;
                case 'B':cout&lt;&lt;"TUE ";break;
                case 'C':cout&lt;&lt;"WED ";break;
                case 'D':cout&lt;&lt;"THU ";break;
                case 'E':cout&lt;&lt;"FRI ";break;
                case 'F':cout&lt;&lt;"SAT ";break;
                case 'G':cout&lt;&lt;"SUN ";break;
            }
            continue;//Essential, ensure that the next if statement can be executed.  If you do not use continue, you can change the execution order of the two if statements.        }
        if(s1[i]==s2[i]&amp;&amp;flag==1&amp;&amp;((s1[i]&gt;='A' &amp;&amp; s1[i]&lt;='N') || (s1[i] &gt;= '0' &amp;&amp; s1[i] &lt;= '9'))){
            hour=s1[i];
            break;
        }
    }
    for(int i=0;i&lt;len2;i++){
        if(s3[i]==s4[i]&amp;&amp;isalpha(s3[i])){
            minute=i;
            break;
        }
    }
    if(hour&gt;='0'&amp;&amp;hour&lt;='9'){
        cout&lt;&lt;'0'&lt;&lt;hour;
    }else{
        cout&lt;&lt;hour-'A'+10;
    }
    printf(":%02d", minute);
    return 0;
} 

The above is the detailed content of the implementation example of C programming PAT Level B study notes. For more information about C language PAT Level B, please pay attention to my other related articles!