团体程序设计天梯赛-练习集L27-L50

28.

/*#pragma GCC optimize(3)//pta判断素数 
#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>n;
    while(n--)
    {
        long long   a;
        cin>>a;
        if(a<=1)
        {
            cout<<"No"<<endl;
            continue;
        }
            bool flag=true;
            for(register int i=2;i<=sqrt(a);i++)
            {
                if(a%i==0)
                {
                    cout<<"No";
                    flag=false;
                    break;
                }
            }
            if(flag)
            {
                cout<<"Yes";
            }
            cout<<endl;
    }
    return 0;
}*/

29.

*#include<bits/stdc++.h>//pta 到底是不是太胖了 
using namespace std;
int n;
struct node
{
    double height;
    double weight;
}a[50];
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>n;
    for(register int i=1;i<=n;i++)
    {
        cin>>a[i].height>>a[i].weight;
        double levelweight=(double)(a[i].height-100)*0.9*2.0;
        if(abs(a[i].weight-levelweight)<levelweight*0.1)
        {
            cout<<"You are wan mei!";
        }
        else
        {
            if(a[i].weight>levelweight)
        {
            cout<<"You are tai pang le!";
        }
        else
        {
            cout<<"You are tai shou le!";
        }
    }
        cout<<endl;
    }
    return 0;
}*/

30.

#include<bits/stdc++.h>//pta 一帮一 
using namespace std;
int n;
struct node
{
    int sex;
    string name;
};
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>n;
    vector<node>a;
    for(register int i=0;i<n;i++)
    {
        node temp;
        cin>>temp.sex>>temp.name;
        a.push_back(temp);
    }
    for(register int i=0;i<a.size();i++)
    {
        for(register int j=a.size()-1;j>=0;j--)
        {
            if(a[i].sex!=a[j].sex)
            {
                cout<<a[i].name<<" "<<a[j].name<<endl;
                a.erase(a.begin()+j);
                break;
            }
        }
    }
    return 0;
}*/

31.

#include<bits/stdc++.h>//pta 到底是不是太胖了 
using namespace std;
int n;
struct node
{
    double height;
    double weight;
}a[50];
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>n;
    for(register int i=1;i<=n;i++)
    {
        cin>>a[i].height>>a[i].weight;
        double levelweight=(double)(a[i].height-100)*0.9*2.0;
        if(abs(a[i].weight-levelweight)<levelweight*0.1)
        {
            cout<<"You are wan mei!";
        }
        else
        {
            if(a[i].weight>levelweight)
        {
            cout<<"You are tai pang le!";
        }
        else
        {
            cout<<"You are tai shou le!";
        }
    }
        cout<<endl;
    }
    return 0;
}*/

32.

#include<bits/stdc++.h>//pta Left-pad
using namespace std;
int n;
char ch; 
string s;
int main()
{      
    cin>>n>>ch;    
    getchar();    
    getline(cin,s);    
    if(n>s.length())    
    {        
        for(int i=0;i<n-s.length();i++)        
        {            
            cout<<ch;        
        }        
        cout<<s;    
    }     
    else    
    {        
        for(int i=s.length()-n;i<s.length();i++)        
        {            
            cout<<s[i];        
        }    
    }
    return 0;
}*/

33.

这个题还是蛮有意思的,用集合来做更为简便,因为集合内不会有重复元素的出现,并且,这个题还考察了to_string 函数的使用,收获还是有的

#include<bits/stdc++.h>pta 出生年 
using namespace std;
int y;
int n;
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>y>>n;
    for(register int i=y;i<=30000;i++)
    {
        string s=to_string(i);//将数字转化为字符串
        if(s.length()<4)
        {
            string temp;
            for(register int j=0;j<4-s.length();j++)
            {
                temp=temp+"0";
            }
            s=temp+s;
        } 
        set<char>up;//用集合可以保证不会出现重复的元素 
        for(register int i=0;i<s.length();i++)
        up.insert(s[i]);
        if(up.size()==n)
        {
            printf("%d %04d",i-y,i);
            break;
        }
    }
    return 0;
}*/

34.

这个题学到了max_element函数和min_element函数的使用,在返回值中如果想获得整数,应该加上间接访问符来使用

#include<bits/stdc++.h>//pta 点赞 
using namespace std;
int n;
int a[1010];
int ans;
int cnt=-1;
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>n;
    for(register int i=1;i<=n;i++)
    {
        int num;
        cin>>num;
        for(register int j=1;j<=num;j++)
        {
            int temp;
            cin>>temp;
            a[temp]++;
        }
    }
    ans=*max_element(a+1,a+1+n);//返回最大值;
    for(register int i=1;i<=1000;i++)
    {
        if(a[i]==ans)
        {
            cnt=max(cnt,i);
        }
    } 
    cout<<cnt<<" "<<ans;
    return 0;
}*/

35.

#include<bits/stdc++.h>//pta 情人节 
using namespace std;
string a;
string b;
int cnt;
int main()
{
    std::ios::sync_with_stdio(false);
    while(1)
    {
        string temp;
        cin>>temp;
        if(temp==".")
        break;
        cnt++;
        if(cnt==2)
        a=temp;
        else if(cnt==14)
        b=temp;
    }
    if(cnt<2)
    cout<<"Momo... No one is for you ...";
    else if(cnt>=2&&cnt<14)
    {
        cout<<a<<" is the only one for you...";
    }
    else if(cnt>=14)
    {
        cout<<a<<" and "<<b<<" are inviting you to dinner...";
    }
    return 0;
}*/ 

36.37特别水,直接跳过

38.水

39

#include<iostream>
#include<string>

using namespace std;
int main()
{    
    int n=0;    
    cin>>n;    
    getchar();    
    string s;    
    getline(cin,s);    
    char blank=32;    
    int col=0;    
    if(s.length()%n!=0)    
    {        
        col=s.length()/n+1;        
        for(int i=0;i<s.length()%n;i++)        
        {            
            s+=blank;        
        }    
    }    
    else    
    {        
        col=s.length()/n;    
    }    
    string antient[col];    
    for(int i=0;i<col;i++)    
    {        
        string temp;        
        for(int j=i*n;j<i*n+n;j++)        
        {            
            temp+=s[j];        
        }        
        antient[i]=temp;    
    }    
    for(int i=0;i<n;i++)    
    {        
        for(int j=col-1;j>=0;j--)        
        {            
            cout<<antient[j][i];        
        }               
        cout<<endl;    
    }
} 

40.水过

41.

这个题需要注意的数据的输入,和LIS典型问题-拦截导弹的输入方式是一样的

#include<bits/stdc++.h>//pta 寻找250 
using namespace std;
int a[9000];
int i=1;
int j=1;
int main()
{
    std::ios::sync_with_stdio(false);
    while(j)
    {
         scanf("%d",&a[j++]);//注意这里是scanf
        char ch=getchar();
        if(ch=='\n')
        break;
    }
    for(i=1;i<=j;i++)
    {
        if(a[i]==250)
        break;
    }
    cout<<i;
    
    return 0;
}*/

42.

#include<bits/stdc++.h>//pta 日期格式化 
using namespace std;
int yyyy;
int mm;
int dd;
char ch1;
char ch2;
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>mm>>ch1>>dd>>ch2>>yyyy;
    printf("%d-%02d-%02d",yyyy,mm,dd);
    return 0;
}*/

43.

#include<bits/stdc++.h>//pta 阅览室 
using namespace std;
struct time{
    int num,hour,mit,flag;
}a[1005];
void init(){
    for(int i=1;i<=1000;i++){
        a[i].flag=0;
        a[i].num=0;
        a[i].hour=0;
        a[i].mit=0;
    }
}
int main(){
    int n,m,h,t,k=1,cnt=0,tag;
    double sum=0;
    char ss;
    scanf("%d",&n);
    init();
    while(n>0){
        scanf("%d %c %d:%d",&m,&ss,&h,&t);
        if(m==0){
            if(cnt&&sum)
                printf("%d %.lf\n",cnt,sum/cnt);
            else
                printf("0 0\n");
            if(n>1){
            k=1;cnt=0;sum=0;
            init();
        }
            n--;
        }
        else if(ss=='S'){
            a[k].num=m;a[k].hour=h;a[k].mit=t;
            k++;
        }
        else if(ss=='E'&&k>1){
            tag=0;
            for(int i=1;i<k;i++){
                if(a[i].num==m&&a[i].flag==0){
                    tag=i;
                    a[tag].flag=1;
                }
            }
            if(tag){
                sum+=(h-a[tag].hour)*60+t-a[tag].mit;
                cnt++;
            }
        }
    }
return 0;
}*/

44.

#include<bits/stdc++.h>//pta 稳赢 
using namespace std;
int n;
int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(register int i=1;;i++)
    {
        string temp;
        cin>>temp;
        if(temp=="End")
        break;
        if(i%(n+1)==0)
        {
            if(temp=="ChuiZi")
            cout<<"ChuiZi"<<endl;
            else if(temp=="JianDao")
            {
                cout<<"JianDao"<<endl;
            }
            else if(temp=="Bu")
            cout<<"Bu"<<endl;
        }
        else
        {
            if(temp=="ChuiZi")
            cout<<"Bu"<<endl;
            else if(temp=="JianDao")
            {
                cout<<"ChuiZi"<<endl;
            }
            else if(temp=="Bu")
            cout<<"JianDao"<<endl;
        }
    }
    return 0;
}*/

45.

46.

这个题我要讲一讲了,不得不说翁老师出题水平很高,佩服佩服;

算了,专门出篇博客吧

47.

48.

考的矩阵乘法运算,今天又给我复习了一下,顺带复习了矩阵快速幂

#include<bits/stdc++.h>//pta 矩阵A*B
using namespace std;
int r1,c1;
int r2,c2;
int a[1010][1010];
int b[1010][1010];
int res[1010][1010];
int main()
{
    std::ios::sync_with_stdio(false);
    cin>>r1>>c1;
    for(register int i=0;i<r1;i++)
    {
        for(register int j=0;j<c1;j++)
        {
            cin>>a[i][j];
        }
    }
    cin>>r2>>c2;
    for(register int i=0;i<r2;i++)
    {
        for(register int j=0;j<c2;j++)
        {
            cin>>b[i][j]; 
        }
    }
    if(c1==r2)
    {
        cout<<r1<<' '<<c2<<endl;
        for(register int i=0;i<r1;i++)
        {
            for(register int j=0;j<c2;j++)
            {
                for(register int k=0;k<r2;k++)
                {
                    res[i][j]+=a[i][k]*b[k][j]; 
                }
                if(j!=c2-1)
                cout<<res[i][j]<<' '; 
                else
                cout<<res[i][j];
            }
            if(i!=r1-1)
            cout<<endl;
        } 
        }
    else
    printf("Error: %d != %d", c1, r2);
    return 0;
}*/

49.

还没做呢

50.

出篇博客讲讲

posted @ 2022-04-14 19:37  江上舟摇  阅读(20)  评论(0编辑  收藏  举报