HDOJ题目(11.22-12.22)

HDOJ题目(11.22-12.22)

[TOC]

 

 

 

 

hdoj_2000

注意getchar的用方法,按照以上的输入之后,处理三个字符以外还会有换行符

 

#include<iostream>
#include<cstdio> 
#include<algorithm>
#include<algorithm>
using namespace std;
int main()
{
    char arr[3];
    while(scanf("%c%c%c",&arr[0],&arr[1],&arr[2])!=EOF)
    {
        //cout<<getchar(); //输出是10 
        //cout<<(int)('\n'); //输出也是10 
        getchar(); 
        sort(arr,arr+3);
        printf("%c %c %c\n",arr[0],arr[1],arr[2]);
    } 
}

 

hdoj_2002

 

#include<iostream>
#include<cstdio> 
#include<algorithm>
#include<algorithm>
#include<cmath>
#define PI 3.1415927
using namespace std;

int main()
{
    //double精度高,有效数字16位,float精度7位
    //用double才够表示,不然会溢出 
    double r;
    double rst;
    while(scanf("%lf",&r)!=EOF)
    {
        rst=(4/3.0)*PI*pow(r,3);//注意一定要3.0或者4.0,不然3/4=1 
        printf("%.3lf\n",rst);
    }
}

 

 

hdoj_2005

会忘记归0很不好

#include<iostream>
#include<cstdio> 
#include<algorithm>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    int year,month,day;
    char ch;
    
    int arr1[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int arr2[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
    while(scanf("%d%c%d%c%d",&year,&ch,&month,&ch,&day)!=EOF)
    {
        int rst=0; //算完一次要归0 
        if(year%400==0 ||(year%4==0 && year%100!=0))//闰年 
        {
            for(int i=0;i<month;i++)
                rst+=arr2[i];
            rst+=day;    
        }
        else//非闰年 
        {
            for(int i=0;i<month;i++)
                rst+=arr1[i];
            rst+=day;
        }
        printf("%d\n",rst);
    }
}

 

 

hdoj_2006

 

#include<iostream>
#include<cstdio> 
#include<algorithm>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int arr[10000];//不可以用int arr[n]本地正确,但是oj会编译出错 
        long long rst=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&arr[i]);
            if(arr[i]%2!=0)
                rst*=arr[i];
        }
        printf("%lld\n",rst);    //用lld不然会溢出 
            
     } 
} 

 

 

hdoj_2007

#include<iostream>
#include<cstdio> 
#include<algorithm>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    int L,R;
    while(scanf("%d %d",&L,&R)!=EOF)
    {
        long long rst1=0,rst2=0;
        if(L>R)
            swap(L,R) ;//坑点在此,题目没有明说大小 
        for(int i=L;i<=R;i++)
        {
            if(i%2==0)
                rst1+=pow(i,2);
            else
                rst2+=pow(i,3);            
        }    
        printf("%lld %lld\n",rst1,rst2);
    }
} 

 

int min=INT_MAX;//定义最大能表示的数



bool cmp(int a,int b)

{

    return abs(a)>abs(b)?true:false;//返回由大到小的数组排序 

}

    sort(arr,arr+n,cmp);//按照绝对值由大到小排序

 

hdoj_2022

#include<iostream>
#include<stdio.h>
#include<string>
#include<cmath> 
#include<algorithm>
#include<vector>
using namespace std;
int arr[2000][2000];
int main()
{
    int m,n;
    while(scanf("%d %d",&m,&n)!=EOF)
    {
            //int arr[2000][2000]; int arr[2000][2000]就退出,这么大居然不用new/malloc。
        //函数体内的数组会放在栈中,一般会爆。不过还是推荐使用malloc/calloc把数组放在堆上或者开全局 
        //全局变量存在于静态区,这样就没有爆栈的问题了。
        int rst=0;
        int position_x=0,position_y=0;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                 scanf("%d",&arr[i][j]);
                 if(rst<abs(arr[i][j]))
                 {
                    position_x=i;
                    position_y=j;
                    rst=abs(arr[i][j]);
                 }
            } 
        }
            
        printf("%d %d %d\n",position_x+1,position_y+1,arr[position_x][position_y]);
    }
    
    
    return 0;
    
}
 

 

hdoj_2023

#include<iostream>
#include<stdio.h>
#include<vector>
#include<stdlib.h>
#include<string>
#include<cstring>
using namespace std;
int arr[51][6];
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        int rst=0;
        double sub_score[6];
        double stu_score[51];
        memset(sub_score,0.0,sizeof(sub_score));//必须是include<cstring>中 
        memset(stu_score,0.0,sizeof(stu_score));
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%d",&arr[i][j]);
                stu_score[i]+=arr[i][j];
            }
            stu_score[i]/=m;
        }
        
        /*
        //测试
        for(int i=0;i<n;i++) 
            for(int j=0;j<m;j++)
                cout<<arr[i][j]<<" ";
        cout<<endl;        
        for(int i=0;i<n;i++)
            cout<<stu_score[i]<<" ";    
        
        */
        
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
                sub_score[i]+=arr[j][i];
            sub_score[i]/=n;
        }
        
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(arr[i][j]<sub_score[j])
                {
                    rst++;
                    break;
                }
            }
        }
        
        for(int i=0;i<n-1;i++)
            printf("%.2lf ",stu_score[i]);//测试等的时候stu_score和sub_score都是正常,最后输出是0,因为他们是double类型,我却输出 
        printf("%.2lf\n",stu_score[n-1]);//是%d,所以输出是0. 
        
        for(int i=0;i<m-1;i++)
            printf("%.2lf ",sub_score[i]);
        printf("%.2lf\n",sub_score[m-1]);
        
        printf("%d\n\n",(n-rst));//每个测试实例后面跟一个空行。  根据题意要两个/n,之前一直没加/n,一直pe 
            
        
        
    }
    return 0;
} 

 

 

hdoj_2024

#include<iostream>
#include<stdio.h>
#include<vector>
#include<stdlib.h>
#include<string>
#include<cstring>
using namespace std;
int main()
{
    
    /* 
    gets(str);//gets只能是char数组不能是string类型。gets(string)是错误的 
    puts(str); 
    "ff  ai_2"含有空格的也可以正常读入(for也可以) 
    */ 
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        //getchar();//注意如果没有getchar()的话\n会被计入str 
        //cout<<getchar(); 输出是10="\n" 
        getchar();
        char str[10000];
        for(int i=0;i<n;i++)
        {
            bool tag=true;
            gets(str);
            for(int i=0;str[i]!='\0';i++)
            {
                if(i==0 && !( (str[i]>='A'&& str[i]<='Z') || (str[i]<='z' && str[i]>='a') || str[i]=='_' ))
                {
                        printf("no\n");
                        tag=false;
                        break;
                }
                if(i!=0 && !( (str[i]>='A'&& str[i]<='Z') || (str[i]<='z' && str[i]>='a') || str[i]=='_'  || (str[i]<='9'&&str[i]>='0')))
                {
                    tag=false;
                    printf("no\n");
                    break;
                }    
            
            }
            if(tag)
                printf("yes\n");    
        }
    }
     
    
    return 0;
 } 

 

 

hdoj_2030

 

#include<iostream>
#include<stdio.h>
#include<vector>
#include<stdlib.h>
#include<string>
#include<cstring>
using namespace std;

/*
汉字编码方式很多,有国标码(GB2312_1980),大五码,GBK,简体18030,区位码,电报码,
还有它们的变形:unicode 大端小端码,UTF-8,UTF-*,HZ 等等。
内码特点 :二进制双字节,每字节用到8bits.
输入原理,把连续输入的ASCII字符串,通过中文输入软件转化为双字节 中文内码。


在C语言中,可以通过将汉字作为字符串输入。
由于一个汉字占2个字节,所以对汉字的操作,只能以2个字节作为操作单位。
下面通过具体实例来说明汉字在C语言中的使用:
char s[] = "首都北京"; // 将汉字字符赋值给字符数组
char s2[20]; // 定义字符数组,存放用户输入的汉字
scanf("%s", s2); // 接收用户输入的汉字字符
printf("%d\n", sizeof(s)); // 计算字符数组s所占的内存单元,输出9(最后一个字节是结束字符'\0')
printf("%s\n", &s[2]); // 输出“都北京”(首字占2个字节)
printf("%s\n", s2); // 输出用户输入的汉字 


**********关键是:汉字的ascii码是小于0的 
*/ 
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        while(n--)
        {
            int rst=0;
            char str[1000];
            gets(str);
            for(int i=0;str[i]!='\0';i++)
                if(str[i]<0)
                    rst++;
            rst=rst/2;        
            printf("%d\n",rst);
        
        }
    }
    
    
    return 0;
 }

 

posted on 2018-11-23 14:30  么猪~  阅读(164)  评论(0编辑  收藏  举报

导航