Points to note for wrong questions in pat basic [1001~1015, 1101~1115]

1101 B is how many times A

normal simulations do

The function that retains 2 digits after the decimal point is cout<<fixed<<setprecision(2)<<a<<endl;

If not fixed, it is the answer that outputs 2 significant digits

#include<bits/stdc++.h>
using namespace std;
int num(long long a){
    int c=0;
    while(a){
        a=a/10;
        c++;
    }
    return c;    
}
int main(){
    long long a;cin>>a;
    int d;cin>>d;
    int n=num(a);//a位数
    long long b=a/pow(10,d);
    b=b+a%(long long)pow(10,d)*pow(10,n-d);
    cout<<fixed<<setprecision(2)<<1.0*b/a;
}

1102 Teach Super Championship Scroll

normal simulations do

It should be noted that there is only one test paper and the sales volume is 0, that is, the judgment

 If(top1.ren<=a[i].ren) and if(top2.sum<=a[i].sum) must add an equal sign

#include<bits/stdc++.h>
using namespace std;
struct node{
    string s;
    long long n=0;//单价
    long long ren=0;
    long long sum=0;
};
int main()
{
    int n;cin>>n;
    node a[10005];
    node top1,top2;
    for(int i=0;i<n;i++){
        cin>>a[i].s>>a[i].n>>a[i].ren;
        a[i].sum=a[i].n*a[i].ren;
    }
     for(int i=0;i<n;i++){
        if(top1.ren<=a[i].ren){
            top1.s=a[i].s;top1.ren=a[i].ren;
        }
        if(top2.sum<=a[i].sum){
            top2.s=a[i].s;top2.sum=a[i].sum;
        }
    }
    cout<<top1.s<<" "<<top1.ren<<'\n';
    cout<<top2.s<<" "<<top2.sum<<'\n';
    return 0;
}

1103 Destiny Score 

Do it with map, complexity 148*(nm)

Note: 1 1 does not meet the meaning of the question and cannot be output (although I do not understand why it does not meet the meaning of the question)

#include<bits/stdc++.h>
using namespace std;
#define int long long
map<long long,long long>mp;
signed main(){
    long long cha;
    int n,m;cin>>m>>n;
    for(int i=2;i<=148;i++){//cout<<sqrt(sqrt(25000*25000*25000-24999*24999*24999)/2)约等于148
        cha=i*i+(i-1)*(i-1);
        mp[cha*cha]=i;//169--3
    }
    bool flag=0;
    for(int i=m;i<=n;i++){
        cha=i*i*i-(i-1)*(i-1)*(i-1);
        if(mp[cha]){
            flag=1;
            cout<<i<<" "<<mp[cha]<<endl;
        }
    }
    if(!flag){
        cout<<"No Solution\n";
    }
}

 

1104 Everlasting

Pay attention to point 1: it needs to be sorted! ! ! ! So use vector vec to sort

Note 2: Remember to vec.clear() after using vector! ! ! ! !

//因为普通的加1肯定不行,所以都是A是9结尾的
//如果个位数是9,那么m=a+9  n=a+1,从2到90来找n和m符合最大公约数是一个大于 2 的素数
//但是找不到,公约数都是4或者8
//所以十位数也应该是9
//如果个位和十位都是9,那么m=a+18  n=a+1,从2到90来
#include<bits/stdc++.h>
using namespace std;
long long sum(long long a){
    long long sum=0;
    while(a){
        sum+=(a%10);
        a=a/10;
    }
    return sum;
}
struct node{
    long long b;long long s;
};
bool cmp(node a,node b){
    if(a.b!=b.b){return a.b<b.b;}
    return a.s<b.s;
}
bool notprime[100];
void pre(){
    for(int i=1;i<100;i++){
        for(int j=2;j*j<=i;j++){
            if(i%j==0){notprime[i]=1;break;}
        }
    }
    return;
}
int main()
{//尝试特别暴力地写代码
    pre();
    vector<node> ans;
   int n;cin>>n;
    for(int i=1;i<=n;i++){
        ans.clear();
        bool flag=0;
        cout<<"Case "<<i<<endl;
        int k,m;cin>>k>>m;
        for(int i=pow(10,k-3);i<=pow(10,k-2)-1;i++){//枚举A的前8位
            long long a=sum(i),b=sum(i+1);
            long long c=__gcd((long long)18+a,b);
            if(18+a==m&&c>2&&notprime[c]==0){
                node aa;aa.b=b;aa.s=i;
                ans.push_back(aa);
                flag=1;
            }
        }
        if(!flag){cout<<"No Solution\n";}
        else {
            sort(ans.begin(),ans.end(),cmp);
            for (auto i:ans){
                cout<<i.b<<" "<<i.s<<"99"<<endl;
            }
        }
    }
    return 0;
}

1105 List Merge

Remember the function expression of deque

Note that the next of the linked list node needs to be updated! (I re-edited the general, next time I want to be comprehensive)

! Note that when n==2m, special judgment is required

[It’s really too troublesome to write, I’m too tired after writing and don’t want to change it]

After the baptism of the last test point, it has been changed again

The last test point has heard that there are 1e6 nodes, so my initial method is to record all node information (that is, to do a discretization), but the last point can’t pass (although I don’t know why)

Because the address is 1e5, the node is directly recorded with the pair number.

Still have to pay attention to the special judgment of each case

How to write leading 0

Simpler 1105 Linked List Merge – PAT Level B Zhenti 

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
pair<int,int> add[100005];
int main()
{
    int A1,B1;int n;
    cin>>A1>>B1>>n;int ad,da,ne;
   // node p;
    for(int i=0;i<n;i++){
        cin>>ad>>da>>ne;
        add[ad]={da,ne};    }
    deque<int>a,b;int L1num=1,L2num=1;
    //L1部分
    a.push_back(A1);
    int nex=add[A1].second;

    while(nex!=-1){
        L1num++;int now=nex;
        a.push_back(now);nex=add[now].second;
    }
    //L2部分
    b.push_back(B1);
    nex=add[B1].second;
    while(nex!=-1){
        L2num++;int now=nex;
        b.push_back(now);nex=add[now].second;
    }

    //假设L1>2L2,输出两个a的头,输出一个b的尾,记得要接上所有的尾
    int p1;int p2;
    if(L1num>=2*L2num){
        p1=a.front();a.pop_front();L1num--;
        while(L2num){
            cout<< setw(5) << setfill('0') <<p1<<" "<<add[p1].first<<" "<<setw(5) << setfill('0')<<add[p1].second<<endl;
            p1=a.front();a.pop_front();L1num--;
            p2=b.back();b.pop_back();
            cout<<setw(5) << setfill('0') <<p1<<" "<<add[p1].first<<" "<<setw(5) << setfill('0') <<p2<<endl;
            if(L1num){
                p1=a.front();a.pop_front();L1num--;
                cout<<setw(5) << setfill('0') <<p2<<" "<<add[p2].first<<" "<<setw(5) << setfill('0') <<p1<<endl;
                L2num--;
            }
            else{
                cout<<setw(5) << setfill('0') <<p2<<" "<<add[p2].first<<" "<<"-1"<<endl;
                return 0;
            }
        }
        if(a.empty())
            cout<<setw(5) << setfill('0') <<p1<<" "<<add[p1].first<<" "<<"-1"<<endl;
        else
            cout<<setw(5) << setfill('0') <<p1<<" "<<add[p1].first<<" "<<setw(5) << setfill('0')<<add[p1].second<<endl;
        while(!a.empty()){
           p1=a.front();a.pop_front();
           if(add[p1].second!=-1)
           cout<<setw(5) << setfill('0') <<p1<<" "<<add[p1].first<<" "<<setw(5) << setfill('0') <<add[p1].second<<endl;
            else
             cout<<setw(5) << setfill('0') <<p1<<" "<<add[p1].first<<" -1"<<endl;
        }
    }
    else{
        p2=b.front();b.pop_front();L2num--;
        while(L1num){
            cout<<setw(5) << setfill('0') <<p2<<" "<<add[p2].first<<" "<<setw(5) << setfill('0') <<add[p2].second<<endl;
            p2=b.front();b.pop_front();L2num--;
            p1=a.back();a.pop_back();
            cout<<setw(5) << setfill('0') <<p2<<" "<<add[p2].first<<" "<<setw(5) << setfill('0') <<p1<<endl;
            if(L2num){
                p2=b.front();b.pop_front();L2num--;
                cout<<setw(5) << setfill('0') <<p1<<" "<<add[p1].first<<" "<<setw(5) << setfill('0') <<p2<<endl;
                L1num--;
            }
            else{
                cout<<setw(5) << setfill('0') <<p1<<" "<<add[p1].first<<" "<<"-1"<<endl;
                return 0;
            }
        }
        if(!b.empty())
            cout<<setw(5) << setfill('0') <<p2<<" "<<add[p2].first<<" "<<setw(5) << setfill('0') <<add[p2].second<<endl;
        if(b.empty())
            cout<<setw(5) << setfill('0') <<p2<<" "<<add[p2].first<<" "<<"-1"<<endl;
        while(!b.empty()){
            p2=b.front();b.pop_front();
            cout<<setw(5) << setfill('0') <<p2<<" "<<add[p2].first<<" ";
            if(add[p2].second!=-1)
                cout<<setw(5) << setfill('0') <<add[p2].second<<endl;
            else
                cout<<"-1"<<endl;
        }
    }
}

1106 2019 series

normal simulation

#include <bits/stdc++.h>
using namespace std;
int a[1004];
int main()
{
    a[0]=2,a[1]=0,a[2]=1,a[3]=9;
    int p=0,sum=0;
    int n;cin>>n;
    if(n==1){cout<<2;return 0;}
    if(n==2){cout<<20;return 0;}
    if(n==3){cout<<201;return 0;}
    if(n==4){cout<<2019;return 0;}
    cout<<"2019";
    sum=2+1+9;
    for(int i=4;i<n;i++){
        a[i]=sum%10;
        cout<<a[i];
        sum+=a[i];
        sum-=a[p];
        p++;
    }
    return 0;
}

1107 Mice Love Rice

Simpler analog than 1106

#include<bits/stdc++.h>
using namespace std;
int aa[104];
int main()
{
    int n,m;cin>>n>>m;
    for(int i=0;i<n;i++){
        int maxx=0,a;
        for(int j=0;j<m;j++){
            cin>>a;maxx=max(maxx,a);
        }
        cout<<maxx;
        if(i!=n-1)cout<<" ";
        aa[i]=maxx;
    }
    cout<<'\n';int maxx=0;
    for(int i=0;i<n;i++){
        maxx=max(maxx,aa[i]);
    }
    cout<<maxx<<'\n';
    return 0;
}

1108 String Repeater

normal simulation

#include<bits/stdc++.h>
using namespace std;
string s;
int a[10]={0};
int main()
{
    cin>>s;
    for(int i=0;i<s.size();i++){
        if(s[i]=='S')a[0]++;
        if(s[i]=='t')a[1]++;
        if(s[i]=='r')a[2]++;
        if(s[i]=='i')a[3]++;
        if(s[i]=='n')a[4]++;
        if(s[i]=='g')a[5]++;
    }
    while(a[0]||a[1]||a[2]||a[3]||a[4]||a[5]){
        if(a[0]){
            cout<<'S';a[0]--;
        }
        if(a[1]){
            cout<<'t';a[1]--;
        }
        if(a[2]){
            cout<<'r';a[2]--;
        }
        if(a[3]){
            cout<<'i';a[3]--;
        }
        if(a[4]){
            cout<<'n';a[4]--;
        }
        if(a[5]){
            cout<<'g';a[5]--;
        }
    }
    return 0;
}

1109 Good at C

I thought about copying and pasting directly in PHP, but found that the meaning of the question was wrong

Pay attention to spaces, newlines, etc.

! The last test point should pay attention to the last symbol is optional

! ! Note that there are spaces in the middle of the string, so use readline

! ! ! There can be many, many non-English uppercase characters at the beginning and end of the string, pay attention to handling

#include<bits/stdc++.h>
using namespace std;
char mp[27][7][7];
void P(string sen,bool flag){
    for(int q=0;q<7;q++){
        for(int i=0;i<(int)sen.size();i++){
            for(int j=0;j<5;j++){
                cout<<mp[sen[i]-'A'][q][j];
            }
            if(i!=(int)sen.size()-1)
                cout<<' ';
        }
        cout<<'\n';
    }
    if(!flag)cout<<'\n';
}

int main()
{
    for(int i=0;i<26;i++)
        for(int j=0;j<7;j++)
            for(int k=0;k<5;k++)
                cin>>mp[i][j][k];

    string ans;string sen="";
    cin.get();
    getline(cin,ans);
    int num=0,al=1;
    int length=ans.size();
    for(int i=length-1;i>=0;i--){
        if(ans[i]>'Z'||ans[i]<'A')length--;
        else break;
    }
    for(int i=0;i<length;i++)
        if(ans[i]>'Z'||ans[i]<'A')al++;
        
    for(int i=0;i<length;i++){
        if(ans[i]>'Z'||ans[i]<'A'){
            num++;
            if(sen!=""){
                P(sen,num==al);sen="";
            }
        }
        else
            sen=sen+ans[i];
    }
    num++;
    if(sen!="")P(sen,num==al);
    sen="";
    return 0;
}

1110 block reversal

It feels simpler than 1109 and 1105

#include<bits/stdc++.h>
using namespace std;
pair<int,int>add[100005];
int a[100005];
stack<int>st;
queue<int>que;
vector<int>vec;
void pre(int len){
    for(int ii=0;ii<len;ii++){
        a[ii]=st.top();st.pop();
    }
    for(int ii=len-1;ii>=0;ii--)
        que.push(a[ii]);
}
int main()
{
    int A,n,K;cin>>A>>n>>K;int q;
    for(int i=0;i<n;i++){
        cin>>q;cin>>add[q].first>>add[q].second;
    }
    int now=A;
    int length=0;
    while(now!=-1){
        st.push(now);//链表结点stack
        length++;
        now=add[now].second;
    }
    if(length%K) pre(length%K);//先处理多出来的结点
    
    for(int i=0;i<length/K;i++) pre(K);

    while(!que.empty()){
        int now=que.front();que.pop();
        cout<<setw(5)<<setfill('0')<<now;
        cout<<" "<<add[now].first;
        if(!que.empty())
            cout<<" "<<setw(5)<<setfill('0')<<que.front()<<'\n';
        else cout<<" -1"<<'\n';
    }
    return 0;
}

1111 Symmetry Day

normal simulation

#include<bits/stdc++.h>
using namespace std;
string ver(string y,string m,string d){
    if(m=="Jan")y=y+"01";
    if(m=="Feb")y=y+"02";
    if(m=="Mar")y=y+"03";
    if(m=="Apr")y=y+"04";
    if(m=="May")y=y+"05";
    if(m=="Jun")y=y+"06";
    if(m=="Jul")y=y+"07";
    if(m=="Aug")y=y+"08";
    if(m=="Sep")y=y+"09";
    if(m=="Oct")y=y+"10";
    if(m=="Nov")y=y+"11";
    if(m=="Dec")y=y+"12";
    if (d[1]==',')y=y+"0"+d[0];
    else y=y+d[0]+d[1];
    return y;
}
string ju(string da){
    for(int i=0,j=(int)da.size()-1;i<j;i++,j--){
        if(da[i]!=da[j])return "N";
    }
    return "Y";
}
int main()
{
    int n;cin>>n;string m,d,y;
    for(int i=0;i<n;i++){
        cin>>m>>d>>y;
        if(y.size()==1)y="000"+y;
        if(y.size()==2)y="00"+y;
        if(y.size()==3)y="0"+y;
        string data=ver(y,m,d);
        cout<<ju(data)<<" "<<data<<'\n';
    }
    
    return 0;
}

 

1112 Excess range

normal simulation

Note: If no data exceeds the standard, output the maximum value of all data in one line.

#include<bits/stdc++.h>
using namespace std;
int a[10004];
int main()
{
    int n,T,maxx=0;bool f=0;
    cin>>n>>T;
    for(int i=0;i<n;i++)cin>>a[i];
    for(int l=0;l<n;l++){
        maxx=max(maxx,a[l]);
        if(a[l]>T){
            f=1;
            int r=l;
            while(a[r]>T&&r<n){
                r++;
            }
            cout<<"["<<l<<", "<<r-1<<"]"<<endl;
            l=r-1;
        }
    }
    if(!f)cout<<maxx;
    
    return 0;
}

1113 Addition of Qianchuanzi

normal simulation

Note: The leading 0 is processed when outputting, so that ordinary 0 can also be output

    bool flag=0;
    for(int i=lena-1;i>=0;i--){
        if(!flag&&c[i]=='0'&&i!=0)continue;
        cout<<c[i];flag=1;
    } 

1114 All day

normal simulation

! Note: 2 is a prime number! ! !

#include<bits/stdc++.h>
using namespace std;
int da[10];
bool pri(int a){
    if(a==1)return 0;
    if(a==2)return 1;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return 0;
    }
    return 1;
}
int main()
{
    string a;cin>>a;
    int sum=0;
    for(int i=7;i>=0;i--){
        sum=(a[i]-'0')*pow(10,7-i)+sum;
        da[i]=sum;
    }
    bool fl=1;
    for(int i=0;i<8;i++){
        cout<<setw(8-i)<<setfill('0')<<da[i]<<" ";
        if(pri(da[i]))cout<<"Yes\n";
        else {fl=0;cout<<"No\n";}
    }
    if(fl)cout<<"All Prime!";
    return 0;
}

1115 Referee machine

Note: The matrix of the sample input is that each row is the number entered by the same person in different rounds

! Notice: Round #k: i is out. 是一个一个输出的,不是合在一起输出的

!!!注意int num[14][1002];是这样设置的,不要设倒了!!

【不然会会浪费个5、6个小时……还没有runtime error的报错……】

#include<bits/stdc++.h>
using namespace std;
vector<int>ex;
bool vis[100005]={0};
bool iru[100005]={0};
bool peo[11]={0};
int num[14][1002];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int a,b;cin>>a>>b;
    ex.push_back(a);ex.push_back(b);
    iru[a]=1;iru[b]=1;
    vis[abs(a-b)]=1;
    int n,m;
    cin>>n>>m;
    int ans;int lo=0;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            cin>>num[i][j];
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            ans=num[j][i];if(peo[j])continue;
             if(vis[ans]==1&&!iru[ans]){
                iru[ans]=1;
                for(auto u:ex){
                    vis[abs(u-ans)]=1;
                }
                ex.push_back(ans);
            }
            else{
                peo[j]=1;lo++;
                cout<<"Round #"<<i<<": ";
                cout<<j<<" ";
                cout<<"is out.\n";
            }
        }
    }
    if(lo==n)cout<<"No winner.";
    else{
        cout<<"Winner(s):";
        for(int i=1;i<=n;i++)
            if(!peo[i])cout<<" "<<i;
    }
    return 0;
}

By the way, in order to do this question, I learned to shoot .

The file to be tested is:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>ex;
bool vis[100005];
bool iru[100005];
bool peo[11]={0};
int num[14][1002];
int main()
{
	freopen("input.txt","r",stdin);//生成得数据文件
	freopen("output1.txt","w",stdout);//输出文件
	//下面填你的待测程序
     ios::sync_with_stdio(0);
     cin.tie(0);
    int a,b;cin>>a>>b;
    ex.push_back(a);ex.push_back(b);
    iru[a]=1;iru[b]=1;
    vis[abs(a-b)]=1;
    int n,m;
    cin>>n>>m;
    int ans;int lo=0;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cin>>num[i][j];
        }
    }
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            ans=num[j][i];if(peo[j])continue;
             if(vis[ans]==1&&!iru[ans]){
                iru[ans]=1;
                for(auto u:ex){
                    vis[abs(u-ans)]=1;
                }
                ex.push_back(ans);
            }
            else{
                peo[j]=1;lo++;
                cout<<"Round #"<<i<<": ";
                cout<<j<<" ";
                cout<<"is out.\n";
            }
        }
    }
    if(lo==n)cout<<"No winner.";
    else{
        cout<<"Winner(s):";
        for(int i=1;i<=n;i++){
            if(!peo[i])cout<<" "<<i;
        }
    }
	return 0;
}

Evaluation file (correct code)

    #include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
vector<int>v;
//unordered_map<int,int>mp;
int mp[100005]={0};
//unordered_map<int,int>exist;
int exist[100005]={0};
bool ac(int number)
{
    for (int i = 0; i < v.size(); ++i) {
        for (int j = i+1; j < v.size() ; ++j) {
            if(abs(v[i]-v[j])==number&&exist[number]!=1)return true;
        }
    }
    return false;
}
int main(){
	freopen("input.txt","r",stdin);//生成得数据文件
	freopen("output2.txt","w",stdout);//输出文件
    int a,b;
    cin>>a>>b;
    v.push_back(a);
    v.push_back(b);
    exist[a]=1;
    exist[b]=1;
    int N,M;
    cin>>N>>M;
    int cnt=0;
    int arr[N][M];
    for (int i = 0; i < N; ++i)
        for (int j = 0; j < M; ++j)
            scanf("%d",&arr[i][j]);
    for (int j = 0; j < M; ++j) {//第j+1轮
        for (int i = 0; i < N; ++i) {//第i+1个人
            if(mp[i]!=-1){
                if(ac(arr[i][j])){
                    v.push_back(arr[i][j]);
                    exist[arr[i][j]]=1;
                }
                else{
                    printf("Round #%d: %d is out.\n",j+1,i+1);
                    mp[i]=-1;
                    cnt++;
                }
            }
        }
    }
    if(cnt!=N){
        cout<<"Winner(s):";
        for (int i = 0; i < N; ++i) {
            if(mp[i]!=-1)printf(" %d",i+1);
        }
    }
    else  printf("No winner.\n");
    return 0;
}

data generation

#include<iostream>
#include<ctime>
#include<fstream>
using namespace std;
void data(){
	ofstream fout("input.txt");//将数据录入到input文件
	//根据题生成所需得数据然后数据范围要根据你的暴力程序去确定范围

	int A=rand()%100000+1;int B=rand()%100000+1;
	int l,r;
	fout<<A<<" "<<B<<endl;
	int n=rand()%9+2;int m=rand()%999+2;
	fout<<n<<" "<<m<<endl;
	for(int i=0;i<m;i++){
	     for(int j=0;j<n;j++){
               l=rand()%(max(A,B)+3)+1;
               fout<<l<<" ";
		}
		fout<<"\n";
	}
	fout.close();

}
bool test(){
	data();//生成数据
	system("test.exe");//运行待测程序
	system("bl.exe");//运行评测程序
	return !system("fc output1.txt output2.txt");//比对这俩的输出是否一样
}
int main(){
	srand(time(NULL));
	for(int i=0;i<100000;i++){//随便测个100次
	     cout<<i<<'\n';
		if(!test()) break;
	}
	return 0;
}

1001 The (3n+1) conjecture that kills people

simple simulation

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;cin>>n;
    int c=0;
    while(n!=1){
        if(n%2==0){
            n=n/2;
        }
        else{
            n=(3*n+1)/2;
        }
        c++;
    }
    cout<<c<<endl;
    return 0;
}

1002 Write the number

normal simulation

Note: zero is back nasal

#include<bits/stdc++.h>
using namespace std;
int p(string s){
    int sum=0;
    for(int i=0;i<(int)s.size();i++){
        sum=sum+(s[i]-'0');
    }
    return sum;
}
string ss[]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
void ps(int n){
    int t=n;int wei=0;
    while(t){
        t=t/10;wei++;
    }
    for(int i=wei-1;i>=0;i--){
        int y=n/pow(10,i);
        cout<<ss[y];
        n=n%(int)(pow(10,i));
        if(i)cout<<" ";
    }
}
int main()
{
    string s;cin>>s;
    if(s=="0"){cout<<"ling";return 0;}
    int sum=p(s);
    ps(sum);
    return 0;
}

1003 I want to pass!

#I can't understand what this topic is talking about

Topic meaning: PAT Level B-1003-"I want to pass" detailed explanation-why I can't even understand the question_pat I want to pass

 As you can see from the previous link, in the third condition:

When a and c are equal, PAT, PAAT, PAAAT, PAAAAT... are all correct

When a and c are A, APATA, APATA, APAATAA, APAAATAAA, APAAAATAAAA...Correct

When a and c are the first AA, AAPATAA, AAPAATAAAA, AAPAAATAAAAAAA

In other words, len(a)*len(b)=len(c) and len(b) is not equal to 0

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T;cin>>T;
    cin.get();
    while(T--){
        string s;
        getline(cin,s);
        int P=-1,T=-1;
        bool flag=0;
        for(int i=0;i<(int)s.size();i++){
            if(s[i]=='P'){//有且只有一个P
                if(P==-1)P=i;
                else {cout<<"NO\n";flag=1;break;}
            }
            else if(s[i]=='T'){//有且只有一个T
                if(T==-1)T=i;
                else {cout<<"NO\n";flag=1;break;}
            }
            else if(s[i]!='A'){cout<<"NO\n";flag=1;break;}//只有PAT
        }
        if(flag==1)continue;
        int lena=0,lenb=0,lenc=0;
        lena=P;
        lenb=T-P-1;
        lenc=(int)s.size()-T-1;
        if(lena*lenb==lenc&&lenb!=0)cout<<"YES\n";
        else cout<<"NO\n";
    }
}

1004 results ranking

normal simulation

#include<bits/stdc++.h>
using namespace std;
vector<string>name;
vector<string>xue;
int cheng[1004];
int  main()
{
    int n;cin>>n;
    string a,b;int l;
    int maxx=0,minn=100;
    for(int i=0;i<n;i++){
        cin>>a>>b;name.push_back(a);xue.push_back(b);
        cin>>l;cheng[l]=i;
        maxx=max(maxx,l);
        minn=min(minn,l);
    }
    cout<<name[cheng[maxx]]<<" "<<xue[cheng[maxx]]<<"\n";
    cout<<name[cheng[minn]]<<" "<<xue[cheng[minn]]<<"\n";
    return 0;
}

1005 Continue (3n+1) conjecture

It was troublesome at the beginning, but it was written and collected

#include<bits/stdc++.h>
using namespace std;
bool f[3500];
vector<int>ve;
bool cmp(int a,int b){return a>b;}
int main()
{
    int K;cin>>K;
    for(int i=0;i<K;i++){
        int n;
        cin>>n;ve.push_back(n);
        if(f[n]==1)continue;
        while(n!=1){
            if(n%2==0){
                n=n/2;
                f[n]=1;
            }
            else{
                n=(3*n+1)/2;
                f[n]=1;
            }
        }
    }
    sort(ve.begin(),ve.end(),cmp);
    int ans=0;
    for(int i=0;i<K;i++)
        if(!f[ve[i]]){ans++;}
    
    for(int i=0;i<K;i++){
        if(!f[ve[i]]){
            cout<<ve[i];ans--;
            if(ans!=0){cout<<" ";}
        }
    }
    return 0;
}

1006 Output integer in another format

normal simulation

#include<bits/stdc++.h>
using namespace std;
void pri(int n){
    int a,b,c;
    a=n/100;
    c=n%10;
    b=(n%100)/10;
    for(int i=0;i<a;i++)cout<<"B";
    for(int i=0;i<b;i++)cout<<"S";
    for(int i=1;i<=c;i++)cout<<i;
}
int main()
{
    int n;cin>>n;
    pri(n);
    return 0;
}

1007 Prime Number Pair Conjecture

Use a linear sieve to find prime numbers, and then find out the pairs of prime numbers that meet the requirements

#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
bool notprime[maxn];
int prime[maxn],cnt;
void getprime(int ma) {
	notprime[0]=notprime[1]=1;
	cnt=0;
	for(int i=2;i<=ma;++i) {
		if(!notprime[i]) {
			prime[cnt++]=i;//筛质数
		}
       	for(int j=0;j<cnt && 1ll*i*prime[j]<=ma;++j) {
            notprime[i*prime[j]]=1;
            if(i%prime[j]==0) {
                break;
            }
		}
	}
}
int main()
{
    int n;cin>>n;int res=0;
    getprime(n);
    for(int i=1;i<cnt;i++){
        if((prime[i]-prime[i-1])==2){
            res++;
        }
    }
    cout<<res;
    return 0;
}

1008 Circular right shift of array elements

! Pay attention to the format output problem when n=m

! ! Note that m=n%m

#include<bits/stdc++.h>
using namespace std;
int a[1004];
int main()
{
    int n,m;cin>>n>>m;int c=0;
    m=m%n;
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=n-m;i<n;i++){
        cout<<a[i];c++;
        if(c!=n){cout<<" ";}
    }
    for(int i=0;i<n-m;i++){
        cout<<a[i];c++;
        if(c!=n){cout<<" ";}
    }
}

1009 Talking Ironically

normal simulation

#include <bits/stdc++.h>
using namespace std;
string s[100];
int main()
{
    int cnt=0;
    while(cin>>s[cnt]){
        cnt++;
    }
    for(int i=cnt-1;i>=0;i--){
        cout<<s[i];
        if(i)cout<<" ";
    }
    return 0;
}

1010 Unary Polynomial Derivation

Some explanations about the topic: 1010 Unary polynomial derivation (25 points) [additional ideas and test point analysis]

#include<bits/stdc++.h>
using namespace std;
int a[1004],b[1004];
int main()
{
    int cnt=0;
    while(cin>>a[cnt]>>b[cnt]){
        cnt++;
    }
    if(!b[cnt-1]&&cnt==1){
        cout<<a[0]*b[0]<<" ";
        cout<<b[0];
        return 0;
    }
    else if(!b[cnt-1]) cnt--;
        
    for(int i=0;i<cnt;i++){
        cout<<a[i]*b[i]<<" ";
        cout<<b[i]-1;
        if(i!=cnt-1)cout<<" ";
    }
    return 0;
}

 

1011 A+B and C

simple simulation

#include<bits/stdc++.h>
using namespace std;
# define int long long
signed main()
{
    int T;cin>>T;int a,b,c;
    for(int i=1;i<=T;i++){
        cin>>a>>b>>c;
        if(a+b>c)cout<<"Case #"<<i<<": true\n";
        else cout<<"Case #"<<i<<": false\n";
    }
    return 0;
}

1012 Number Classification

simple simulation

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int A=0,B=0,C=0,D=0,E=-1,flag=1,Dn=0,Bn=0,An=0;
    int a,n;cin>>n;
    while(n--){
        cin>>a;
        if(!(a%10)){
            A+=a;An++;
        }
        if(a%5==1){
            B=B+flag*a;Bn++;
            flag=flag*-1;
        }
        if(a%5==2)C++;
        if(a%5==3){D=D+a;Dn++;}
        if(a%5==4)E=max(E,a);
    }
    if(!An)cout<<"N ";
    else cout<<A<<" ";
    if(!Bn)cout<<"N ";
    else cout<<B<<" ";
    if(!C)cout<<"N ";
    else cout<<C<<" ";
    if(!Dn)cout<<"N ";
    else cout<<fixed<<setprecision(1)<<1.0*D/Dn<<" ";
    if(E==-1)cout<<"N";
    else cout<<E;
}

 

1013 number prime

Finding Prime Numbers Using a Linear Sieve

! Pay attention to test point 4: refer to 1013 prime numbers (20 points) + test point 4,  you need to pay attention to the maximum N=10000, that is, the 10000th prime number is about 105000

#include<bits/stdc++.h>
using namespace std;
# define ma 105000
bool notprime[105000];
int prime[ma],cnt;
void getprime() {
	notprime[0]=notprime[1]=1;
	cnt=0;
	for(int i=2;i<=ma;++i) {
		if(!notprime[i]) {
			prime[cnt++]=i;//筛质数
		}
       	for(int j=0;j<cnt && 1ll*i*prime[j]<=ma;++j) {
            notprime[i*prime[j]]=1;
            if(i%prime[j]==0) {
                break;
            }
		}
	}
}
int main()
{
    int n,m;cin>>n>>m;
    if(n==0)n=1;
    getprime();int c=0;
    for(int i=n-1;i<m;i++){
        cout<<prime[i];c++;
        if(c%10&&i!=m-1)cout<<" ";
        if(c%10==0&&c!=0)cout<<"\n";
    }
    
    return 0;
}

1014 Sherlock Holmes' Date

#It feels like a password made up at random, just break it hard

Note the leading padding of 0

! Note that the week is A to G

! ! Note that there is also a leading zero in front of the hour

#include<bits/stdc++.h>
using namespace std;
string week[]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
int main()
{
    string a,b,c,d;
    int we=0,hou=0,minu=0;
    cin>>a>>b>>c>>d;
    int cou=0;
    for(int i=0;i<(int)a.size();i++){
        if(a[i]==b[i]&&b[i]>='A'&&b[i]<='G'&&!cou){
            we=a[i]-'A';cou++;
        }
        else if(a[i]==b[i]&&cou){
            if(a[i]<='9'&&a[i]>='0'){
                hou=a[i]-'0';break;
            }
            else if(a[i]<='N'&&a[i]>='A'){
                hou=10+(a[i]-'A');break;
            }
        }
    }
    for(int i=0;i<(int)c.size();i++){
        if(c[i]==d[i]){
            if(c[i]>='a'&&d[i]<='z'){
                minu=i;break;
            }
            else if(c[i]>='A'&&d[i]<='Z'){
                minu=i;break;
            }
        }
    }
    cout<<week[we]<<" "<<setw(2)<<setfill('0')<<hou<<":"<<setw(2)<<setfill('0')<<minu;
    return 0;
}

1015 Theory of Virtue and Talent

 1015 Theory of Virtue and Ability (25 points) 

Guess you like

Origin blog.csdn.net/zy98zy998/article/details/130094727