HDU 2021 - 2030【入门】

题目链接:http://acm.hdu.edu.cn/listproblem.php?vol=11

冲冲冲!


HDU 2021

有一点贪心的感觉,就是尽量选择人民币面值大的,这样就可以保证使用的人民币数量最少

#include<stdio.h>
#include<math.h>
int read()
{
    char c;
    int x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return x*res;
}
int main()
{
    int n;
    int f[]={0,100,50,10,5,2,1};
    while(~scanf("%d",&n)&&n){
        int ans=0,a;
        while(n--){
            a=read();
            for(int i=1;i<=6;i++){
                ans+=a/f[i];
                a%=f[i];
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


HDU 2022

注意这里x y初始化为1的原因,如果n=m=1 并且唯一的一个人是0分,那么我这代码不初始化就是错的(但是杭电没有这个测试点,照样AC)

#include<stdio.h>
#include<math.h>
int read()
{
    char c;
    int x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return x*res;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        int x=1,y=1,a,p=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                a=read();
                if(abs(p)<abs(a)){
                    x=i;
                    y=j;
                    p=a;
                }
            }
        }
        printf("%d %d %d\n",x,y,p);
    }
    return 0;
}


HDU 2023

对数组的初等掌握,注意看清题目,每个样例要额外输出一个空行,不然PE欢迎你~

#include<stdio.h>
#include<math.h>
int read()
{
    char c;
    int x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return x*res;
}
int main()
{
    int n,m;
    int f[55][10];
    double ave[6];
    while(~scanf("%d%d",&n,&m)){
        memset(f,0,sizeof f);
        memset(ave,0,sizeof ave);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                f[i][j]=read();
                f[i][8]+=f[i][j];
                ave[j]+=f[i][j];
            }
        }
        for(int i=1;i<=n;i++){
            printf("%.2lf%c",f[i][8]*1.0/m,i==n?'\n':' ');
        }
        for(int i=1;i<=m;i++){
            printf("%.2lf%c",ave[i]*=1.0/n,i==m?'\n':' ');
        }
        int ans=0;
        for(int i=1;i<=n;i++){
            int flag=1;
            for(int j=1;j<=m;j++){
                if(f[i][j]<ave[j]){
                    flag=0;
                    break;
                }
            }
            if(flag) ans++;
        }
        printf("%d\n\n",ans);
    }
    return 0;
}


HDU 2024

这题出的还是有点问题的,关键字不是合法表示符吧?这里没有考虑

#include<stdio.h>
#include<math.h>
int read()
{
    char c;
    int x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return x*res;
}
int main()
{
    int n;
    char s[100];
    while(~scanf("%d",&n)){
        getchar();
        while(n--){
            gets(s);
            int flag=1,len=strlen(s);
            if((s[0]<'a'||s[0]>'z')&&(s[0]<'A'||s[0]>'Z')&&s[0]!='_'){
                flag=0;
            }
            else{
                for(int i=1;i<len;i++){
                    if((s[i]<'a'||s[i]>'z')&&(s[i]<'A'||s[i]>'Z')&&s[i]!='_'&&(s[i]<'0'||s[i]>'9')){
                        flag=0;
                        break;
                    }
                }
            }
            printf("%s\n",flag?"yes":"no");
        }

    }
    return 0;
}


HDU 2025

实际要插入的话,还是有一丢丢小麻烦的,但是根本不需要嘛~

#include<stdio.h>
#include<math.h>
int read()
{
    char c;
    int x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return x*res;
}
int main()
{
    int n;
    char s[100];
    while(~scanf("%s",s)){
        int len=strlen(s);
        char m=0;
        for(int i=0;i<len;i++){
            if(m<s[i]) m=s[i];
        }
        for(int i=0;i<len;i++){
            printf("%c",s[i]);
            if(s[i]==m) printf("(max)");
        }
        printf("\n");
    }
    return 0;
}


HDU 2026

这题学会找规律,每个单词之间会有空格,通过这个来标记就好办了

#include<stdio.h>
#include<math.h>
int read()
{
    char c;
    int x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return x*res;
}
int main()
{
    int n;
    char s[105];
    while(gets(s)){
        int len=strlen(s);
        int flag=1;
        for(int i=0;i<len;i++){
            if(flag){
                if(s[i]>='a'&&s[i]<='z'){
                    s[i]+='A'-'a';
                }
                flag=0;
            }
            if(s[i]==' ') flag=1;
        }
        printf("%s\n",s);
    }
    return 0;
}


HDU 2027

还是看题的问题,不然PE,其他问题不大

#include<stdio.h>
#include<math.h>
int read()
{
    char c;
    int x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return x*res;
}
int main()
{
    int n;
    char s[105];
    int ans[6];
    char c[7]={' ','a','e','i','o','u'};
    while(~scanf("%d",&n)){
        getchar();
        while(n--){
            memset(ans,0,sizeof ans);
            gets(s);
            int len=strlen(s);
            for(int i=0;i<len;i++){
                if(s[i]=='a') ans[1]++;
                else if(s[i]=='e') ans[2]++;
                else if(s[i]=='i') ans[3]++;
                else if(s[i]=='o') ans[4]++;
                else if(s[i]=='u') ans[5]++;
            }
            for(int i=1;i<=5;i++){
                printf("%c:%d\n",c[i],ans[i]);
            }
            if(n>0) printf("\n");
        }
    }
    return 0;
}


HDU 2028

这题究极坑,虽然我一开始意识到了中间可能会超过32位,但是我以为long long可以幸免,没想到是这种操作…令人窒息的操作

#include<stdio.h>
#include<math.h>
int read()
{
    char c;
    int x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return x*res;
}
int gcd(int x,int y)
{
    return y==0?x:gcd(y,x%y);
}
int main()
{
    int n;
    while(~scanf("%d",&n)){
        int ans=read();
        if(n==1){ printf("%d\n",ans); continue;}
        while(--n){
            int t=read();
            ans=ans/gcd(ans,t)*t;
        }
        printf("%d\n",ans);
    }
    return 0;
}


HDU 2029

简单粗暴的方法

#include<stdio.h>
#include<math.h>
int read()
{
    char c;
    int x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return x*res;
}
int main()
{
    int n;
    char s[105];
    while(~scanf("%d",&n)){
        getchar();
        while(n--){
            gets(s);
            int len=strlen(s);
//            printf("%s\n",s);
            int flag=1;
            for(int i=0;i<len/2;i++){
                if(s[i]!=s[len-i-1]){
                    flag=0;
                    break;
                }
            }
            printf("%s\n",flag?"yes":"no");
        }
    }
    return 0;
}


HDU 2030

这题跟汉字机内码有关,汉字在计算机中如何表示呢?一个汉字占两个字符,且每个字符的最高位为1,也就是说如果这两个字符表示的整型数为负数。我们就可以统计为负数的字符数量,答案除二即可~ 另外这题字符数组大小开100不够,要开1000.或者我提供了另一种写法不需要考虑数组大小,可以参考一下~

//用了数组的
#include<stdio.h>
#include<math.h>
int read()
{
    char c;
    int x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return x*res;
}
int main()
{
    int n;
    char s[1005];
    while(~scanf("%d",&n)){
        getchar();
        while(n--){
            gets(s);
            int len=strlen(s);
            int ans=0;
            for(int i=0;i<len;i++){
                if((int)s[i]<0){
                    ans++;
                }
            }
            printf("%d\n",ans>>1);
        }
    }
    return 0;
}

//不用数组
#include<stdio.h>
#include<math.h>
int read()
{
    char c;
    int x=1;
    while((c=getchar())<'0'||c>'9') if(c=='-') x=-1;
    int res=c-'0';
    while((c=getchar())>='0'&&c<='9') res=res*10+c-'0';
    return x*res;
}
int main()
{
    int n;
    while(~scanf("%d",&n)){
        getchar();
        while(n--){
            int ans=0;
            char c;
            while((c=getchar())!='\n'){
                if((int)c<0){
                    ans++;
                }
            }
            printf("%d\n",ans>>1);
        }
    }
    return 0;
}


加油~ 同志仍需努力~

发布了71 篇原创文章 · 获赞 89 · 访问量 8546

猜你喜欢

转载自blog.csdn.net/weixin_43890662/article/details/102578921