杭电ACM基础题(1201、1202、1205、1219、1234、1235)

1201、计算一个人从出生到18岁生日所经历的时间

Code:
分析闰年、平年和2月份的关系

//计算一个人从出生到18岁生日所经历的总的天数
#include<iostream>
#include<string>
using namespace std;
bool isLeap(int year){
    if((year%4==0&&year%100!=0)||year%400==0){
        return 1;//闰年 
    }else{
        return 0;
    } 
}
int main(){
    int n,year,month,day;
    char c[10];
    while(cin>>n){
        for(int i=0;i<n;i++){
            cin>>c;
            //提取c中的年月日信息 
            year=1000*(c[0]-'0')+100*(c[1]-'0')+10*(c[2]-'0')+(c[3]-'0');
            month=10*(c[5]-'0')+(c[6]-'0');
            day=10*(c[8]-'0')+(c[9]-'0');
            if(month==2&&day==29){
                cout<<-1<<endl;
                continue;
            }
            int sum=0,ayear=year+18;
            if(isLeap(year)==1&&month<3){
                sum++;
            }
            if(isLeap(ayear)==1&&month>2){
                sum++;
            }
            for(int i=year+1;i<ayear;i++){
                if(isLeap(i)==1){
                    sum++;
                }
            }
            cout<<18*365+sum<<endl;
        }
    }
} 

1202、The calculation of GPA[根据一定规则计算GPA]

每学期的期末,大家都会忙于计算自己的平均成绩,这个成绩对于评奖学金是直接有关的。国外大学都是计算GPA(grade point average) 又称GPR(grade point ratio),即成绩点数与学分的加权平均值来代表一个学生的成绩的。那么如何来计算GPA呢?

一般大学采用之计分法

A90 - 100 4 点
B80 - 89 3 点
C70 - 79 2 点
D60 - 69 1 点
E0 - 59 0 点

例如:某位学生修习三门课,其课目、学分及成绩分别为:
英文:三学分、92 分;化学:五学分、80 分;数学:二学分、60分,则GPA的算法如下:

科目 学分 分数 点数 分数×点数
英文 3 92 4 12
化学 5 80 3 15
数学 2 60 1 2
合计 10 29
29/10=2.9
2.9即为某生的GPA
下面有请你写一个用于计算GPA的程序。
Input
包含多组数据,每组数据的第一行有一个数N,接下来N行每行表示一门成绩。每行有两个实型的数 s,p,s表示这门课的学分,p表示该学生的成绩(百分制)。如果p=-1则说明该学生这门课缺考,是不应该计算在内的。
Output
对每组数据输出一行,表示该学生的GPA,保留两位小数。如果GPA不存在,输出-1。
Sample Input

3
3 92
5 80
2 60

Sample Output

2.90

Code
1、计算给定多门成绩的GPA
2、注意输入的成绩、学分可能是小数

//计算GPA
//注意输入的成绩、学分可能是小数
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
    int n;
    while(cin>>n){
        //学分和成绩 
        double *s=new double[n];
        double *g=new double[n]; 
        double sum_s=0,s_g,sum_s_g=0;
        double gpa; 
        for(int i=0;i<n;i++){
            cin>>s[i]>>g[i];
            //如果成绩为-1,则表明该学生的学分、成绩均为0 
            if(g[i]==-1){
                s[i]=0;g[i]=0; 
            }
            //计算学生成绩所对应的绩点,并将绩点存放在数组g[]中
            if(g[i]>=90){
                g[i]=4;
            }
            else if(g[i]>=80&&g[i]<90){
                g[i]=3;
            }
            else if(g[i]>=70&&g[i]<80){
                g[i]=2;
            }
            else if(g[i]>=60&&g[i]<70){
                g[i]=1;
            }
            else{
                g[i]=0;
            }
            //计算学分的总和 
            sum_s=sum_s+s[i];
            //计算学分*绩点
            s_g=s[i]*g[i];
            //计算学分*绩点的总和
            sum_s_g=sum_s_g+s_g;
        }
        if(sum_s==0){
            cout<<"-1"<<endl;
        }
        else{
            gpa=sum_s_g/sum_s;
            cout<<setiosflags(ios::fixed);
            cout<<setprecision(2)<<gpa<<endl;
        }
    }
} 

1205、吃糖果

HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一种,这样;可是Gardon不知道是否存在一种吃糖果的顺序使得他能把所有糖果都吃完?请你写个程序帮忙计算一下。
Input
第一行有一个整数T,接下来T组数据,每组数据占2行,第一行是一个整数N(0<N<=1000000),第二行是N个数,表示N种糖果的数目Mi(0<Mi<=1000000)。
Output
对于每组数据,输出一行,包含一个"Yes"或者"No"。
Sample Input

2
3
4 1 1
5
5 4 3 2 1

Sample Output

No
Yes

Code:
1、定义的数据类型不需要过大,适合最好,不然会超时

#include<stdio.h>
#include<stdint.h>
#include<stdlib.h>
long m[1000000];
int main (){
    int t;//组数     
    while(scanf("%d",&t)!=EOF){
        long n;//n种糖果数目(0,1000000]
        for(int i=0;i<t;i++){
            scanf("%d",&n);
            //每种糖果的数目(0,1000000] 
            long long sum=0;
            int max=0;
            //输入m[i],找出m[i]中的最大值,以及除m[i]最大值外其他值的和
            for(int i=0;i<n;i++){
                scanf("%d",&m[i]);
                if(m[max]<m[i]){
                    max=i;
                }
                sum=sum+m[i];
            }
            //m[max]即为糖果数的最大值,sum为总糖果数
            if(m[max]<=(sum-m[max]+1)) {
                printf("Yes\n");
            }
            else{
                printf("No\n");
            }
        }
    } 
    return 0;
}

1219、AC Me[统计一行中字母的个数]

Ignatius is doing his homework now. The teacher gives him some articles and asks him to tell how many times each letter appears.

It’s really easy, isn’t it? So come on and AC ME.
Input
Each article consists of just one line, and all the letters are in lowercase. You just have to count the number of each letter, so do not pay attention to other characters. The length of article is at most 100000. Process to the end of file.

Note: the problem has multi-cases, and you may use “while(gets(buf)){…}” to process to the end of file.
Output
For each article, you have to tell how many times each letter appears. The output format is like “X:N”.

Output a blank line after each test case. More details in sample output.
Sample Input

hello, this is my first acm contest!
work hard for hdu acm.

Sample Output

a:1
b:0
c:2
d:0
e:2
f:1
g:0
h:2
i:3
j:0
k:0
l:2
m:2
n:1
o:2
p:0
q:0
r:1
s:4
t:4
u:0
v:0
w:0
x:0
y:1
z:0

a:2
b:0
c:1
d:2
e:0
f:1
g:0
h:2
i:0
j:0
k:1
l:0
m:1
n:0
o:2
p:0
q:0
r:3
s:0
t:0
u:1
v:0
w:1
x:0
y:0
z:0

Code:
1、统计一行中字母的个数
2、gets()函数用来从标准输入设备中读取字符串函数,以回车结束,需要添加头文件
3、strlen(s);求字符数组的长度,添加头文件<string.h>

//统计一行中字母的个数
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int main(){
    char str[100000];//用来存放输入的字符串 
    while(gets(str)){//gets()从标准输入设备中读字符串函数,可以无限读取,以回车结束 ,添加头文件<cstdio> 
        int len=strlen(str);//添加头文件<string.h> 
        int num[26];
        //初始化num[]数组,用来存放各个字母出现的数量 
        for(int i=0;i<26;i++){
            num[i]=0;
        }
        //遍历字符串数组 
        for(int i=0;i<len;i++){
            switch(str[i]){
                case 'a':
                    num[0]++;
                    break; 
                case 'b':
                    num[1]++;
                    break;
                case 'c':
                    num[2]++;
                    break; 
                case 'd':
                    num[3]++;
                    break;
                case 'e':
                    num[4]++;
                    break; 
                case 'f':
                    num[5]++;
                    break;
                case 'g':
                    num[6]++;
                    break; 
                case 'h':
                    num[7]++;
                    break;
                case 'i':
                    num[8]++;
                    break; 
                case 'j':
                    num[9]++;
                    break;
                case 'k':
                    num[10]++;
                    break; 
                case 'l':
                    num[11]++;
                    break;
                case 'm':
                    num[12]++;
                    break; 
                case 'n':
                    num[13]++;
                    break;
                case 'o':
                    num[14]++;
                    break; 
                case 'p':
                    num[15]++;
                    break;
                case 'q':
                    num[16]++;
                    break; 
                case 'r':
                    num[17]++;
                    break;
                case 's':
                    num[18]++;
                    break; 
                case 't':
                    num[19]++;
                    break;
                case 'u':
                    num[20]++;
                    break; 
                case 'v':
                    num[21]++;
                    break;
                case 'w':
                    num[22]++;
                    break; 
                case 'x':
                    num[23]++;
                    break;
                case 'y':
                    num[24]++;
                    break; 
                case 'z':
                    num[25]++;
                    break;
                default:
                    break;
            }
        }
        //输出各个字母对应的数量
        char c[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
                    'p','q','r','s','t','u','v','w','x','y','z'};
        for(int i=0;i<26;i++){
            cout<<c[i]<<":"<<num[i]<<endl;
        } 
        cout<<endl;
    }
} 

1234、开门人和关门人

每天第一个到机房的人要把门打开,最后一个离开的人要把门关好。现有一堆杂乱的机房签
到、签离记录,请根据记录找出当天开门和关门的人。
Input
测试输入的第一行给出记录的总天数N ( > 0 )。下面列出了N天的记录。
每天的记录在第一行给出记录的条目数M ( > 0 ),下面是M行,每行的格式为

证件号码 签到时间 签离时间

其中时间按“小时:分钟:秒钟”(各占2位)给出,证件号码是长度不超过15的字符串。
Output
对每一天的记录输出1行,即当天开门和关门人的证件号码,中间用1空格分隔。
注意:在裁判的标准测试输入中,所有记录保证完整,每个人的签到时间在签离时间之前,
且没有多人同时签到或者签离的情况。
Sample Input

3
1
ME3021112225321 00:00:00 23:59:59
2
EE301218 08:05:35 20:56:35
MA301134 12:35:45 21:40:42
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

Sample Output

ME3021112225321 ME3021112225321
EE301218 MA301134
SC3021234 CS301133

Code
1、定义char[]数组时,大小应比实际需要多一位

//根据签到记录表,找出每一天最早来的人与最晚离开的人
#include<iostream>
#include<climits>
#include<string>
using namespace std;
int main(){
    int n;//记录的总天数,n>0

    while(cin>>n){
        for(int i=0;i<n;i++){
            int m;//记录的条目数,m>0
            cin>>m;    
            char numid[16];//定义存储证件号码
            char time1[9],time2[9];//定义存储到达时间、离开时间的数组
            string idunlocked,idlocked;
            int etime=INT_MAX,ltime=INT_MIN;
            int timein,timeout;//到达时间和离开时间 
        
            for(int i=0;i<m;i++){
                cin>>numid>>time1>>time2;
            //计算到达时间 
                timein=((time1[0]-'0')*10+(time1[1]-'0'))*3600+
                          ((time1[3]-'0')*10+(time1[4]-'0'))*60+
                       ((time1[6]-'0')*10+(time1[7]-'0'));
            //计算离开时间 
                timeout=((time2[0]-'0')*10+(time2[1]-'0'))*3600+
                        ((time2[3]-'0')*10+(time2[4]-'0'))*60+
                        ((time2[6]-'0')*10+(time2[7]-'0'));
                   
                if(timein<etime){
                    etime=timein;
                    idunlocked=numid;//字符数组转化为字符串 
                }
                if(timeout>ltime){
                    ltime=timeout;
                    idlocked=numid;
                }
            }
            cout<<idunlocked<<" "<<idlocked<<endl;
        }
    
    } 
    return 0; 
} 

1235、统计同成绩学生人数

读入N名学生的成绩,将获得某一给定分数的学生人数输出。
Input
测试输入包含若干测试用例,每个测试用例的格式为

第1行:N
第2行:N名学生的成绩,相邻两数字用一个空格间隔。
第3行:给定分数

当读到N=0时输入结束。其中N不超过1000,成绩分数为(包含)0到100之间的一个整数。
Output
对每个测试用例,将获得给定分数的学生人数输出。
Sample Input

3
80 60 90
60
2
85 66
0
5
60 75 90 55 75
75
0

Sample Output

1
0
2

Code
1、统计给定成绩的学生的人数,比较简单

//统计指定分数的学生人数
#include<iostream>
using namespace std;
int main(){
    //n名学生的成绩(0,1000]
    int n;
    while(cin>>n){
        if(n==0) return 0;
        int grade[1000];
        //输入n名学生的成绩 
        for(int i=0;i<n;i++){
            cin>>grade[i];
        }
        int score;
        //输入给定分数 
        cin>>score;
        //统计给定分数的学生人数
        int sum=0;
        for(int i=0;i<n;i++){
            if(grade[i]==score){
                sum++;
            }
        }
        cout<<sum<<endl; 
    } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值