PTA 中文题目题解合集

文章目录

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

在这里插入图片描述

#include <cstdio>
using namespace std;
int main()
{
   
   
    int n;
    scanf("%d",&n);
    int step = 0;
    while(n != 1)
    {
   
   
        if(n&1) n = n*3 + 1;
        n >>= 1;
        step++;
    }
    printf("%d\n",step);
    return 0;
}

1002 写出这个数

在这里插入图片描述

#include <iostream>
#include <map>
using namespace std;
typedef long long ll;
string s;
char t[1007];
map<int,string> mp;
ll ans = 0;
void init()
{
   
   
    for(int i = 0; i <= 9; ++i)
    {
   
   
        if(i == 0) mp[i] = "ling";
        if(i == 1) mp[i] = "yi";
        if(i == 2) mp[i] = "er";
        if(i == 3) mp[i] = "san";
        if(i == 4) mp[i] = "si";
        if(i == 5) mp[i] = "wu";
        if(i == 6) mp[i] = "liu";
        if(i == 7) mp[i] = "qi";
        if(i == 8) mp[i] = "ba";
        if(i == 9) mp[i] = "jiu";
    }
}
int main()
{
   
   
    init();
    cin>>s;
    int len = s.size();
    for(int i = 0; i < len; i++)
        ans += s[i] - '0';
    int cnt = 0;
    while(ans)
    {
   
   
        t[cnt++] = ans%10 + '0';
        ans /= 10;
    }
    for(int i = cnt - 1; ~i; --i)
    {
   
   
        int x = t[i] - '0';
        if(i == cnt-1) cout<<mp[x];
        else cout<<" "<<mp[x];
    }
    cout<<'\n';
    return 0;
}

1003 我要通过!

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;
string s;
int main()
{
   
   
    int t;
    cin>>t;
    while(t--)
    {
   
   
        cin>>s;
        int len = s.size();
        int pos1 = 0, pos2 = 0, okP = 0;
        for(int i = 0; i < len; ++i)
        {
   
   
            if(s[i] == 'P' && !okP) pos1 = i, okP = 1;
            if(s[i] == 'T' && okP == 1) pos2 = i, okP = 2;
        }
        
        if(okP != 2) cout<<"NO"<<'\n';
        else
        {
   
   
            int x = 0, y = 0, z = 0;
            for(int i = 0; i < pos1; ++i)
            {
   
   
                if(s[i] == 'A') x++;
                else 
                {
   
   
                    x = -0x3f3f3f3f;
                    break;
                }
            }
            for(int i = pos1 + 1; i < pos2; ++i)
            {
   
   
                if(s[i] == 'A') y++;
                else 
                {
   
   
                    y = 0;
                    break;
                }
            }
            for(int i = pos2 + 1; i < len; ++i)
            {
   
   
                if(s[i] == 'A') z++;
                else 
                {
   
   
                    z = 0;
                    break;
                }
            }
            if(!y || x*y != z) cout<<"NO"<<'\n';
            else cout<<"YES"<<'\n';
        }
    }
    return 0;
}

1004 成绩排名

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
struct node
{
   
   
    string name,id;
    int core;
    bool operator < (const node & t) const{
   
   
        return core < t.core;
    }
}a[1007];
int main()
{
   
   
    int n;
    cin>>n;
    for(int i = 0; i < n; ++i) cin>>a[i].name>>a[i].id>>a[i].core;
    sort(a,a+n);
    cout<<a[n-1].name<<" "<<a[n-1].id<<'\n';
    cout<<a[0].name<<" "<<a[0].id<<'\n';
    return 0;
}

1005 继续(3n+1)猜想

在这里插入图片描述
把 2 ~ 100 中运算过程中覆盖的数字求出来,用set存,开个标记数组,把输入的数从大向小找,如果 输入的数是出现在覆盖了的表中,然后标记一下,最后看哪些数没有被覆盖。

#include <cstdio>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5 +  7;
int a[107];
set<int> st[100];
int vis[maxn];
int ans[107];
void init()
{
   
   
    for(int i = 2; i < 100; i++)
    {
   
   
        int t = i;
        while(t != 1)
        {
   
   
            if(t&1) t = t*3 + 1;
            t >>= 1;
            st[i].insert(t);
        }
    }
}
int main()
{
   
   
    init();
    int n;
    scanf("%d",&n);
    for(int i = 0; i < n; ++i) scanf("%d",&a[i]);
    sort(a,a+n);
    for(int i = n-1; i >= 0; --i)
    {
   
   
        for(int j = 0; j < n; ++j)
        {
   
   
            if(st[a[i]].find(a[j])!= st[a[i]].end()) vis[a[j]] = 1;
        }
    }
    int idx = 0;
    for(int i = 0; i < n; ++i)
        if(!vis[a[i]]) ans[idx++] = a[i];

    sort(ans,ans+idx);
    for(int i = idx-1; i >= 0; --i)
    {
   
   
        if(i == idx-1) printf("%d",ans[i]);
        else printf(" %d",ans[i]);
    }
    return 0;
}

1006 换个格式输出整数

在这里插入图片描述

#include <cstdio>
using namespace std;
int main()
{
   
   
    int n;
    scanf("%d\n",&n);
    int b,s,g;
    g = n%10;
    n/=10;
    s = n%10;
    n/=10;
    b = n;
    for(int i = 0; i < b; ++i) printf("B");
    for(int i = 0; i < s; ++i) printf("S");
    for(int i = 1; i <= g; ++i) printf("%d",i);
    return 0;
}

1007 素数对猜想

在这里插入图片描述
线性素数筛

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 7;
bool st[maxn];
int primes[maxn],cnt = 0;
void Get_primes()
{
   
   
    for(int i = 2; i <= maxn; ++i)
    {
   
   
        if(!st[i]) primes[cnt++] = i;
        for(int j = 0; primes[j] <= maxn/i; ++j)
        {
   
   
            st[primes[j]*i] = true;
            if(i%primes[j] == 0) break;
        }
    }
}
int main()
{
   
   
    Get_primes();
    int n, cnt = 0;
    scanf("%d",&n);
    for(int i = 1; primes[i] <= n; i++)
        if(primes[i] - primes[i-1] == 2) cnt++;
    printf("%d\n",cnt);
    return 0;
}

1008 数组元素循环右移问题

在这里插入图片描述

#include <iostream>
using namespace std;
const int maxn = 110;
int a[maxn], b[maxn];
int n,m;
int main()
{
   
   
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i++) scanf("%d",&a[i]);
    for(int i = 0; i < n; i++) b[(i+m)%n] = a[i];
    for(int i = 0; i < n; i++)
        if(!i) printf("%d",b[i]);
        else printf(" %d",b[i]);
    return 0;
}

1009 说反话

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int main()
{
   
   
    string s="";
    getline(cin,s);

    for(int i = s.size()-1; i >= 0; --i)
    {
   
   
        string t = "";
        while(s[i] != ' ' && i >= 0)
        {
   
   
            t += s[i];
            --i;
        }
        for(int j = t.size()-1; j >= 0; --j) cout<<t[j];
        if(i>=0) cout<<" ";
    }
    return 0;
}

1010 一元多项式求导

在这里插入图片描述
g e t l i n e getline getline 读入然后去转换会莫名的 w a wa wa 掉一部分数据。

#include <iostream>
using namespace std;
int a[110],b[110];
int c[110],d[110];
int main()
{
   
   
    int cnt = 0;
    while(cin>>a[cnt]>>b[cnt])
    {
   
   
        if(b[cnt] == 0) break;
        cnt++;
    }
    int idx = 0;
    if(!cnt)
        cout<<"0 0"<<endl;
    else
    {
   
   
        for(int i = 0; i < cnt; ++i)
        {
   
   
            c[idx] = a[i]*b[i];
            d[idx++] = b[i] - 1;
        }
        for(int i = 0; i < idx; ++i)
            if(!i) cout<<c[i]<<" "<<d[i];
            else cout<<" "<<c[i]<< " "<<d[i];
        cout<<endl;
    }
    return 0;
}

1011 A+B 和 C

在这里插入图片描述

#include <iostream>
using namespace std;
int main()
{
   
   
    long long a,b,c;
    int t;
    cin>>t;
    for(int i = 1; i <= t; i++)
    {
   
   
        cin>>a>>b>>c;
        cout<<"Case #"<<i<<": ";
        if(a+b > c) cout<<"true"<<'\n';
        else cout<<"false"<<'\n';
    }
    return 0;
}

1012 数字分类

在这里插入图片描述

#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
int a[1007], b[1007];
int main()
{
   
   
    int n;
    cin>>n;
    for(int i = 0; i < n; i++) cin>>a[i];
    int A = 0, B = 0, c = 0, d = 0, e = 0;
    int sum = 0,cnt = 0;
    for(int i = 0; i < n; ++i)
    {
   
   
        if(a[i]%5 == 0 && a[i]%2 == 0) A += a[i];
        if(a[i]%5 == 1) b[cnt++] = a[i];
        if(a[i]%5 == 2) c++;
        if(a[i]%5 == 3) d++, sum += a[i];
        if(a[i]%5 == 4) e = max(e,a[i]);
    }
    for(int i = 0; i < cnt; i+=2)
        B = B + b[i] - b[i+1];
    if(!A) cout<<"N ";
    else cout<<A<<" ";
    if(!cnt) cout<<"N ";
    else cout<<B<<" ";
    if(!c) cout<<"N ";
    else cout<<c<<" ";
    if(!d) cout<<"N ";
    else cout<<setiosflags(ios::fixed)<<setprecision(1)<<(double)sum/d<<" ";
    if(!e) cout<<"N";
    else cout<<e;
    return 0;
}

1013 数素数

在这里插入图片描述
素数线性筛法

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
const int maxn = 1e6 + 7;
bool st[maxn];
int cnt;
int primes[maxn];
void Get_primes()
{
   
   
    for(int i = 2; i <= maxn; ++i)
    {
   
   
        if(!st[i]) primes[cnt++] = i;
        for(int j = 0; primes[j] <= maxn/i; ++j)
        {
   
   
            st[primes[j]*i] = true;
            if(i%primes[j] == 0) break;
        }
    }
}
int main()
{
   
   
    Get_primes();
    int n,m;
    scanf(" %d%d",&n,&m);
    if(m > n) swap(m,n);
    int count = 0;
    /***
    int k = 0;
    for(int i = 2; i <= maxn; ++i) if(!st[i]) k++;
    printf("%d\n",k);
    ***/
    for(int i = m; i <= n; ++i)
    {
   
   
        count++;
        if(count == 1) printf("%d",primes[i-1]);
        else printf(" %d",primes[i-1]);
        if(count == 10) puts(""), count = 0;
    }
    return 0;
}

1014 福尔摩斯的约会

在这里插入图片描述

#include <iostream>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
string a,b,c,d;
map<char,string> mp;
map<char,int> S;
void init()
{
   
   
    mp['A'] = "MON"; mp['B'] = "TUE"; mp['C'] = "WED"; mp['D'] = "THU";
    mp['E'] = "FRI"; mp['F'] = "SAT"; mp['G'] = "SUN";

    char s = 'A', t = '0';
    for(int i = 0; i < 24; i++)
    {
   
   
        if(i < 10) S[t] = i, t++;
        if(i >= 10) S[s] = i, s++;
    }

}
int main()
{
   
   
    init();
    cin>>a>>b>>c>>d;

    int lena = a.size(), lenb = b.size(), lenc = c.size(), lend = d.size();
    int pos = 0, cnt = 0;
    int len1 = min(lena,lenb), len2 = min(lenc,lend);

    char x='0', y='0';
    for(int i = 0; i < len1; ++i)
    {
   
   
        if(a[i] == b[i] && a[i] >= 'A' && a[i] <= 'G' && !cnt) cnt++, x = a[i];
        else if(a[i] == b[i] && cnt == 1 && ((a[i] >= 'A' && a[i] <= 'N')|| (a[i] >= '0' && a[i] <= '9')) ) y = a[i],cnt++;
        if(cnt == 2) i = len1;
    }
    for(int i = 0; i < len2; ++i)
         if(c[i] == d[i] && ((c[i] >= 'a' && c[i] <= 'z') || (c[i] >= 'A' && c[i] <= 'Z'))) pos = i, i = len2;

    cout<<mp[x]<<" ";
    if(S[y] < 10) cout<<"0"<<S[y]<<":";
    else cout<<S[y]<<":";
    if(pos < 10) cout<<"0"<<pos<<'\n';
    else cout<<pos<<'\n';
    return 0;
}

1015 德才论

在这里插入图片描述

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
struct node{
   
   
    int id,d,c;
    bool operator < (const node &t) const{
   
   
        if(d+c != t.d + t.c) return (d+c) > (t.d+t.c);
        if(d != t.d) return d > t.d;
        return id < t.id;
    }
};
node a[maxn];
vector<node>v[5];
int main()
{
   
   
    int n,h,l,cnt = 0;
    scanf("%d%d%d",&n,&l,&h);
    for(int i = 0; i < n; ++i)
    {
   
   
        scanf("%d%d%d",&a[i].id,&a[i].d,&a[i].c);
        if(a[i].d >= l && a[i].c >=l) cnt++;
        if(a[i].d < l || a[i].c < l) continue;
        else if(a[i].d >= h && a[i].c >= h) v[0].push_back(a[i]);
        else if(a[i].c < h && a[i].d >= h) v[1].push_back(a[i]);
        else if(a[i].d < h && a[i].c < h && a[i].d >= a[i].c) v[2].push_back(a[i]);
        else v[3].push_back(a[i]);
    }
    printf("%d\n",cnt);
    for(int i = 0; i < 4; ++i)
    {
   
   
        sort(v[i].begin(),v[i].end());
        for(auto it : v[i]) printf("%d %d %d\n",it.id,it.d,it.c);
    }
    return 0;
}

1016 部分A+B

在这里插入图片描述

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int work(ll x, int y)
{
   
   
    int a[10] = {
   
   0};
    while(x)
    {
   
   
        int t = x%10;
        a[t]++;
        x /= 10;
    }
    return a[y];
}
int change(int x,int num)
{
   
   
    if(!num) return 0;
    int t = x;
    for(int i = 1; i < num; ++i)
        x = x*10 + t;
    return x;
}
int main()
{
   
   
    ll A,B; int x,y;
    cin>>A>>x>>B>>y;
    int ansx = work(A,x), ansy = work(B,y);

    int xx = change(x,ansx), yy = change(y,ansy);
    ll ans = (ll)(xx + yy);
    cout<<ans<<'\n';
    return 0;
}

1017 A除以B

在这里插入图片描述

#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<int>v;
int main()
{
   
   
    string s; int t;
    cin>>s>>t;
    int len = s.size();
    int temp = 0, yu = 0;
    for(int i = 0; i < len; ++i)
    {
   
   
        temp = temp*10 + s[i] - '0';
        int x = temp/t;
        temp %= t;
        v.push_back(x);
        if(i == len-1) yu = temp;
    }
    int pos = 0;
    if(!v[0] && v.size() > 1) pos++;
    for(int i = pos; i < v.size(); ++i) cout<<v[i];
    cout<<" "<<yu<<'\n';
    return 0;
}

1018 锤子剪刀布

在这里插入图片描述
输入样例

10
C J
J B
C B
B B
B C
C C
C B
J B
B C
J J

输出样例

5 3 2
2 3 5
B B
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 7;
vector<char>a,b;
int s[4],t[4];
int main()
{
   
   
    int n;
    scanf("%d",&n);
    for(int i = 0; i < n; ++i) 
    {
   
   
        char x,y;
        scanf(" %c %c",&x,&y);

        if(x == y) s[1]++,t[1]++;
        else
        {
   
   
            if((x == 'C' && y == 'J')||(x == 'J' && y == 'B') || (x == 'B' && y == 'C')) 
                s[0]++,t[2]++,a.push_back(x);
            if((x == 'C' && y == 'B')||(x == 'J' && y == 'C') || (x == 'B' && y == 'J')) 
                t[0]++,s[2]++,b.push_back(y);
        }
        
    }
    for(int i = 0; i < 3; i++)
        if(!i) printf("%d",s[i]);
        else printf(" %d",s[i]);
    puts("");
    for(int i = 0; i < 3; i++)
        if(!i) printf("%d",t[i]);
        else printf(" %d",t[i]);
    puts("");

    int bb1 = 0, bb2 = 0, cc1 = 0, cc2 = 0, jj1 = 0, jj2 = 0;
    for(auto &i : a)
    {
   
   
        if(i == 'B') bb1++;
        if(i == 'C') cc1++;
        if(i == 'J') jj1++;
    }
    for(auto &i : b)
    {
   
   
        if(i == 'B') bb2++;
        if(i == 'C') cc2++;
        if(i == 'J') jj2++;
    }
    
    if(bb1 >= cc1 && bb1 >= jj1) cout<<"B";
    else if(cc1 > bb1 && cc1 >= jj1) cout<<"C";
    else if(jj1 > bb1 && jj1 > cc1) cout<<"J";
    cout<<" ";
    if(bb2 >= cc2 && bb2 >= jj2) cout<<"B";
    else if(cc2 > bb2 && cc2 >= jj2) cout<<"C";
    else if(jj2 > bb2 && jj2 > cc2) cout<<"J";
    cout<<'\n';
    return 0;
}

1019 数字黑洞

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[4];
string changex(string x)
{
   
   
    for(int i = 0; i < 4; i++)
        a[i] = (int)(x[i] - '0');
    sort(a,a+4);
    string ans = "";
    for(int i = 0; i < 4; ++i) ans += char(a[i]+'0');
    return ans;
}

string changed(string x)
{
   
   
     for(int i = 0; i < 4; i++)
        a[i] = x[i] - '0';
    sort(a,a+4);
    string ans = "";
    for(int i = 3; ~i; --i) ans += char(a[i] + '0');
    return ans;
}

string cal(string x, string y)
{
   
   
    int ok = 0;
    string ans = "";
    for(int i = 3; ~i; --i)
    {
   
   
        int t1 = x[i] - '0', t2 = y[i] - '0';
        if(ok) t1--, ok = 0;
        if(t1 
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幸愉信奥

谢谢亲的支持,我会继续努力啦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值