admin管理员组

文章数量:1621657

还是好好学英文去打甲级顶级吧

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

思路:

可以递归也可以循环,递归算最简单

代码:

#include <iostream>

using namespace std;
int _3n(int n)
{
    if(n==1) return 0;
    if(n&1) return _3n((n*3+1)/2)+1;
    else return _3n(n/2)+1;
}
int main()
{
    int n;
    cin>>n;
    cout<<_3n(n)<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

1002 写出这个数 (20)

思路:

水题

代码:

#include <bits/stdc++.h>

using namespace std;
string shu[]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu","shi"};
int main()
{
    char ch,num[100];
    int sum=0;
    while(~scanf("%c",&ch)&&ch!='\n')
    {
        sum+=ch-'0';
    }
    sprintf(num,"%d",sum);
    cout<<shu[num[0]-'0'];
    for(int i=1;i<strlen(num);i++)
    {
        cout<<' '<<shu[num[i]-'0'];
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

1003 我要通过!(20)

思路:

字符串题目,一直都是比较弱也是怕的题目比较考验细心和耐心,这个要注意到题目上递推的关系

代码:

#include <bits/stdc++.h>

using namespace std;
int judge_1(char s[])
{
    int l=strlen(s),p=1,t=1;
    for(int i=0;i<l;i++)
    {
        if(!(s[i]=='P'||s[i]=='A'||s[i]=='T'))
            return 0;
        if(s[i]=='P')
        {
            if(p) p=0;
            else return 0;
        }
        if(s[i]=='T')
        {
            if(t) t=0;
            else return 0;
        }
    }
    return 1;
}
int judge_2(char s[])
{
    int l=strlen(s),p,t;
    for(int i=0;i<l;i++)
    {
        if(s[i]=='P') p=i;
        if(s[i]=='T') t=i;
    }
    if(t-p>1&&p*(t-p-1)==l-t-1)
    {
        return 1;
    }
    else return 0;
}
int judge(char s[])
{
    return (judge_1(s)&&judge_2(s));
}
int main()
{
    int n;
    char s[105];
    cin>>n;
    while(n--)
    {
        scanf("%s",s);
        if(judge(s))
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

1004 成绩排名 (20)

思路:

直接比较

代码:

#include <iostream>

using namespace std;

int main()
{
    string name_max,xh_max,name_min,xh_min,s,s2;
    int max=-1,min=999,ans,n;
    cin>>n;
    while(n--)
    {
        cin>>s>>s2>>ans;
        if(ans>max)
        {
            max=ans;
            name_max=s;
            xh_max=s2;
        }
        if(ans<min)
        {
            min=ans;
            name_min=s;
            xh_min=s2;
        }
    }
    cout<<name_max<<' '<<xh_max<<endl<<name_min<<' '<<xh_min<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

1005 继续(3n+1)猜想 (25)

思路:

暴力

代码:

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
map<int ,int>mp;
int _3n(int n)
{
    if(n==1) return 1;
    if(n&1)
    {
        if(!mp[(n*3+1)/2]) mp[(n*3+1)/2]=_3n((n*3+1)/2);
        return mp[(n*3+1)/2]+1;
    }
    else
    {
        if(!mp[n/2]) mp[n/2]=_3n(n/2);
        return mp[n/2]+1;
    }
}
int main()
{
    int n,a[105],flag=1;

    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        mp[a[i]]=0;
    }
    sort(a,a+n);
    for(int i=n-1;i>=0;i--)
    {
        _3n(a[i]);
    }
    for(map<int,int>::reverse_iterator it=mp.rbegin();it!=mp.rend();it++)
    {
        //cout<<"key:"<<it->first<<' '<<"ve:"<<it->second<<endl;
        if(it->second==0)
        {
            if(flag)
            {
                cout<<it->first;
                flag=0;
            }
            else
            {
                cout<<' '<<it->first;
            }
        }
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

1006 换个格式输出整数 (15)

思路:

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    char s[5];
    cin>>n;
    sprintf(s,"%03d",n);
    for(int i=0;i<s[0]-'0';i++) printf("B");
    for(int i=0;i<s[1]-'0';i++) printf("S");
    for(int i=0;i<s[2]-'0';i++) printf("%d",i+1);
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

1007. 素数对猜想 (20)

思路:

代码:

#include <bits/stdc++.h>

using namespace std;
int judeg(int n)
{
    for(int i=2;i<sqrt(n)+1;i++)
    {
        if(n%i==0) return 0;
    }
    return 1;
}
void init(int a[],int n)
{
    int k=0;
    for(int i=2;i<=n+1;i++)
    {
        if(judeg(i)) a[k++]=i;
    }
}
int main()
{
    int n,a[10005],sum=0;
    cin>>n;
    init(a,n);
    for(int i=0;a[i+1]<=n;i++)
    {
        if(a[i+1]-a[i]==2) sum++;
    }
    cout<<sum<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

1008 数组元素循环右移问题 (20)

思路:

循环输出,不用储存

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,k,a[105];
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=n-(k%n);i<2*n-(k%n);i++)
    {
        printf("%d%c",a[i%n],i==2*n-(k%n)-1?'\n':' ');
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1009 说反话 (20)

思路:

还是水,用一个string的栈存然后输出

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    stack<string>st;
    string s;
    while(cin>>s) st.push(s);
    cout<<st.top();st.pop();
    while(!st.empty())
    {
        cout<<' '<<st.top();
        st.pop();
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

1010 一元多项式求导 (25)

思路:

比较坑,题目上说的不清晰,输入的系数和质数都为0时什么都不输出,如果输入是空输出“0 0”。

代码:

#include <bits/stdc++.h>

using namespace std;
int flag=0;
void fx(int a,int b)
{
    if(a*b) 
    {   
        if(flag) cout<<' '; flag=1;
        printf("%d %d",a*b,b-1);
    }
}
int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        fx(a,b);
    }
    if(!flag) printf("0 0\n");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

1011 A+B和C (15)

思路:

水题不用思路

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long a,b,c;
    int n;cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld%lld%lld",&a,&b,&c);
        if((a+b)>c)
            printf("Case #%d: true\n",i);
        else
            printf("Case #%d: false\n",i);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

1012 数字分类 (20)

思路:

按照要求分类就好,比较繁琐,写了无数if -_-!

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;cin>>n;
    int a1=0,s1=0,a2=0,s2=0,a3=0,s3=0,a4=0,s4=0,a5=-0x3f3f3f3f,s5=0;
    while(n--)
    {
        int ans;
        cin>>ans;
        switch(ans%5)
        {
        case 0:
            if(!(ans%2))
            {
                a1+=ans;
                s1++;
            }
            break;
        case 1:
            if(!(s2%2))
                a2+=ans;
            else
                a2-=ans;
            s2++;
            break;
        case 2:
            s3++;
            break;
        case 3:
            a4+=ans;
            s4++;
            break;
        case 4:
            a5=max(a5,ans);
            s5++;
            break;
        }
    }
    if(!s1)
        cout<<'N'<<' ';
    else
        cout<<a1<<' ';
    if(!s2)
        cout<<'N'<<' ';
    else
        cout<<a2<<' ';
    if(!s3)
        cout<<'N'<<' ';
    else
        cout<<s3<<' ';
    if(!s4)
        cout<<'N'<<' ';
    else
        printf("%.1lf ",a4*1.0/s4);
    if(!s5)
        cout<<'N'<<endl;
    else
        cout<<a5<<endl;

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

1013 数素数 (20)

思路:

打表存1w个素数,筛法打表,普通方法不知道超不超,然后输出就好,隔是个要换行一次。

代码:

#include <bits/stdc++.h>

using namespace std;
int a[104799]={0},su[10005];
void init()
{
    int k=0;
    a[0]=a[1]=1;
    for(int i=2;i<104799&&k<10005;i++)
    {
        if(!a[i])
        {
            su[k]=i;
            k++;
            for(int j=i+i;j<104799;j+=i)
            {
                a[j]=1;
            }
        }
    }
}

int main()
{
    init();
    int n,m;
    cin>>n>>m;
    for(int i=n;i<=m;i++)
    {
        if((i-n+1)%10==0||i==m)
            printf("%d\n",su[i-1]);
        else
            printf("%d ",su[i-1]);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

1014 福尔摩斯的约会 (20)

思路:

这个题略坑,刚开始理解错了两次都以为是顺序往下找相同的一对,结果只用两行上下对比是不是相同就行了。然后就是注意题目上的什么大写字母,字母,数字分清每一个要找那种相同的就行了

代码:

#include <bits/stdc++.h>

using namespace std;
string DAY[7]={"MON","TUE","TUE","THU","FRI","SAT","SUN"};
int main()
{
    char s[200],s2[100];
    scanf("%s",s);
    int l=strlen(s),l2,flag=0,hh,mm,day;
    scanf("%s",s2);
    l2=strlen(s2);
    map<char, int>mp;
    for(int i=0;i<l;i++)
        mp[s[i]]=1;
    for(int i=0;i<l2;i++)
    {
        if(s2[i]==s[i])
        {
            if(!flag&&s2[i]>='A'&&s2[i]<='Z')
            {
                flag=1;
                day=s2[i]-'A';
            }
            else if(flag&&(s2[i]>='A'&&s2[i]<='N'||s2[i]>='0'&&s2[i]<='9'))
            {
                if(s[i]>='0'&&s[i]<='9')
                    hh = s2[i]-'0';
                else
                    hh = s2[i]-'A'+10;
                break;
            }
        }
    }
    scanf("%s",s);
    l=strlen(s);
    scanf("%s",s2);
    l2=strlen(s2);
    mp.clear();
    for(int i=0;i<l;i++)
        mp[s[i]]=1;
    for(int i=0;i<l2;i++)
    {
        if(s2[i]==s[i]&&(s2[i]>='A'&&s2[i]<='Z'||s2[i]>='a'&&s2[i]<='z'))
        {
            mm=i;
            break;
        }
    }
    cout<<DAY[day];
    printf(" %02d:%02d\n",hh,mm);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

1015 德才论 (25)

思路:

排序的题目,不过条件太多并且麻烦需要细心的分类和写cmp函数,定义一个结构体储存一个人的所有信息,只要分类和cmp函数没搞错,就直接用sort排序输出就行了。

代码:

#include <bits/stdc++.h>

using namespace std;
struct node{
    int id,de,ca,lei,zong;
};
int cmp(node a,node b)
{
    if(a.lei==b.lei)
    {
        if(a.zong==b.zong)
        {
            if(a.de==b.de)
                return a.id<b.id;
            return a.de>b.de;
        }
        return a.zong>b.zong;
    }
    return a.lei<b.lei;
}
int main()
{
    int sum=0,n,l,h;
    node a[100005],ans;
    cin>>n>>l>>h;
    while(n--)
    {
        scanf("%d %d %d",&ans.id,&ans.de,&ans.ca);
        if(ans.ca>=l&&ans.de>=l)
        {
            ans.zong=ans.ca+ans.de;
            if(ans.de>=h)
            {
                if(ans.ca>=h)
                    ans.lei=1;
                else
                    ans.lei=2;
            }
            else
            {
                if(ans.de>=ans.ca)
                    ans.lei=3;
                else
                    ans.lei=4;
            }
            a[sum++]=ans;
        }
    }
    sort(a,a+sum,cmp);
    cout<<sum<<endl;
    for(int i=0;i<sum;i++)
    {
        printf("%d %d %d\n",a[i].id,a[i].de,a[i].ca);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

1016 部分A+B (15)

思路:

意思很简单,只需要找出Da,Db的数量,然后写一个函数能把pa pb算出来直接相加,找Da的时候可以直接写不麻烦,也可以调用STL中的函数count()(又发现一个函数 ヽ(✿゚▽゚)ノ)。

代码:

#include <bits/stdc++.h>

using namespace std;
int px(int dx,int n)
{
    if(n==0) return 0;
    if(n==1) return dx;
    return px(dx,n-1)*10+dx;
}
int main()
{
    char s[100],s2[100],Da,Db;
    scanf("%s %c %s %c",s,&Da,s2,&Db);
    printf("%d\n",px(Da-'0',count(s,s+strlen(s),Da))+px(Db-'0',count(s2,s2+strlen(s2),Db)));
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

1017 A除以B (20)

思路:

大数问题,不过是大数除10以下整数比较容易,模拟手工算,一位一位的算然后存下来就好了。 
牛客过了官网没过,最后发现没有考虑啊a

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char a[1005],q[1005];
    int b,r,ans,j=0,i=0;
    scanf("%s %d",a,&b);
    int l=strlen(a);
    ans=a[0]-'0';i=1;
    do
    {
        if(ans<b&&i<l)
        {
            ans=ans*10+a[i++]-'0';
        }
        q[j++]=ans/b;
        ans%=b;
    }while(i<l);
    for(int i=0;i<j;i++)
        printf("%c",q[i]+'0');
    printf(" %d\n",ans);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

1018 锤子剪刀布 (20)

思路:

按照题意模拟即可(又是一堆if,是我没有get到乙级题的真谛吗 (ಥ _ ಥ))

代码:

#include <bits/stdc++.h>

using namespace std;
struct node
{

    int sheng,fu,ping;
    map<char,int>mp;
    node()
    {
        sheng=fu=ping=0;
    }
};
void input_jcb(node *J)
{
    if(J->mp['B']>=J->mp['C']&&J->mp['B']>=J->mp['J'])
    {
        printf("B");
    }
    else if(J->mp['C']>=J->mp['B']&&J->mp['C']>=J->mp['J'])
    {
        printf("C");
    }
    else if(J->mp['J']>=J->mp['B']&&J->mp['J']>=J->mp['C'])
    {
        printf("J");
    }
}
int main()
{
    int n;
    cin>>n;
    char ch,ch2;
    node *J = new node;
    node *Y = new node;
    while(n--)
    {


        cin>>ch>>ch2;
        if(ch=='B'&&ch2=='B'||ch=='C'&&ch2=='C'||ch=='J'&&ch2=='J')
        {
            J->ping++;
            Y->ping++;
        }
        if(ch=='B'&&ch2=='C')
        {
            J->sheng++;
            Y->fu++;
            J->mp['B']++;
        }
        if(ch=='B'&&ch2=='J')
        {
            Y->sheng++;
            J->fu++;
            Y->mp['J']++;
        }
        if(ch=='C'&&ch2=='J')
        {
            J->sheng++;
            Y->fu++;
            J->mp['C']++;
        }
        if(ch=='C'&&ch2=='B')
        {
            Y->sheng++;
            J->fu++;
            Y->mp['B']++;
        }
        if(ch=='J'&&ch2=='C')
        {
            Y->sheng++;
            J->fu++;
            Y->mp['C']++;
        }
        if(ch=='J'&&ch2=='B')
        {
            J->sheng++;
            Y->fu++;
            J->mp['J']++;
        }
    }
    printf("%d %d %d\n",J->sheng,J->ping,J->fu);
    printf("%d %d %d\n",Y->sheng,Y->ping,Y->fu);
    input_jcb(J);
    cout<<' ';
    input_jcb(Y);
    cout<<endl;
    return 0;
}
##大佬代码果然是  短小快!

#include <iostream>
using namespace std;
int win[3] = {0};
char max3(int *a) {
    char r = 'B';
    if(a[1] > a[0]) {
        a[0] = a[1];
        r = 'C';
    } 
    if(a[2] > a[0]) r = 'J';
    return r;
}
int main() {
    int n;
    char a[105],b[105];
    int awin[3] = {0};
    int bwin[3] = {0};
    cin >> n;     
    for(int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        int k = (a[i] - b[i]) * (a[i] - b[i]);
        if(k == 1)
            a[i] == 66 ? win[0]++,awin[0]++ : (win[1]++,bwin[0]++);
        else if(k == 49)
            a[i] == 67 ? win[0]++,awin[1]++ : (win[1]++,bwin[1]++);
        else if(k == 64)
            a[i] == 74 ? win[0]++,awin[2]++ : (win[1]++,bwin[2]++);
        else
            win[2]++;
    }
    cout << win[0] << " " << win[2] << " " << win[1] << endl;
    cout << win[1] << " " << win[2] << " " << win[0] << endl;
    cout << max3(awin) << " " << max3(bwin);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128

1019 数字黑洞 (20)

思路:

经典题,字符串与整型相互转换就好了,可以自己写也可以直接用sscanfsprintf

代码:

#include <bits/stdc++.h>

using namespace std;
int cmp(char a,char b)
{
    return a>b;
}
void zhuanhuan(char s[],int *a,int *b)
{
    sort(s,s+4,cmp);
    sscanf(s,"%d",a);
    sort(s,s+4);
    sscanf(s,"%d",b);
}
int main()
{
    int a=0,b=0,n;
    char s[10];
    cin>>n;
    sprintf(s,"%04d",n);
    zhuanhuan(s,&a,&b);
    while(1)
    {
        printf("%04d - %04d = %04d\n",a,b,a-b);
        if(a-b==6174||a==b)
            break;
        sprintf(s,"%04d",a-b);
        zhuanhuan(s,&a,&b);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

1020 月饼 (25)

思路:

基础贪心,选择性价比最高的

代码:

#include <bits/stdc++.h>

using namespace std;
struct node
{
    double jia,liang,bizhi;
};
int cmp(node a,node b)
{
    return a.bizhi>b.bizhi;
}
int main()
{
    node a[1005];
    int n;
    double d,sum=0;
    cin>>n>>d;
    for(int i=0;i<n;i++)
    {
        cin>>a[i].liang;
    }
    for(int i=0;i<n;i++)
    {
        cin>>a[i].jia;
        a[i].bizhi=a[i].jia/a[i].liang;
    }
    sort(a,a+n,cmp);
    for(int i=0;d;i++)
    {
        if(d>a[i].liang)
        {
            d-=a[i].liang;
            sum+=a[i].jia;
        }
        else
        {
            sum+=d*a[i].bizhi;
            d=0;
        }
    }
    printf("%.2lf\n",sum);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

1021 个位数统计 (15)

思路:

水题,开一个大小10的数组存每一个的次数,数字就是下标

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int mp[10]={0};
    char ch;
    while(~scanf("%c",&ch))
    {
        mp[ch-'0']++;
    }
    for(int i=0;i<10;i++)
    {
        if(mp[i])
            printf("%d:%d\n",i,mp[i]);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

1022 D进制的A+B (20)

思路:

就是考察进制转换 
牛客过了官网没过,没有考虑0的情况

代码:

#include <bits/stdc++.h>

using namespace std;
void my_itoa(int n,int r)
{
    if(n==0)printf("0");
    stack<int>st;
    while(n)
    {
        st.push(n%r);
        n/=r;
    }
    while(!st.empty())
    {
        cout<<st.top();
        st.pop();
    }
}
int main()
{
    int a,b,d;
    cin>>a>>b>>d;
    my_itoa(a+b,d);
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

1023 组个最小数 (20)

思路:

先找出一个非零的最小数字输出,然后将剩下的数字升序输出就好了

代码:

#include <iostream>

using namespace std;

int main()
{
    int mp[10]={0};
    for(int i=0;i<10;i++)
    {
        cin>>mp[i];
    }
    for(int i=1;i<10;i++)
    {
        if(mp[i])
        {
            mp[i]--;
            cout<<i;
            break;
        }
    }
    for(int i=0;i<10;i++)
    {
        if(mp[i])
        {
            for(int j=0;j<mp[i];j++)
                cout<<i;
        }
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

1024 科学计数法 (20)

思路:

看似复杂,只要明白了题目上科学计数法的规则,实际上还是对字符串的处理

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s[10005],ch;
    queue<char>qu;
    int i=0,flag=1;
    while(~scanf("%c",&ch))
    {
        if(ch=='-')cout<<'-';
        else if(ch=='+'||ch=='.');
        else if(ch>='0'&&ch<='9')
        {
            qu.push(ch);
        }
        else
        {
            break;
        }
    }
    int ans;
    cin>>ans;
    flag+=ans;
    if(flag<1)
    {
        for(int i=0; i<=-flag; i++)
        {
            if(i==1)cout<<'.';
            cout<<'0';
        }
        while(!qu.empty())
        {
            cout<<qu.front();
            qu.pop();
        }
    }
    else
    {
        while(!qu.empty())
        {
            if(i==flag)cout<<'.';
            cout<<qu.front();
            qu.pop();
            i++;
        }
        for(; i<flag; i++)
        {
            cout<<'0';
        }
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

1025 反转链表 (25)

思路:

这道题目比较坑卡了我很长时间,刚开始完全就是模拟一个链表出来然后改变指针,写着巨麻烦还wa掉了,后来想到了完全必须要在原来链表上面改动,把新的顺序重新存一遍就好了,反向部分用栈来操作反向,然后用结构体vector来存。写完过来但是也挺长的,看来大佬代码恍然大悟,只用存“指针”部分就行了,然后用c++提供的反向库函数来操作,代码果断比我的短一半。 
有一个坑点,输入的节点不一定全用上会有部分不出现在链表中的废节点,所以需要重新计算N的数量 
牛客过了官网没过,原因没考虑全部反转和反转后剩一个的情况

代码:

#include <bits/stdc++.h>

using namespace std;
struct node
{
    int data,next;
} mp[100005];
struct node2
{
    int id,data,next;
};
int main()
{
    int head,n,k,ans,flag=1;
    vector<node2>ve;
    stack<node2>st;
    cin>>head>>n>>k;
    for(int i=0; i<n; i++)//储存节点
    {
        scanf("%d",&ans);
        scanf("%d %d",&mp[ans].data,&mp[ans].next);
    }
    ans=head;
    n=1;
    while(1)
    {
        if(mp[ans].next!=-1)//遍历链表统计N
        {
            ans=mp[ans].next;
            n++;
        }
        else
        {
            break;
        }
    }
    do
    {
            for(int i=0; i<k; i++)
            {
                node2 p;
                p.data=mp[head].data;
                p.id=head;
                p.next=mp[head].next;
                st.push(p);//存入栈中
                head=mp[head].next;
            }
            while(!st.empty())
            {
                ve.push_back(st.top());//反转放入vector
                st.pop();
            }
            n-=k;
    }while(n>=k);
    while(n)//n>0且不足k个的直接放入vector
    {
        node2 p;
        p.data=mp[head].data;
        p.id=head;
        p.next=mp[head].next;
        ve.push_back(p);
        if(p.next==-1)break;
        else head=p.next;
    }
    for(int i=0;i<ve.size();i++)//输出
    {
        printf("%05d %d ",ve[i].id,ve[i].data);
        if(i==ve.size()-1)
        {
            printf("-1\n");
        }
        else
        {
            printf("%05d\n",ve[i+1].id);
        }
    }
    return 0;
}
/**
00100 4 4
00100 3 12345
12345 1 12121
12121 5 13131
13131 6 -1

00100 1 1
00100 1 -1
*/


/** 
大佬的简短代码
#include <iostream>
#include <algorithm>
using namespace std;

int main(int argc, const char * argv[])
{
    int FirstAdd,Total,K,key;
    cin>>FirstAdd>>Total>>K;
    key=FirstAdd;
    int temp,Data[100005],Next[100005];
    int i,j;
    for(i=0;i<Total;i++)
    {
        cin>>temp>>Data[temp]>>Next[temp];
    }
    int Order[100005],count=0;
    while(key!=-1)
    {
        Order[count]=key;
        key=Next[key];
        count++;
    }
    int round=count/K;
    for(i=j=0;j<round;i=i+K,j++)
    {
        reverse(Order+i, Order+i+K);
    }
    for(i=0;i<count-1;i++)
    {
        printf("%05d %d %05d\n",Order[i],Data[Order[i]],Order[i+1]);
    }
    printf("%05d %d -1\n",Order[i],Data[Order[i]]);
    return 0;
}
 */     

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129

1026 程序运行时间(15)

思路:

简单水题

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a,b,time;
    cin>>a>>b;
    time=(b-a)*1.0/100+0.5;
    int m=time/60;
    int h=m/60;
    m%=60;time%=60;
    printf("%02d:%02d:%02d\n",h,m,time);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1027 打印沙漏(20)

思路:

首先计算最多可以打印多大的沙漏,然后控制好每一行打印多少个空格和字符就好

代码:

#include <bits/stdc++.h>

using namespace std;
void out_char(int n,char ch)
{
    while(n--)printf("%c",ch);
}
int main()
{
    int n,i,sum=0;
    char ch;
    scanf("%d %c",&n,&ch);
    for(i=1;;i++)//计算最大行数
    {
        sum+=i*2-1;
        if(sum*2-1>n)
        {i--;sum=0;break;}
    }
    for(int j=0;j<i-1;j++)//打印上半个三角
    {
        out_char(j,' ');
        out_char((i-j)*2-1,ch);
        sum+=(i-j)*2-1;
        cout<<endl;
    }
    for(int j=i-1;j>=0;j--)//下半个三角
    {
        out_char(j,' ');
        out_char((i-j)*2-1,ch);
        sum+=(i-j)*2-1;
        cout<<endl;
    }
    cout<<n-sum<<endl;//输出剩余
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

1028 人口普查(20)

思路:

把日期转换成int类型直接判断范围和相减判断年龄大小就可以了 
牛客过了,官网一个wa一个pe,wa在于年龄边界。pe是因为如果没有符合条件的则只输出0。

代码:

#include <bits/stdc++.h>
#define XDATE 20140906
#define MDATA 18140906
using namespace std;

int main()
{
    string mins,maxs,ans;
    int minn=XDATE,maxx=0,y,m,d,data,sum=0;
    int n;cin>>n;
    while(n--)
    {
        cin>>ans;
        scanf("%d/%d/%d",&y,&m,&d);
        data=y*10000+m*100+d;
        if(data<=XDATE&&data>=MDATA)
        {
            sum++;
            if(data>maxx)
            {maxx=data;maxs=ans;}
            if(data<minn)
            {minn=data;mins=ans;}
        }
    }
    if(sum)
        cout<<sum<<' '<<mins<<' '<<maxs<<endl;
    else
        cout<<0<<endl;
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

1029 旧键盘 (20)

思路:

先储存键盘输出的字符,然后将应该打出的字符进行比较,如果没有输出过且没有被标记就变成大写输出来,同时标记一下这个字母的大小写。 
牛客过了官网没过,原因想当然的认为了范围直接用else来做另一种情况少考虑的一种情况。要把每种情况都用if判断出来

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s[100],s2[100];
    set<char>se;
    map<char ,int >flag;
    gets(s);gets(s2);
    int l=strlen(s2);
    for(int i=0;i<l;i++)
    {
        se.insert(s2[i]);
    }
    l=strlen(s);
    for(int i=0;i<l;i++)
    {
        if(!se.count(s[i])&&!flag[s[i]])
        {
            flag[s[i]]=1;
            if(s[i]>='a'&&s[i]<='z')
            {
                printf("%c",s[i]-32);
                flag[s[i]-32]=1;
            }
            else if(s[i]>='A'&&s[i]<='Z')
            {
                flag[s[i]+32]=1;
                printf("%c",s[i]);
            }
            else
            {
                printf("%c",s[i]);
            }

        }
    }
    cout<<endl;
    return 0;
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

1030 完美数列(25)

思路:

想了一会儿发现并没有什么好的思路,那就暴力找+二分优化,复杂度nlngn也可以接受,不过每次写二分的时候都会在边界问题上纠结好久,还是内功太差

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n,p,a[100005],maxx=0;
    scanf("%lld %lld",&n,&p);
    for(int i=0;i<n;i++)
        scanf("%lld",&a[i]);
    sort(a,a+n);
    for(int i=0;i<n-maxx;i++)
    {
        int ans=upper_bound(a+i,a+n,a[i]*p)-a;//因为查找到的是第一个大于查找值的地址,它的下一个就是<=a[i]*p了
        if(ans-i>maxx)//然后ans-i就是第i个到ans个之间的数量
            maxx=ans-i;
    }
    cout<<maxx<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

1031 查验身份证(15)

思路:

按照题意判断即可

代码:

#include <bits/stdc++.h>

using namespace std;
int quan[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char jiao[]={'1','0','X','9','8','7','6','5','4','3','2'};
int judeg(char s[])
{
    int sum=0;
    for(int i=0;i<17;i++)
        if(s[i]<'0'||s[i]>'9')
            return 0;
        else
        {
            sum+=(s[i]-'0')*quan[i];
        }
    sum%=11;
    if(jiao[sum]==s[17])
        return 1;
    return 0;
}
int main()
{
    char id[20];
    int n,flag=1;
    cin>>n;getchar();
    while(n--)
    {
        scanf("%s",id);
        if(!judeg(id))
        {
            flag=0;
            printf("%s\n",id);
        }
    }
    if(flag)printf("All passed\n");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

1032 挖掘机技术哪家强(20)

思路:

存下每个学校id,然后遍历一遍找最大就好。

代码:

#include <bits/stdc++.h>

using namespace std;
int xue[100005];
int main()
{
    set<int>se;
    int n,xveid,maxx=0;
    cin>>n;
    while(n--)
    {
        int id,fen;
        scanf("%d %d",&id,&fen);
        se.insert(id);
        xue[id]+=fen;
    }
    for(set<int>::iterator it=se.begin();it!=se.end();it++)
    {
        if(xue[*it]>maxx)
        {
            xveid=*it;
            maxx=xue[*it];
        }
    }
    cout<<xveid<<' '<<maxx<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

1033 旧键盘打字(20)

思路:

存下坏的键,然后将要打出的字符一个个判断如果没坏就输出,否则不操作 
牛客过官网wa一组,注意最后一句保证第二组非空,所以还有一种第行为空的情况

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int mp[300]={0},l;
    char s[40],s2[100005];
    gets(s);
    gets(s2);
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]>='A'&&s[i]<='Z')
        {
            mp[s[i]]=1;
            mp[s[i]+32]=1;
        }
        else
            mp[s[i]]=1;
    }
    l=strlen(s2);
    for(int i=0;i<l;i++)
    {
        if(s2[i]>='A'&&s2[i]<='Z')
        {
            if(!mp['+']&&!mp[s2[i]])
                printf("%c",s2[i]);
        }
        else
        {
            if(!mp[s2[i]])
                printf("%c",s2[i]);
        }
    }
    cout<<endl;
    return 0;
}
/**

7+IE.
7_This_is_a_test.
*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

1034 有理数四则运算(20)

思路:

用四个变量分别储存分子分明,模拟分数的计算方法,然后写好化简的函数将数字转换成最简输出 
牛客过官网wa,原因忘记考虑两个都为负数的情况

代码:

#include <bits/stdc++.h>
#define ull  long long
using namespace std;
ull gcd(ull a,ull b)
{
    return !(a%b)?b:gcd(b,a%b);
}
void huajian(ull fz,ull fm)
{
    ull gy,flag=0;
    if(fz<0||fm<0)
    {
        if(!(fz<0&&fm<0))
        {
            printf("(-");
            flag=1;
        }
        if(fz<0)fz=-fz;
        if(fm<0)fm=-fm;
    }
    gy=gcd(fz,fm);
    fz/=gy;fm/=gy;

    if(!fz)
    {
        printf("0");
    }
    else if(fm==1)
    {
        printf("%lld",fz);
    }
    else if(fz>fm)
    {
        printf("%lld %lld/%lld",fz/fm,fz%fm,fm);
    }
    else
        printf("%lld/%lld",fz%fm,fm);
    if(flag)
        printf(")");
}
int main()
{
    ull a,b,c,d;
    scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
    huajian(a,b);
    printf(" + ");
    huajian(c,d);
    printf(" = ");
    huajian(a*d+b*c,b*d);
    printf("\n");
    huajian(a,b);
    printf(" - ");
    huajian(c,d);
    printf(" = ");
    huajian(a*d-b*c,b*d);
    printf("\n");
    huajian(a,b);
    printf(" * ");
    huajian(c,d);
    printf(" = ");
    huajian(a*c,b*d);
    printf("\n");
    huajian(a,b);
    printf(" / ");
    huajian(c,d);
    printf(" = ");
    if(!c)
        printf("Inf");
    else
        huajian(a*d,b*c);
    printf("\n");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

1035 插入与归并(25)

思路:

没有什么好的思路,用插入和归并排一遍对比中间过程,如果遇到相同的就输出下一次排序的序列,当然想要A掉这一题还要会这两种排序,不会的话。。。跟我一样现学去吧。╮(╯_╰)╭ 
牛客过了官网没过,5号到7号卡了三天,代码从95行写到39行,能用别的方法还是不要模拟的好,模拟细节太多了╮(╯_╰)╭,现在对归并理解的又深了

代码:

#include <bits/stdc++.h>

using namespace std;
int yuanshi[105],mubiao[105],n,ans[105],flag=0;
int juedg(int a[],int b[],int l)
{
    for(int i=0;i<l;i++)
    {
        if(a[i]!=b[i])
            return 0;
    }
    return 1;
}
void mergepass(int a[],int b[],int k,int n)
{
    if(flag==2)return ;
    int i;
    for(i=0;i<=n-k*2;i+=k*2)
    {
        merge(a+i,a+i+k,a+i+k,a+i+2*k,b+i);
    }
    if(n<k*2)
        merge(a,a+k,a+k,a+n,b);
    if(flag)
    {
        for(int i=0;i<n;i++)
        {
            printf("%d%c",b[i],i==n-1?'\n':' ');
        }
        flag=2;
        return ;
    }
    if(juedg(mubiao,b,n))
    {
        printf("Merge Sort\n");
        flag=1;
    }
}
void input()
{
        for(int i=0;i<n;i++)
        scanf("%d",&yuanshi[i]);
    for(int i=0;i<n;i++)
        scanf("%d",&mubiao[i]);
}
int main()
{

    cin>>n;
    input();
    copy(yuanshi,yuanshi+n,ans);
    for(int i=1;i<n;i++)//插入排序
    {
        if(ans[i]<ans[i-1])
        {
            int p=upper_bound(ans,ans+i,ans[i])-ans,q=ans[i];
            copy(ans+p,ans+i,ans+p+1);
            ans[p]=q;
            if(flag)
            {
                for(int i=0;i<n;i++)
                {
                    printf("%d%c",ans[i],i==n-1?'\n':' ');
                }
                return 0;
            }
            if(juedg(mubiao,ans,n))
            {
                printf("Insertion Sort\n");
                flag=1;
            }
        }
    }
    int k=1;//归并排序
    while(k<n)
    {

        mergepass(yuanshi,ans,k,n);
        k<<=1;
        mergepass(ans,yuanshi,k,n);
        k<<=1;
        if(flag==2)
            break;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

1036 跟奥巴马一起编程(15)

思路:

水题一道,四舍五入的时候要细心

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    char ch;
    cin>>n>>ch;
    for(int i=0;i<n;i++)
        cout<<ch;
    cout<<endl;
    for(int j=0;j<(int)(n/2.0+0.5)-2;j++)
    {
        for(int i=0;i<n;i++)
            printf("%c",i==0||i==n-1?ch:' ');
        cout<<endl;
    }
    for(int i=0;i<n;i++)
        cout<<ch;
    cout<<endl;
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

1037 在霍格沃茨找零钱(20)

思路:

先全部转换成最小的单位,然后相减在转换回来,另类的进制转换

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int G,S,K,GSK_sum=0,g,s,k,gsk_sum=0;
    scanf("%d.%d.%d %d.%d.%d",&g,&s,&k,&G,&S,&K);
    GSK_sum=(G*17+S)*29+K;
    gsk_sum=(g*17+s)*29+k;
    GSK_sum-=gsk_sum;
    if(GSK_sum<0){GSK_sum=-GSK_sum;printf("-");}
    K=GSK_sum%29;
    S=(GSK_sum/29)%17;
    G=(GSK_sum/29)/17;
    printf("%d.%d.%d\n",G,S,K);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

1038 统计同成绩学生(20)

思路:

下标法标记,水题

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int mp[105]={0},ans;
    int n;cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&ans);
        mp[ans]++;
    }
    cin>>n;
    while(n--)
    {
        scanf("%d",&ans);
        printf("%d%c",mp[ans],n?' ':'\n');
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

1039 到底买不买(20)

思路:

存下每个需要的珠子的数量,然后遍历另一个串,查看是否都有,如果所需要珠子都有输出l-l2,否则遍历标记数组统计有多少没有,(刚开始我还想了有没有需要的珠子有多出的情况这时候多余的珠子就不再是l-l2,后来测试发现没有这样的数据,这个问题考不考虑都能A)

代码:

#include <bits/stdc++.h>

using namespace std;
int judeg(int a[])
{
    int sum=0;
    for(int i=0;i<300;i++)
        if(a[i])sum+=a[i];
    return sum;
}
int main()
{
    int mp[300]={0},sum=0,l,l2;
    char s[1005],s2[1005];
    scanf("%s %s",s,s2);
    l=strlen(s);sum=l2=strlen(s2);
    for(int i=0;i<l2;i++)
    {
        mp[s2[i]]++;
    }
    for(int i=0;i<l;i++)
    {
        if(mp[s[i]])
        {
            sum--;
            mp[s[i]]--;
        }
    }
    if(!judeg(mp))
    {
        printf("Yes %d\n",l-l2+sum);
    }
    else
    {
        printf("No %d\n",judeg(mp));
    }
    return 0;
}
/**
ppRYYGrrYBR2258
YrR8RrY
*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

1040 有几个PAT(25)

思路:

这个思路有点儿动态规划的感觉,每次遇到P将数量++,碰到A的时候将pa数量加上P的数量(就是这个A可以组合出来的Pa的数量),每碰到一个T的时候将PA的数量累加的SUM上,这是这个T可以组合出来的所有PAT,然后每一步判断是否要取余,最后答案就出来了

代码:

#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;

int main()
{
    char ch;
    int p=0,a=0,sum=0;
    while(~scanf("%c",&ch))
    {
        if(ch=='P')p++;
        else if(ch=='A')a+=p;
        else if(ch=='T')
        {
            sum+=a;
            if(sum>MOD)
                sum%=MOD;
        }
    }
    cout<<sum<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

1041 考试座位号(15)

思路:

水题

代码:

#include <bits/stdc++.h>

using namespace std;
struct node
{
    string id;
    int zw;
}a[1005];
int main()
{
    int n,sj,zw;
    string s;
    cin>>n;
    while(n--)
    {
        cin>>s>>sj>>zw;
        a[sj].id=s;
        a[sj].zw=zw;
    }
    cin>>n;
    while(n--)
    {
        cin>>sj;
        cout<<a[sj].id<<' '<<a[sj].zw<<endl;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

1042 字符统计(20)

思路:

水题

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int mp[30]={0},maxx=0;
    char ch;
    while(scanf("%c",&ch),ch!='\n')
    {
        if(ch>='A'&&ch<='Z')
            ch+=32;
        if(ch>='a'&&ch<='z')
            mp[ch-'a']++;
    }
    for(int i=0;i<26;i++)
    {
        if(mp[i]>mp[maxx])
            maxx=i;
    }
    printf("%c %d\n",maxx+'a',mp[maxx]);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

1043 输出PATest(20)

思路:

水,照着题目意思来就好

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int mp[30]={0},maxx=0;
    char ch;
    while(scanf("%c",&ch),ch!='\n')
    {
        if(ch>='A'&&ch<='Z')
            ch+=32;
        if(ch>='a'&&ch<='z')
            mp[ch-'a']++;
    }
    for(int i=0;i<26;i++)
    {
        if(mp[i]>mp[maxx])
            maxx=i;
    }
    printf("%c %d\n",maxx+'a',mp[maxx]);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

1044 火星数字(20)

思路:

这个题目的意思实现起来并不难,不过被坑了好久,我是被坑在高低位的火星文是不一样的,如果只有一位火星文的时候我默认当作低位来算了,事实上只有一位要判断是高位还是低位在计算。

代码:

#include <bits/stdc++.h>

using namespace std;
string _12q[]= {"tret", "jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string _12h[]= {"tret", "tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};

int stio(string ans)
{
    for(int i=0; i<13; i++)
    {
        if(_12q[i]==ans)
            return i;
    }
    for(int i=0; i<13; i++)
    {
        if(_12h[i]==ans)
            return i*13;
    }
    return 0;
}
int main()
{
    int n,num,t=0;
    cin>>n;getchar();
    cin.clear();
    while(n--)
    {
        string ans;
        getline(cin,ans);
        stringstream ss(ans);
        if(ans[0]<='9'&&ans[0]>='0')
        {
            ss>>num;
            if(num<13)
                cout<<_12q[num]<<endl;
            else
            {
                t=num%13;
                num/=13;
                cout<<_12h[num];
                if(t)
                    cout<<' '<<_12q[t];
                cout<<endl;
            }

        }
        else
        {
            ss>>ans;
            t=0;
            num=stio(ans);
            while(ss>>ans)
            {t = stio(ans);}
            cout<<num+t<<endl;
            ss.clear();
        }
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

1045 快速排序(25)

思路:

我的思路比较low 一个原数组一个排好序的数组,存下左边的最大值,右边排好序保持第一个就是原数组中右边的最小值,这样比下去把满足条件的存下来。AC后看其他人代码发现,还是没有找到排序的精髓,主元的位置就是拍好的位置,所以说遍历两个数字碰到相同的且这个数字是左边最大的就是主元(这样说不知道清不清楚意思),按照这个思路代码就简单好多了,果然还是思路最重要。因为数据比较大直接硬算会超时,我那个方法算是一种取巧的硬算。

代码:

#include <bits/stdc++.h>
#define INF 1000000005
using namespace std;

int main()
{
    int n,a[100005],j=0,b[100005],maxx=-1,minn=0;
    map<int,int>mp;
    vector<int>ve;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
        mp[a[i]]++;
    }
    b[n]=INF;mp[INF]=1;
    sort(b,b+n+1);
    for(int i=0;i<n;i++)
    {
        mp[a[i]]--;
        while(!mp[b[maxx]]) maxx++;
        //printf("%d %d %d---max==%d\n",minn,a[i],b[maxx],maxx);
        if(a[i]>minn&&a[i]<b[maxx])
        {
            ve.push_back(a[i]);
        }
        minn=max(a[i],minn);
    }
    sort(ve.begin(),ve.end());
    cout<<ve.size()<<endl;
    if(ve.size()) cout<<ve[0];
    for(int i=1;i<ve.size();i++)
    {
        printf(" %d",ve[i]);
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

1046 划拳(15)

思路:

水题,直接比大小

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a,b,c,d,n,j=0,y=0;
    cin>>n;
    while(n--)
    {
        cin>>a>>b>>c>>d;
        int ans=a+c;
        if(ans==b&&ans!=d) y++;
        else if(ans!=b&&ans==d) j++;
    }
    cout<<j<<' '<<y<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

1047. 编程团体赛(20)

思路:

开一个下标为队伍的数组,直接累加储存最大值即可。

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,d,f,dui[1005]={0},maxd=0,maxx=0;
    cin>>n;
    while(n--)
    {
        scanf("%d-%*d %d",&d,&f);
        dui[d]+=f;
        if(dui[d]>maxx)
        {
            maxd=d;maxx=dui[d];
        }
    }
    cout<<maxd<<' '<<maxx<<endl;
    return 0;
}

客上的三十道题目做完了,加起来差不多做了三个下午,真的是应了知乎上不知哪位说的,乙级最难到排序。乙级的题目用来练基础还是很不错的,虽然有的超级水不过就算是基础差点儿的所有的题目也应该是看看有思路的,最起码能拿到大分,乙级的话基本上稍微练练90+是没问题的,应陈越姥姥说的:“设立乙级还是因为有企业需要的,不是所有企业都要算法和英文很厉害的,能敲代码就是好娃”。具体的记不清楚了,大概就是这么个意思,身为一个ACMer感觉到了赤果果的嘲讽,so~,还是好好学英文去打甲级顶级吧

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

思路:

可以递归也可以循环,递归算最简单

代码:

#include <iostream>

using namespace std;
int _3n(int n)
{
    if(n==1) return 0;
    if(n&1) return _3n((n*3+1)/2)+1;
    else return _3n(n/2)+1;
}
int main()
{
    int n;
    cin>>n;
    cout<<_3n(n)<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

1002 写出这个数 (20)

思路:

水题

代码:

#include <bits/stdc++.h>

using namespace std;
string shu[]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu","shi"};
int main()
{
    char ch,num[100];
    int sum=0;
    while(~scanf("%c",&ch)&&ch!='\n')
    {
        sum+=ch-'0';
    }
    sprintf(num,"%d",sum);
    cout<<shu[num[0]-'0'];
    for(int i=1;i<strlen(num);i++)
    {
        cout<<' '<<shu[num[i]-'0'];
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

1003 我要通过!(20)

思路:

字符串题目,一直都是比较弱也是怕的题目比较考验细心和耐心,这个要注意到题目上递推的关系

代码:

#include <bits/stdc++.h>

using namespace std;
int judge_1(char s[])
{
    int l=strlen(s),p=1,t=1;
    for(int i=0;i<l;i++)
    {
        if(!(s[i]=='P'||s[i]=='A'||s[i]=='T'))
            return 0;
        if(s[i]=='P')
        {
            if(p) p=0;
            else return 0;
        }
        if(s[i]=='T')
        {
            if(t) t=0;
            else return 0;
        }
    }
    return 1;
}
int judge_2(char s[])
{
    int l=strlen(s),p,t;
    for(int i=0;i<l;i++)
    {
        if(s[i]=='P') p=i;
        if(s[i]=='T') t=i;
    }
    if(t-p>1&&p*(t-p-1)==l-t-1)
    {
        return 1;
    }
    else return 0;
}
int judge(char s[])
{
    return (judge_1(s)&&judge_2(s));
}
int main()
{
    int n;
    char s[105];
    cin>>n;
    while(n--)
    {
        scanf("%s",s);
        if(judge(s))
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

1004 成绩排名 (20)

思路:

直接比较

代码:

#include <iostream>

using namespace std;

int main()
{
    string name_max,xh_max,name_min,xh_min,s,s2;
    int max=-1,min=999,ans,n;
    cin>>n;
    while(n--)
    {
        cin>>s>>s2>>ans;
        if(ans>max)
        {
            max=ans;
            name_max=s;
            xh_max=s2;
        }
        if(ans<min)
        {
            min=ans;
            name_min=s;
            xh_min=s2;
        }
    }
    cout<<name_max<<' '<<xh_max<<endl<<name_min<<' '<<xh_min<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

1005 继续(3n+1)猜想 (25)

思路:

暴力

代码:

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
map<int ,int>mp;
int _3n(int n)
{
    if(n==1) return 1;
    if(n&1)
    {
        if(!mp[(n*3+1)/2]) mp[(n*3+1)/2]=_3n((n*3+1)/2);
        return mp[(n*3+1)/2]+1;
    }
    else
    {
        if(!mp[n/2]) mp[n/2]=_3n(n/2);
        return mp[n/2]+1;
    }
}
int main()
{
    int n,a[105],flag=1;

    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        mp[a[i]]=0;
    }
    sort(a,a+n);
    for(int i=n-1;i>=0;i--)
    {
        _3n(a[i]);
    }
    for(map<int,int>::reverse_iterator it=mp.rbegin();it!=mp.rend();it++)
    {
        //cout<<"key:"<<it->first<<' '<<"ve:"<<it->second<<endl;
        if(it->second==0)
        {
            if(flag)
            {
                cout<<it->first;
                flag=0;
            }
            else
            {
                cout<<' '<<it->first;
            }
        }
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

1006 换个格式输出整数 (15)

思路:

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    char s[5];
    cin>>n;
    sprintf(s,"%03d",n);
    for(int i=0;i<s[0]-'0';i++) printf("B");
    for(int i=0;i<s[1]-'0';i++) printf("S");
    for(int i=0;i<s[2]-'0';i++) printf("%d",i+1);
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

1007. 素数对猜想 (20)

思路:

代码:

#include <bits/stdc++.h>

using namespace std;
int judeg(int n)
{
    for(int i=2;i<sqrt(n)+1;i++)
    {
        if(n%i==0) return 0;
    }
    return 1;
}
void init(int a[],int n)
{
    int k=0;
    for(int i=2;i<=n+1;i++)
    {
        if(judeg(i)) a[k++]=i;
    }
}
int main()
{
    int n,a[10005],sum=0;
    cin>>n;
    init(a,n);
    for(int i=0;a[i+1]<=n;i++)
    {
        if(a[i+1]-a[i]==2) sum++;
    }
    cout<<sum<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

1008 数组元素循环右移问题 (20)

思路:

循环输出,不用储存

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,k,a[105];
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=n-(k%n);i<2*n-(k%n);i++)
    {
        printf("%d%c",a[i%n],i==2*n-(k%n)-1?'\n':' ');
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1009 说反话 (20)

思路:

还是水,用一个string的栈存然后输出

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    stack<string>st;
    string s;
    while(cin>>s) st.push(s);
    cout<<st.top();st.pop();
    while(!st.empty())
    {
        cout<<' '<<st.top();
        st.pop();
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

1010 一元多项式求导 (25)

思路:

比较坑,题目上说的不清晰,输入的系数和质数都为0时什么都不输出,如果输入是空输出“0 0”。

代码:

#include <bits/stdc++.h>

using namespace std;
int flag=0;
void fx(int a,int b)
{
    if(a*b) 
    {   
        if(flag) cout<<' '; flag=1;
        printf("%d %d",a*b,b-1);
    }
}
int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        fx(a,b);
    }
    if(!flag) printf("0 0\n");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

1011 A+B和C (15)

思路:

水题不用思路

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long a,b,c;
    int n;cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld%lld%lld",&a,&b,&c);
        if((a+b)>c)
            printf("Case #%d: true\n",i);
        else
            printf("Case #%d: false\n",i);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

1012 数字分类 (20)

思路:

按照要求分类就好,比较繁琐,写了无数if -_-!

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;cin>>n;
    int a1=0,s1=0,a2=0,s2=0,a3=0,s3=0,a4=0,s4=0,a5=-0x3f3f3f3f,s5=0;
    while(n--)
    {
        int ans;
        cin>>ans;
        switch(ans%5)
        {
        case 0:
            if(!(ans%2))
            {
                a1+=ans;
                s1++;
            }
            break;
        case 1:
            if(!(s2%2))
                a2+=ans;
            else
                a2-=ans;
            s2++;
            break;
        case 2:
            s3++;
            break;
        case 3:
            a4+=ans;
            s4++;
            break;
        case 4:
            a5=max(a5,ans);
            s5++;
            break;
        }
    }
    if(!s1)
        cout<<'N'<<' ';
    else
        cout<<a1<<' ';
    if(!s2)
        cout<<'N'<<' ';
    else
        cout<<a2<<' ';
    if(!s3)
        cout<<'N'<<' ';
    else
        cout<<s3<<' ';
    if(!s4)
        cout<<'N'<<' ';
    else
        printf("%.1lf ",a4*1.0/s4);
    if(!s5)
        cout<<'N'<<endl;
    else
        cout<<a5<<endl;

    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

1013 数素数 (20)

思路:

打表存1w个素数,筛法打表,普通方法不知道超不超,然后输出就好,隔是个要换行一次。

代码:

#include <bits/stdc++.h>

using namespace std;
int a[104799]={0},su[10005];
void init()
{
    int k=0;
    a[0]=a[1]=1;
    for(int i=2;i<104799&&k<10005;i++)
    {
        if(!a[i])
        {
            su[k]=i;
            k++;
            for(int j=i+i;j<104799;j+=i)
            {
                a[j]=1;
            }
        }
    }
}

int main()
{
    init();
    int n,m;
    cin>>n>>m;
    for(int i=n;i<=m;i++)
    {
        if((i-n+1)%10==0||i==m)
            printf("%d\n",su[i-1]);
        else
            printf("%d ",su[i-1]);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

1014 福尔摩斯的约会 (20)

思路:

这个题略坑,刚开始理解错了两次都以为是顺序往下找相同的一对,结果只用两行上下对比是不是相同就行了。然后就是注意题目上的什么大写字母,字母,数字分清每一个要找那种相同的就行了

代码:

#include <bits/stdc++.h>

using namespace std;
string DAY[7]={"MON","TUE","TUE","THU","FRI","SAT","SUN"};
int main()
{
    char s[200],s2[100];
    scanf("%s",s);
    int l=strlen(s),l2,flag=0,hh,mm,day;
    scanf("%s",s2);
    l2=strlen(s2);
    map<char, int>mp;
    for(int i=0;i<l;i++)
        mp[s[i]]=1;
    for(int i=0;i<l2;i++)
    {
        if(s2[i]==s[i])
        {
            if(!flag&&s2[i]>='A'&&s2[i]<='Z')
            {
                flag=1;
                day=s2[i]-'A';
            }
            else if(flag&&(s2[i]>='A'&&s2[i]<='N'||s2[i]>='0'&&s2[i]<='9'))
            {
                if(s[i]>='0'&&s[i]<='9')
                    hh = s2[i]-'0';
                else
                    hh = s2[i]-'A'+10;
                break;
            }
        }
    }
    scanf("%s",s);
    l=strlen(s);
    scanf("%s",s2);
    l2=strlen(s2);
    mp.clear();
    for(int i=0;i<l;i++)
        mp[s[i]]=1;
    for(int i=0;i<l2;i++)
    {
        if(s2[i]==s[i]&&(s2[i]>='A'&&s2[i]<='Z'||s2[i]>='a'&&s2[i]<='z'))
        {
            mm=i;
            break;
        }
    }
    cout<<DAY[day];
    printf(" %02d:%02d\n",hh,mm);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

1015 德才论 (25)

思路:

排序的题目,不过条件太多并且麻烦需要细心的分类和写cmp函数,定义一个结构体储存一个人的所有信息,只要分类和cmp函数没搞错,就直接用sort排序输出就行了。

代码:

#include <bits/stdc++.h>

using namespace std;
struct node{
    int id,de,ca,lei,zong;
};
int cmp(node a,node b)
{
    if(a.lei==b.lei)
    {
        if(a.zong==b.zong)
        {
            if(a.de==b.de)
                return a.id<b.id;
            return a.de>b.de;
        }
        return a.zong>b.zong;
    }
    return a.lei<b.lei;
}
int main()
{
    int sum=0,n,l,h;
    node a[100005],ans;
    cin>>n>>l>>h;
    while(n--)
    {
        scanf("%d %d %d",&ans.id,&ans.de,&ans.ca);
        if(ans.ca>=l&&ans.de>=l)
        {
            ans.zong=ans.ca+ans.de;
            if(ans.de>=h)
            {
                if(ans.ca>=h)
                    ans.lei=1;
                else
                    ans.lei=2;
            }
            else
            {
                if(ans.de>=ans.ca)
                    ans.lei=3;
                else
                    ans.lei=4;
            }
            a[sum++]=ans;
        }
    }
    sort(a,a+sum,cmp);
    cout<<sum<<endl;
    for(int i=0;i<sum;i++)
    {
        printf("%d %d %d\n",a[i].id,a[i].de,a[i].ca);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

1016 部分A+B (15)

思路:

意思很简单,只需要找出Da,Db的数量,然后写一个函数能把pa pb算出来直接相加,找Da的时候可以直接写不麻烦,也可以调用STL中的函数count()(又发现一个函数 ヽ(✿゚▽゚)ノ)。

代码:

#include <bits/stdc++.h>

using namespace std;
int px(int dx,int n)
{
    if(n==0) return 0;
    if(n==1) return dx;
    return px(dx,n-1)*10+dx;
}
int main()
{
    char s[100],s2[100],Da,Db;
    scanf("%s %c %s %c",s,&Da,s2,&Db);
    printf("%d\n",px(Da-'0',count(s,s+strlen(s),Da))+px(Db-'0',count(s2,s2+strlen(s2),Db)));
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

1017 A除以B (20)

思路:

大数问题,不过是大数除10以下整数比较容易,模拟手工算,一位一位的算然后存下来就好了。 
牛客过了官网没过,最后发现没有考虑啊a

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char a[1005],q[1005];
    int b,r,ans,j=0,i=0;
    scanf("%s %d",a,&b);
    int l=strlen(a);
    ans=a[0]-'0';i=1;
    do
    {
        if(ans<b&&i<l)
        {
            ans=ans*10+a[i++]-'0';
        }
        q[j++]=ans/b;
        ans%=b;
    }while(i<l);
    for(int i=0;i<j;i++)
        printf("%c",q[i]+'0');
    printf(" %d\n",ans);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

1018 锤子剪刀布 (20)

思路:

按照题意模拟即可(又是一堆if,是我没有get到乙级题的真谛吗 (ಥ _ ಥ))

代码:

#include <bits/stdc++.h>

using namespace std;
struct node
{

    int sheng,fu,ping;
    map<char,int>mp;
    node()
    {
        sheng=fu=ping=0;
    }
};
void input_jcb(node *J)
{
    if(J->mp['B']>=J->mp['C']&&J->mp['B']>=J->mp['J'])
    {
        printf("B");
    }
    else if(J->mp['C']>=J->mp['B']&&J->mp['C']>=J->mp['J'])
    {
        printf("C");
    }
    else if(J->mp['J']>=J->mp['B']&&J->mp['J']>=J->mp['C'])
    {
        printf("J");
    }
}
int main()
{
    int n;
    cin>>n;
    char ch,ch2;
    node *J = new node;
    node *Y = new node;
    while(n--)
    {


        cin>>ch>>ch2;
        if(ch=='B'&&ch2=='B'||ch=='C'&&ch2=='C'||ch=='J'&&ch2=='J')
        {
            J->ping++;
            Y->ping++;
        }
        if(ch=='B'&&ch2=='C')
        {
            J->sheng++;
            Y->fu++;
            J->mp['B']++;
        }
        if(ch=='B'&&ch2=='J')
        {
            Y->sheng++;
            J->fu++;
            Y->mp['J']++;
        }
        if(ch=='C'&&ch2=='J')
        {
            J->sheng++;
            Y->fu++;
            J->mp['C']++;
        }
        if(ch=='C'&&ch2=='B')
        {
            Y->sheng++;
            J->fu++;
            Y->mp['B']++;
        }
        if(ch=='J'&&ch2=='C')
        {
            Y->sheng++;
            J->fu++;
            Y->mp['C']++;
        }
        if(ch=='J'&&ch2=='B')
        {
            J->sheng++;
            Y->fu++;
            J->mp['J']++;
        }
    }
    printf("%d %d %d\n",J->sheng,J->ping,J->fu);
    printf("%d %d %d\n",Y->sheng,Y->ping,Y->fu);
    input_jcb(J);
    cout<<' ';
    input_jcb(Y);
    cout<<endl;
    return 0;
}
##大佬代码果然是  短小快!

#include <iostream>
using namespace std;
int win[3] = {0};
char max3(int *a) {
    char r = 'B';
    if(a[1] > a[0]) {
        a[0] = a[1];
        r = 'C';
    } 
    if(a[2] > a[0]) r = 'J';
    return r;
}
int main() {
    int n;
    char a[105],b[105];
    int awin[3] = {0};
    int bwin[3] = {0};
    cin >> n;     
    for(int i = 0; i < n; i++) {
        cin >> a[i] >> b[i];
        int k = (a[i] - b[i]) * (a[i] - b[i]);
        if(k == 1)
            a[i] == 66 ? win[0]++,awin[0]++ : (win[1]++,bwin[0]++);
        else if(k == 49)
            a[i] == 67 ? win[0]++,awin[1]++ : (win[1]++,bwin[1]++);
        else if(k == 64)
            a[i] == 74 ? win[0]++,awin[2]++ : (win[1]++,bwin[2]++);
        else
            win[2]++;
    }
    cout << win[0] << " " << win[2] << " " << win[1] << endl;
    cout << win[1] << " " << win[2] << " " << win[0] << endl;
    cout << max3(awin) << " " << max3(bwin);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128

1019 数字黑洞 (20)

思路:

经典题,字符串与整型相互转换就好了,可以自己写也可以直接用sscanfsprintf

代码:

#include <bits/stdc++.h>

using namespace std;
int cmp(char a,char b)
{
    return a>b;
}
void zhuanhuan(char s[],int *a,int *b)
{
    sort(s,s+4,cmp);
    sscanf(s,"%d",a);
    sort(s,s+4);
    sscanf(s,"%d",b);
}
int main()
{
    int a=0,b=0,n;
    char s[10];
    cin>>n;
    sprintf(s,"%04d",n);
    zhuanhuan(s,&a,&b);
    while(1)
    {
        printf("%04d - %04d = %04d\n",a,b,a-b);
        if(a-b==6174||a==b)
            break;
        sprintf(s,"%04d",a-b);
        zhuanhuan(s,&a,&b);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

1020 月饼 (25)

思路:

基础贪心,选择性价比最高的

代码:

#include <bits/stdc++.h>

using namespace std;
struct node
{
    double jia,liang,bizhi;
};
int cmp(node a,node b)
{
    return a.bizhi>b.bizhi;
}
int main()
{
    node a[1005];
    int n;
    double d,sum=0;
    cin>>n>>d;
    for(int i=0;i<n;i++)
    {
        cin>>a[i].liang;
    }
    for(int i=0;i<n;i++)
    {
        cin>>a[i].jia;
        a[i].bizhi=a[i].jia/a[i].liang;
    }
    sort(a,a+n,cmp);
    for(int i=0;d;i++)
    {
        if(d>a[i].liang)
        {
            d-=a[i].liang;
            sum+=a[i].jia;
        }
        else
        {
            sum+=d*a[i].bizhi;
            d=0;
        }
    }
    printf("%.2lf\n",sum);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

1021 个位数统计 (15)

思路:

水题,开一个大小10的数组存每一个的次数,数字就是下标

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int mp[10]={0};
    char ch;
    while(~scanf("%c",&ch))
    {
        mp[ch-'0']++;
    }
    for(int i=0;i<10;i++)
    {
        if(mp[i])
            printf("%d:%d\n",i,mp[i]);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

1022 D进制的A+B (20)

思路:

就是考察进制转换 
牛客过了官网没过,没有考虑0的情况

代码:

#include <bits/stdc++.h>

using namespace std;
void my_itoa(int n,int r)
{
    if(n==0)printf("0");
    stack<int>st;
    while(n)
    {
        st.push(n%r);
        n/=r;
    }
    while(!st.empty())
    {
        cout<<st.top();
        st.pop();
    }
}
int main()
{
    int a,b,d;
    cin>>a>>b>>d;
    my_itoa(a+b,d);
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

1023 组个最小数 (20)

思路:

先找出一个非零的最小数字输出,然后将剩下的数字升序输出就好了

代码:

#include <iostream>

using namespace std;

int main()
{
    int mp[10]={0};
    for(int i=0;i<10;i++)
    {
        cin>>mp[i];
    }
    for(int i=1;i<10;i++)
    {
        if(mp[i])
        {
            mp[i]--;
            cout<<i;
            break;
        }
    }
    for(int i=0;i<10;i++)
    {
        if(mp[i])
        {
            for(int j=0;j<mp[i];j++)
                cout<<i;
        }
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

1024 科学计数法 (20)

思路:

看似复杂,只要明白了题目上科学计数法的规则,实际上还是对字符串的处理

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s[10005],ch;
    queue<char>qu;
    int i=0,flag=1;
    while(~scanf("%c",&ch))
    {
        if(ch=='-')cout<<'-';
        else if(ch=='+'||ch=='.');
        else if(ch>='0'&&ch<='9')
        {
            qu.push(ch);
        }
        else
        {
            break;
        }
    }
    int ans;
    cin>>ans;
    flag+=ans;
    if(flag<1)
    {
        for(int i=0; i<=-flag; i++)
        {
            if(i==1)cout<<'.';
            cout<<'0';
        }
        while(!qu.empty())
        {
            cout<<qu.front();
            qu.pop();
        }
    }
    else
    {
        while(!qu.empty())
        {
            if(i==flag)cout<<'.';
            cout<<qu.front();
            qu.pop();
            i++;
        }
        for(; i<flag; i++)
        {
            cout<<'0';
        }
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

1025 反转链表 (25)

思路:

这道题目比较坑卡了我很长时间,刚开始完全就是模拟一个链表出来然后改变指针,写着巨麻烦还wa掉了,后来想到了完全必须要在原来链表上面改动,把新的顺序重新存一遍就好了,反向部分用栈来操作反向,然后用结构体vector来存。写完过来但是也挺长的,看来大佬代码恍然大悟,只用存“指针”部分就行了,然后用c++提供的反向库函数来操作,代码果断比我的短一半。 
有一个坑点,输入的节点不一定全用上会有部分不出现在链表中的废节点,所以需要重新计算N的数量 
牛客过了官网没过,原因没考虑全部反转和反转后剩一个的情况

代码:

#include <bits/stdc++.h>

using namespace std;
struct node
{
    int data,next;
} mp[100005];
struct node2
{
    int id,data,next;
};
int main()
{
    int head,n,k,ans,flag=1;
    vector<node2>ve;
    stack<node2>st;
    cin>>head>>n>>k;
    for(int i=0; i<n; i++)//储存节点
    {
        scanf("%d",&ans);
        scanf("%d %d",&mp[ans].data,&mp[ans].next);
    }
    ans=head;
    n=1;
    while(1)
    {
        if(mp[ans].next!=-1)//遍历链表统计N
        {
            ans=mp[ans].next;
            n++;
        }
        else
        {
            break;
        }
    }
    do
    {
            for(int i=0; i<k; i++)
            {
                node2 p;
                p.data=mp[head].data;
                p.id=head;
                p.next=mp[head].next;
                st.push(p);//存入栈中
                head=mp[head].next;
            }
            while(!st.empty())
            {
                ve.push_back(st.top());//反转放入vector
                st.pop();
            }
            n-=k;
    }while(n>=k);
    while(n)//n>0且不足k个的直接放入vector
    {
        node2 p;
        p.data=mp[head].data;
        p.id=head;
        p.next=mp[head].next;
        ve.push_back(p);
        if(p.next==-1)break;
        else head=p.next;
    }
    for(int i=0;i<ve.size();i++)//输出
    {
        printf("%05d %d ",ve[i].id,ve[i].data);
        if(i==ve.size()-1)
        {
            printf("-1\n");
        }
        else
        {
            printf("%05d\n",ve[i+1].id);
        }
    }
    return 0;
}
/**
00100 4 4
00100 3 12345
12345 1 12121
12121 5 13131
13131 6 -1

00100 1 1
00100 1 -1
*/


/** 
大佬的简短代码
#include <iostream>
#include <algorithm>
using namespace std;

int main(int argc, const char * argv[])
{
    int FirstAdd,Total,K,key;
    cin>>FirstAdd>>Total>>K;
    key=FirstAdd;
    int temp,Data[100005],Next[100005];
    int i,j;
    for(i=0;i<Total;i++)
    {
        cin>>temp>>Data[temp]>>Next[temp];
    }
    int Order[100005],count=0;
    while(key!=-1)
    {
        Order[count]=key;
        key=Next[key];
        count++;
    }
    int round=count/K;
    for(i=j=0;j<round;i=i+K,j++)
    {
        reverse(Order+i, Order+i+K);
    }
    for(i=0;i<count-1;i++)
    {
        printf("%05d %d %05d\n",Order[i],Data[Order[i]],Order[i+1]);
    }
    printf("%05d %d -1\n",Order[i],Data[Order[i]]);
    return 0;
}
 */     

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129

1026 程序运行时间(15)

思路:

简单水题

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a,b,time;
    cin>>a>>b;
    time=(b-a)*1.0/100+0.5;
    int m=time/60;
    int h=m/60;
    m%=60;time%=60;
    printf("%02d:%02d:%02d\n",h,m,time);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

1027 打印沙漏(20)

思路:

首先计算最多可以打印多大的沙漏,然后控制好每一行打印多少个空格和字符就好

代码:

#include <bits/stdc++.h>

using namespace std;
void out_char(int n,char ch)
{
    while(n--)printf("%c",ch);
}
int main()
{
    int n,i,sum=0;
    char ch;
    scanf("%d %c",&n,&ch);
    for(i=1;;i++)//计算最大行数
    {
        sum+=i*2-1;
        if(sum*2-1>n)
        {i--;sum=0;break;}
    }
    for(int j=0;j<i-1;j++)//打印上半个三角
    {
        out_char(j,' ');
        out_char((i-j)*2-1,ch);
        sum+=(i-j)*2-1;
        cout<<endl;
    }
    for(int j=i-1;j>=0;j--)//下半个三角
    {
        out_char(j,' ');
        out_char((i-j)*2-1,ch);
        sum+=(i-j)*2-1;
        cout<<endl;
    }
    cout<<n-sum<<endl;//输出剩余
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

1028 人口普查(20)

思路:

把日期转换成int类型直接判断范围和相减判断年龄大小就可以了 
牛客过了,官网一个wa一个pe,wa在于年龄边界。pe是因为如果没有符合条件的则只输出0。

代码:

#include <bits/stdc++.h>
#define XDATE 20140906
#define MDATA 18140906
using namespace std;

int main()
{
    string mins,maxs,ans;
    int minn=XDATE,maxx=0,y,m,d,data,sum=0;
    int n;cin>>n;
    while(n--)
    {
        cin>>ans;
        scanf("%d/%d/%d",&y,&m,&d);
        data=y*10000+m*100+d;
        if(data<=XDATE&&data>=MDATA)
        {
            sum++;
            if(data>maxx)
            {maxx=data;maxs=ans;}
            if(data<minn)
            {minn=data;mins=ans;}
        }
    }
    if(sum)
        cout<<sum<<' '<<mins<<' '<<maxs<<endl;
    else
        cout<<0<<endl;
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

1029 旧键盘 (20)

思路:

先储存键盘输出的字符,然后将应该打出的字符进行比较,如果没有输出过且没有被标记就变成大写输出来,同时标记一下这个字母的大小写。 
牛客过了官网没过,原因想当然的认为了范围直接用else来做另一种情况少考虑的一种情况。要把每种情况都用if判断出来

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s[100],s2[100];
    set<char>se;
    map<char ,int >flag;
    gets(s);gets(s2);
    int l=strlen(s2);
    for(int i=0;i<l;i++)
    {
        se.insert(s2[i]);
    }
    l=strlen(s);
    for(int i=0;i<l;i++)
    {
        if(!se.count(s[i])&&!flag[s[i]])
        {
            flag[s[i]]=1;
            if(s[i]>='a'&&s[i]<='z')
            {
                printf("%c",s[i]-32);
                flag[s[i]-32]=1;
            }
            else if(s[i]>='A'&&s[i]<='Z')
            {
                flag[s[i]+32]=1;
                printf("%c",s[i]);
            }
            else
            {
                printf("%c",s[i]);
            }

        }
    }
    cout<<endl;
    return 0;
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

1030 完美数列(25)

思路:

想了一会儿发现并没有什么好的思路,那就暴力找+二分优化,复杂度nlngn也可以接受,不过每次写二分的时候都会在边界问题上纠结好久,还是内功太差

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n,p,a[100005],maxx=0;
    scanf("%lld %lld",&n,&p);
    for(int i=0;i<n;i++)
        scanf("%lld",&a[i]);
    sort(a,a+n);
    for(int i=0;i<n-maxx;i++)
    {
        int ans=upper_bound(a+i,a+n,a[i]*p)-a;//因为查找到的是第一个大于查找值的地址,它的下一个就是<=a[i]*p了
        if(ans-i>maxx)//然后ans-i就是第i个到ans个之间的数量
            maxx=ans-i;
    }
    cout<<maxx<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

1031 查验身份证(15)

思路:

按照题意判断即可

代码:

#include <bits/stdc++.h>

using namespace std;
int quan[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char jiao[]={'1','0','X','9','8','7','6','5','4','3','2'};
int judeg(char s[])
{
    int sum=0;
    for(int i=0;i<17;i++)
        if(s[i]<'0'||s[i]>'9')
            return 0;
        else
        {
            sum+=(s[i]-'0')*quan[i];
        }
    sum%=11;
    if(jiao[sum]==s[17])
        return 1;
    return 0;
}
int main()
{
    char id[20];
    int n,flag=1;
    cin>>n;getchar();
    while(n--)
    {
        scanf("%s",id);
        if(!judeg(id))
        {
            flag=0;
            printf("%s\n",id);
        }
    }
    if(flag)printf("All passed\n");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

1032 挖掘机技术哪家强(20)

思路:

存下每个学校id,然后遍历一遍找最大就好。

代码:

#include <bits/stdc++.h>

using namespace std;
int xue[100005];
int main()
{
    set<int>se;
    int n,xveid,maxx=0;
    cin>>n;
    while(n--)
    {
        int id,fen;
        scanf("%d %d",&id,&fen);
        se.insert(id);
        xue[id]+=fen;
    }
    for(set<int>::iterator it=se.begin();it!=se.end();it++)
    {
        if(xue[*it]>maxx)
        {
            xveid=*it;
            maxx=xue[*it];
        }
    }
    cout<<xveid<<' '<<maxx<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

1033 旧键盘打字(20)

思路:

存下坏的键,然后将要打出的字符一个个判断如果没坏就输出,否则不操作 
牛客过官网wa一组,注意最后一句保证第二组非空,所以还有一种第行为空的情况

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int mp[300]={0},l;
    char s[40],s2[100005];
    gets(s);
    gets(s2);
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]>='A'&&s[i]<='Z')
        {
            mp[s[i]]=1;
            mp[s[i]+32]=1;
        }
        else
            mp[s[i]]=1;
    }
    l=strlen(s2);
    for(int i=0;i<l;i++)
    {
        if(s2[i]>='A'&&s2[i]<='Z')
        {
            if(!mp['+']&&!mp[s2[i]])
                printf("%c",s2[i]);
        }
        else
        {
            if(!mp[s2[i]])
                printf("%c",s2[i]);
        }
    }
    cout<<endl;
    return 0;
}
/**

7+IE.
7_This_is_a_test.
*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

1034 有理数四则运算(20)

思路:

用四个变量分别储存分子分明,模拟分数的计算方法,然后写好化简的函数将数字转换成最简输出 
牛客过官网wa,原因忘记考虑两个都为负数的情况

代码:

#include <bits/stdc++.h>
#define ull  long long
using namespace std;
ull gcd(ull a,ull b)
{
    return !(a%b)?b:gcd(b,a%b);
}
void huajian(ull fz,ull fm)
{
    ull gy,flag=0;
    if(fz<0||fm<0)
    {
        if(!(fz<0&&fm<0))
        {
            printf("(-");
            flag=1;
        }
        if(fz<0)fz=-fz;
        if(fm<0)fm=-fm;
    }
    gy=gcd(fz,fm);
    fz/=gy;fm/=gy;

    if(!fz)
    {
        printf("0");
    }
    else if(fm==1)
    {
        printf("%lld",fz);
    }
    else if(fz>fm)
    {
        printf("%lld %lld/%lld",fz/fm,fz%fm,fm);
    }
    else
        printf("%lld/%lld",fz%fm,fm);
    if(flag)
        printf(")");
}
int main()
{
    ull a,b,c,d;
    scanf("%lld/%lld %lld/%lld",&a,&b,&c,&d);
    huajian(a,b);
    printf(" + ");
    huajian(c,d);
    printf(" = ");
    huajian(a*d+b*c,b*d);
    printf("\n");
    huajian(a,b);
    printf(" - ");
    huajian(c,d);
    printf(" = ");
    huajian(a*d-b*c,b*d);
    printf("\n");
    huajian(a,b);
    printf(" * ");
    huajian(c,d);
    printf(" = ");
    huajian(a*c,b*d);
    printf("\n");
    huajian(a,b);
    printf(" / ");
    huajian(c,d);
    printf(" = ");
    if(!c)
        printf("Inf");
    else
        huajian(a*d,b*c);
    printf("\n");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

1035 插入与归并(25)

思路:

没有什么好的思路,用插入和归并排一遍对比中间过程,如果遇到相同的就输出下一次排序的序列,当然想要A掉这一题还要会这两种排序,不会的话。。。跟我一样现学去吧。╮(╯_╰)╭ 
牛客过了官网没过,5号到7号卡了三天,代码从95行写到39行,能用别的方法还是不要模拟的好,模拟细节太多了╮(╯_╰)╭,现在对归并理解的又深了

代码:

#include <bits/stdc++.h>

using namespace std;
int yuanshi[105],mubiao[105],n,ans[105],flag=0;
int juedg(int a[],int b[],int l)
{
    for(int i=0;i<l;i++)
    {
        if(a[i]!=b[i])
            return 0;
    }
    return 1;
}
void mergepass(int a[],int b[],int k,int n)
{
    if(flag==2)return ;
    int i;
    for(i=0;i<=n-k*2;i+=k*2)
    {
        merge(a+i,a+i+k,a+i+k,a+i+2*k,b+i);
    }
    if(n<k*2)
        merge(a,a+k,a+k,a+n,b);
    if(flag)
    {
        for(int i=0;i<n;i++)
        {
            printf("%d%c",b[i],i==n-1?'\n':' ');
        }
        flag=2;
        return ;
    }
    if(juedg(mubiao,b,n))
    {
        printf("Merge Sort\n");
        flag=1;
    }
}
void input()
{
        for(int i=0;i<n;i++)
        scanf("%d",&yuanshi[i]);
    for(int i=0;i<n;i++)
        scanf("%d",&mubiao[i]);
}
int main()
{

    cin>>n;
    input();
    copy(yuanshi,yuanshi+n,ans);
    for(int i=1;i<n;i++)//插入排序
    {
        if(ans[i]<ans[i-1])
        {
            int p=upper_bound(ans,ans+i,ans[i])-ans,q=ans[i];
            copy(ans+p,ans+i,ans+p+1);
            ans[p]=q;
            if(flag)
            {
                for(int i=0;i<n;i++)
                {
                    printf("%d%c",ans[i],i==n-1?'\n':' ');
                }
                return 0;
            }
            if(juedg(mubiao,ans,n))
            {
                printf("Insertion Sort\n");
                flag=1;
            }
        }
    }
    int k=1;//归并排序
    while(k<n)
    {

        mergepass(yuanshi,ans,k,n);
        k<<=1;
        mergepass(ans,yuanshi,k,n);
        k<<=1;
        if(flag==2)
            break;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87

1036 跟奥巴马一起编程(15)

思路:

水题一道,四舍五入的时候要细心

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    char ch;
    cin>>n>>ch;
    for(int i=0;i<n;i++)
        cout<<ch;
    cout<<endl;
    for(int j=0;j<(int)(n/2.0+0.5)-2;j++)
    {
        for(int i=0;i<n;i++)
            printf("%c",i==0||i==n-1?ch:' ');
        cout<<endl;
    }
    for(int i=0;i<n;i++)
        cout<<ch;
    cout<<endl;
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

1037 在霍格沃茨找零钱(20)

思路:

先全部转换成最小的单位,然后相减在转换回来,另类的进制转换

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int G,S,K,GSK_sum=0,g,s,k,gsk_sum=0;
    scanf("%d.%d.%d %d.%d.%d",&g,&s,&k,&G,&S,&K);
    GSK_sum=(G*17+S)*29+K;
    gsk_sum=(g*17+s)*29+k;
    GSK_sum-=gsk_sum;
    if(GSK_sum<0){GSK_sum=-GSK_sum;printf("-");}
    K=GSK_sum%29;
    S=(GSK_sum/29)%17;
    G=(GSK_sum/29)/17;
    printf("%d.%d.%d\n",G,S,K);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

1038 统计同成绩学生(20)

思路:

下标法标记,水题

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int mp[105]={0},ans;
    int n;cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&ans);
        mp[ans]++;
    }
    cin>>n;
    while(n--)
    {
        scanf("%d",&ans);
        printf("%d%c",mp[ans],n?' ':'\n');
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

1039 到底买不买(20)

思路:

存下每个需要的珠子的数量,然后遍历另一个串,查看是否都有,如果所需要珠子都有输出l-l2,否则遍历标记数组统计有多少没有,(刚开始我还想了有没有需要的珠子有多出的情况这时候多余的珠子就不再是l-l2,后来测试发现没有这样的数据,这个问题考不考虑都能A)

代码:

#include <bits/stdc++.h>

using namespace std;
int judeg(int a[])
{
    int sum=0;
    for(int i=0;i<300;i++)
        if(a[i])sum+=a[i];
    return sum;
}
int main()
{
    int mp[300]={0},sum=0,l,l2;
    char s[1005],s2[1005];
    scanf("%s %s",s,s2);
    l=strlen(s);sum=l2=strlen(s2);
    for(int i=0;i<l2;i++)
    {
        mp[s2[i]]++;
    }
    for(int i=0;i<l;i++)
    {
        if(mp[s[i]])
        {
            sum--;
            mp[s[i]]--;
        }
    }
    if(!judeg(mp))
    {
        printf("Yes %d\n",l-l2+sum);
    }
    else
    {
        printf("No %d\n",judeg(mp));
    }
    return 0;
}
/**
ppRYYGrrYBR2258
YrR8RrY
*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

1040 有几个PAT(25)

思路:

这个思路有点儿动态规划的感觉,每次遇到P将数量++,碰到A的时候将pa数量加上P的数量(就是这个A可以组合出来的Pa的数量),每碰到一个T的时候将PA的数量累加的SUM上,这是这个T可以组合出来的所有PAT,然后每一步判断是否要取余,最后答案就出来了

代码:

#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;

int main()
{
    char ch;
    int p=0,a=0,sum=0;
    while(~scanf("%c",&ch))
    {
        if(ch=='P')p++;
        else if(ch=='A')a+=p;
        else if(ch=='T')
        {
            sum+=a;
            if(sum>MOD)
                sum%=MOD;
        }
    }
    cout<<sum<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

1041 考试座位号(15)

思路:

水题

代码:

#include <bits/stdc++.h>

using namespace std;
struct node
{
    string id;
    int zw;
}a[1005];
int main()
{
    int n,sj,zw;
    string s;
    cin>>n;
    while(n--)
    {
        cin>>s>>sj>>zw;
        a[sj].id=s;
        a[sj].zw=zw;
    }
    cin>>n;
    while(n--)
    {
        cin>>sj;
        cout<<a[sj].id<<' '<<a[sj].zw<<endl;
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

1042 字符统计(20)

思路:

水题

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int mp[30]={0},maxx=0;
    char ch;
    while(scanf("%c",&ch),ch!='\n')
    {
        if(ch>='A'&&ch<='Z')
            ch+=32;
        if(ch>='a'&&ch<='z')
            mp[ch-'a']++;
    }
    for(int i=0;i<26;i++)
    {
        if(mp[i]>mp[maxx])
            maxx=i;
    }
    printf("%c %d\n",maxx+'a',mp[maxx]);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

1043 输出PATest(20)

思路:

水,照着题目意思来就好

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int mp[30]={0},maxx=0;
    char ch;
    while(scanf("%c",&ch),ch!='\n')
    {
        if(ch>='A'&&ch<='Z')
            ch+=32;
        if(ch>='a'&&ch<='z')
            mp[ch-'a']++;
    }
    for(int i=0;i<26;i++)
    {
        if(mp[i]>mp[maxx])
            maxx=i;
    }
    printf("%c %d\n",maxx+'a',mp[maxx]);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

1044 火星数字(20)

思路:

这个题目的意思实现起来并不难,不过被坑了好久,我是被坑在高低位的火星文是不一样的,如果只有一位火星文的时候我默认当作低位来算了,事实上只有一位要判断是高位还是低位在计算。

代码:

#include <bits/stdc++.h>

using namespace std;
string _12q[]= {"tret", "jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string _12h[]= {"tret", "tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};

int stio(string ans)
{
    for(int i=0; i<13; i++)
    {
        if(_12q[i]==ans)
            return i;
    }
    for(int i=0; i<13; i++)
    {
        if(_12h[i]==ans)
            return i*13;
    }
    return 0;
}
int main()
{
    int n,num,t=0;
    cin>>n;getchar();
    cin.clear();
    while(n--)
    {
        string ans;
        getline(cin,ans);
        stringstream ss(ans);
        if(ans[0]<='9'&&ans[0]>='0')
        {
            ss>>num;
            if(num<13)
                cout<<_12q[num]<<endl;
            else
            {
                t=num%13;
                num/=13;
                cout<<_12h[num];
                if(t)
                    cout<<' '<<_12q[t];
                cout<<endl;
            }

        }
        else
        {
            ss>>ans;
            t=0;
            num=stio(ans);
            while(ss>>ans)
            {t = stio(ans);}
            cout<<num+t<<endl;
            ss.clear();
        }
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

1045 快速排序(25)

思路:

我的思路比较low 一个原数组一个排好序的数组,存下左边的最大值,右边排好序保持第一个就是原数组中右边的最小值,这样比下去把满足条件的存下来。AC后看其他人代码发现,还是没有找到排序的精髓,主元的位置就是拍好的位置,所以说遍历两个数字碰到相同的且这个数字是左边最大的就是主元(这样说不知道清不清楚意思),按照这个思路代码就简单好多了,果然还是思路最重要。因为数据比较大直接硬算会超时,我那个方法算是一种取巧的硬算。

代码:

#include <bits/stdc++.h>
#define INF 1000000005
using namespace std;

int main()
{
    int n,a[100005],j=0,b[100005],maxx=-1,minn=0;
    map<int,int>mp;
    vector<int>ve;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        b[i]=a[i];
        mp[a[i]]++;
    }
    b[n]=INF;mp[INF]=1;
    sort(b,b+n+1);
    for(int i=0;i<n;i++)
    {
        mp[a[i]]--;
        while(!mp[b[maxx]]) maxx++;
        //printf("%d %d %d---max==%d\n",minn,a[i],b[maxx],maxx);
        if(a[i]>minn&&a[i]<b[maxx])
        {
            ve.push_back(a[i]);
        }
        minn=max(a[i],minn);
    }
    sort(ve.begin(),ve.end());
    cout<<ve.size()<<endl;
    if(ve.size()) cout<<ve[0];
    for(int i=1;i<ve.size();i++)
    {
        printf(" %d",ve[i]);
    }
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

1046 划拳(15)

思路:

水题,直接比大小

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a,b,c,d,n,j=0,y=0;
    cin>>n;
    while(n--)
    {
        cin>>a>>b>>c>>d;
        int ans=a+c;
        if(ans==b&&ans!=d) y++;
        else if(ans!=b&&ans==d) j++;
    }
    cout<<j<<' '<<y<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

1047. 编程团体赛(20)

思路:

开一个下标为队伍的数组,直接累加储存最大值即可。

代码:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,d,f,dui[1005]={0},maxd=0,maxx=0;
    cin>>n;
    while(n--)
    {
        scanf("%d-%*d %d",&d,&f);
        dui[d]+=f;
        if(dui[d]>maxx)
        {
            maxd=d;maxx=dui[d];
        }
    }
    cout<<maxd<<' '<<maxx<<endl;
    return 0;
}

本文标签: 题解PAT