ACM 第二次测验 关于 数据结构 和 STL

Problem A

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑 
每题的分值,所以并不是最后的排名。给定录取分数线,请你写程序找出最后通过分数线的 
考生,并将他们的成绩按降序打印。 

Input

测试输入包含若干场考试的信息。每场考试信息的第1行给出考生人数N ( 0 < N 
< 1000 )、考题数M ( 0 < M < = 10 )、分数线(正整数)G;第2行排序给出第1题至第M题的正整数分值;以下N行,每行给出一 
名考生的准考证号(长度不超过20的字符串)、该生解决的题目总数m、以及这m道题的题号 
(题目号由1到M)。 
当读入的考生人数为0时,输入结束,该场考试不予处理。 

Output

对每场考试,首先在第1行输出不低于分数线的考生人数n,随后n行按分数从高 
到低输出上线考生的考号与分数,其间用1空格分隔。若有多名考生分数相同,则按他们考 
号的升序输出。 

Sample Input

4 5 25
10 10 12 13 15
CS004 3 5 1 3
CS003 5 2 4 1 3 5
CS002 2 1 2
CS001 3 2 3 5
1 2 40
10 30
CS001 1 2
2 3 20
10 10 10
CS000000000000000001 0
CS000000000000000002 2 1 2
0

Sample Output

3
CS003 60
CS001 37
CS004 37
0
1
CS000000000000000002 20

Hint

Huge input, scanf is recommended.

#include "iostream"  
#include "stdio.h"  
#include "algorithm"  
#include <string.h>  
using namespace std;  
const int MAXN = 11;  
const int MAX = 22;  
int point[MAXN];  
struct Student  
{  
       char num[MAX];  
       int pro_num[MAXN];  
       int pnt ;  
};  
bool cmp(const Student &a,const Student &b)  
{  
     if (a.pnt == b.pnt)  
     return strcmp(a.num,b.num) < 0 ? 1 : 0;  
     else return a.pnt > b.pnt;  
}  
int main()  
{  
    int N, sum;  
    while (scanf("%d",&N) != EOF)  
    {  
          sum = 0;  
          if (N == 0) break;  
          int M, G;  
          scanf("%d%d",&M,&G);  
          int i;  
          int max;  
          for(i = 0; i < M; i++)  
          {  
                scanf("%d",&point[i]);  
          }  
          Student stu[1001];  
          for(i = 0; i < N; i ++)  
          {  
                scanf("%s",&stu[i].num);  
                stu[i].pnt = 0;  
                int slv, j;  
                scanf("%d",&slv);  
                for (j = 0; j < slv; j++)  
                {  
                    scanf("%d",&stu[i].pro_num[j]);  
                    stu[i].pnt = stu[i].pnt + point[stu[i].pro_num[j] - 1];  
                }  
          }  
          for (i = 0; i < N; i++)  
          {  
              if (stu[i].pnt >= G) sum++;  
          }  
          printf("%d\n",sum);  
          sort (stu,stu + N,cmp);  
          for (i = 0; i < sum; i++)  
          {  
              printf("%s %d\n", stu[i].num,stu[i].pnt);  
          }  
            
    }  
    system ("pause");  
    return 0;  
}  

Problem B

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

输入一个十进制数N,将它转换成R进制数输出。

Input

输入数据包含多个测试实例,每个测试实例包含两个整数N(32位整数)和R(2<=R<=16, R<>10)。

Output

为每个测试实例输出转换后的数,每个输出占一行。如果R大于10,则对应的数字规则参考16进制(比如,10用A表示,等等)。

Sample Input

7 2
23 12
-4 3

Sample Output

111
1B
-11

#include <cstdio>
#include <cstdlib>


using namespace std;


void change(int n,int r)
{
    if(n < r)
    {
        if(n == 10)
            printf("A");
        else if(n == 11)
            printf("B");
        else if(n == 12)
            printf("C");
        else if(n == 13)
            printf("D");
        else if(n == 14)
            printf("E");
        else if(n == 15)
            printf("F");
        else
            printf("%d",n);
    }
    else
    {
        change(n / r,r);
        change(n % r ,r);
    }
}


int main()
{
    int n,r;
    while(scanf("%d%d",&n,&r) != EOF)
    {
        if(n < 0)
        {
            n *= -1;
            printf("-");
        }
        change(n,r);
        printf("\n");
    }
    return 0;
}

Problem C

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.

Input

The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.

Output

The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.

Sample Input

3 123 321
3 123 312

Sample Output

Yes.
in
in
in
out
out
out
FINISH
No.
FINISH

Hint

Hint
For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output "No.".

#include<cstdio>  
#include<stack>  
#include<cstring>  
using namespace std;  
int main(){  
    int n,j,i,k,flag[50];  
    char in[15],out[15];//入栈顺序和出栈顺序  
    while(~scanf("%d %s %s",&n,in,out)){  
        stack <char> sta;  
        memset(flag,-1,sizeof(flag));//为1表示压入栈,为0表示输出栈,记录进出栈过程  
        j=k=0;  
        for(i=0;i<n;i++){//输入的位置的变化  
            sta.push(in[i]);//入栈  
            flag[k++]=1;  
            while(!sta.empty()&&sta.top()==out[j]){//当栈不空并且栈顶元素与当前要出栈元素相等时出栈,否者入栈  
                flag[k++]=0;//为0表示输出栈  
                j++;  
                sta.pop();  
            }  
        }  
        if(j==n){  
            printf("Yes.\n");  
            for(i=0;i<k;i++){  
                if(flag[i]) printf("in\n");//为1表示压入栈,为0表示输出栈  
                else printf("out\n");  
            }  
        }  
        else printf("No.\n");  
        printf("FINISH\n");  
    }  
    return 0;  
}  

Problem D

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。

Input

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。

Output

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。

Sample Input

1 + 2
4 + 2 * 5 - 7 / 11
0

Sample Output

3.00
13.36
#include<cstdio>  
#include<cstring>  
#include<stack>  
using namespace std;  
stack <double> num;//数字栈  
stack <char> sta;//字符栈  
  
double math(char c){//变量为识别的运算符  
    double a,b;//结果  
    a=num.top(); num.pop();  
    b=num.top(); num.pop();  
    switch(c){  
        case '*': return b*a;  
        case '/': return b/a;  
        case '+': return b+a;  
        case '-': return b-a;  
    }//注意这里 a,b 顺序不能反  
}//只进行一次运算  
  
int main(){  
    char oper,str[210];  
    int len,i;  
    double nnum;  
    memset(str,0,sizeof(str));  
    while(gets(str)&&strcmp(str,"0")!=0){  
        len=strlen(str);  
        /********************** for *******************/  
        for(i=0;i<len;i++){  
            while(str[i]==' ') i++;  //略过空格  
  
            /************下面为转换数字**********/  
            nnum=0;  
            while(str[i]>='0'&&str[i]<='9'&&i<len){  
                nnum=nnum*10+str[i]-'0';  
                i++;  
            } //将数字由char转换为double  
            num.push(nnum); //数字进栈  
  
            /************下面为略过空格**********/  
            while(str[i]==' ') i++;//略过空格  
  
            /************下面为判断操作符**********/  
            if(str[i]=='+'||str[i]=='-'){  
                while(!sta.empty()){  
                    num.push(math(sta.top()));  
                    sta.pop();  
                }  
                sta.push(str[i]);  
            }//运算符为‘+’,‘-’就清栈  
  
            else if(str[i]=='*'||str[i]=='/'){  
                if(!sta.empty()&&(sta.top()=='*'||sta.top()=='/')){  
                    num.push(math(sta.top()));  
                    sta.pop();  
                }  
                sta.push(str[i]);  
            }//运算符为‘*’,‘/’时,先判断前方是否是‘*’,‘/’,若是就按运算顺序算前一个运算符,否者此运算符进栈  
        }  
        /*********************** for *************************/  
        while(!sta.empty()){  
                nnum=math(sta.top());  
                sta.pop();  
                num.push(nnum);  
            }//清空剩余栈  
            printf("%.2lf\n",num.top());  
            num.pop();  
            memset(str,0,sizeof(str));  
    }  
    return 0;  
}  

Problem E

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

看病要排队这个是地球人都知道的常识。
不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则选择最早来排队的病人。

现在就请你帮助医院模拟这个看病过程。

Input

输入数据包含多组测试,请处理到文件结束。
每组数据第一行有一个正整数N(0<N<2000)表示发生事件的数目。
接下来有N行分别表示发生的事件。
一共有两种事件:
1:"IN A B",表示有一个拥有优先级B的病人要求医生A诊治。(0<A<=3,0<B<=10)
2:"OUT A",表示医生A进行了一次诊治,诊治完毕后,病人出院。(0<A<=3)

Output

对于每个"OUT A"事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出"EMPTY"。
诊治人的编号ID的定义为:在一组测试中,"IN A B"事件发生第K次时,进来的病人ID即为K。从1开始编号。

Sample Input

7
IN 1 1
IN 1 2
OUT 1
OUT 2
IN 2 1
OUT 2
OUT 1
2
IN 1 1
OUT 1

Sample Output

2
EMPTY
3
1
1
#include<stdio.h>  
#include<string.h>  
#include<stdlib.h>  
  
struct patient{//病人信息数据结构定义;   
       int id;//病人编号;  
       int prior;//病人优先级;   
}p[3][2001];//3个队列,对应3个医生;   
  
int main(){  
    int N;  
    while(scanf("%d",&N)!=EOF){  
          int i,j,cnt=0;//记录病人编号;   
          int count[3]={0};//记录各医生队列长度;   
          for(i=0;i<N;i++){  
               int A,B;//A:医生,B:优先级;   
               char s[4];  
               getchar();//接受回车符;   
               scanf("%s",s);  
               if(strcmp(s,"IN")==0){//输入IN事件;   
                     scanf("%d %d",&A,&B);  
                     //printf("%s %d %d/n",s,A,B);  
                     cnt++;//病人编号增1;   
                     //printf("%d**********/n",cnt);  
                     if(count[A-1]==0){//病人队列为空;   
                            p[A-1][0].id=cnt;  
                            p[A-1][0].prior=B;           
                     }  
                     else{  
                          for(j=count[A-1]-1;j>=0;j--){//数组元素移动;  
                              if(p[A-1][j].prior>=B)break;  
                              p[A-1][j+1].id=p[A-1][j].id;   
                              p[A-1][j+1].prior=p[A-1][j].prior;                
                          }  
                     //插入病人记录;  
                     p[A-1][j+1].id=cnt;  
                     p[A-1][j+1].prior=B;  
                     }  
                     count[A-1]++;//病人队列长度增加1;   
               }   
               else{  
                    scanf("%d",&A);  
                    if(count[A-1]==0){//医生A队列空;   
                          printf("EMPTY\n");            
                    }  
                    else{  
                         printf("%d\n",p[A-1][0].id);  
                         for(j=0;j<count[A-1]-1;j++){  
                              p[A-1][j].id=p[A-1][j+1].id;   
                              p[A-1][j].prior=p[A-1][j+1].prior;                      
                         }  
                         p[A-1][count[A-1]].id=0;  
                         p[A-1][count[A-1]].prior=0;  
                         count[A-1]--;//病人数量减一;   
                    }  
               }             
          }                       
    }  
}  

Problem F

Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

四月一日快到了,Vayko想了个愚人的好办法——送礼物。嘿嘿,不要想的太好,这礼物可没那么简单,Vayko为了愚人,准备了一堆盒子,其中有一个盒子里面装了礼物。盒子里面可以再放零个或者多个盒子。假设放礼物的盒子里不再放其他盒子。

用()表示一个盒子,B表示礼物,Vayko想让你帮她算出愚人指数,即最少需要拆多少个盒子才能拿到礼物。

Input

本题目包含多组测试,请处理到文件结束。
每组测试包含一个长度不大于1000,只包含'(',')'和'B'三种字符的字符串,代表Vayko设计的礼物透视图。
你可以假设,每个透视图画的都是合法的。

Output

对于每组测试,请在一行里面输出愚人指数。

Sample Input

((((B)()))())
(B)

Sample Output

4
1

#include<stdio.h>
int main()
{    int i,n;
    char a[1000];
    while(scanf("%s",&a)!=EOF){
    for(i=0,n=0;;i++)
    {    if(a[i]==')') n--;
        if(a[i]=='(') n++;
        if(a[i]=='B') break;}
    printf("%d\n",n);}}

Problem G

Time Limit : 1000/500ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

Hey, welcome to HDOJ(Hangzhou Dianzi University Online Judge).

In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n.

Input

The input will consist of a series of integers n, one integer per line.

Output

For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.

Sample Input

1
100

Sample Output

1

5050
#include<stdio.h>
int main()
{
    int n,sum;
    while(scanf("%d",&n)!=EOF)
    {
        if(n%2==0)
            sum=n/2*(n+1);
         else
            sum=(n+1)/2*n;
        printf("%d\n\n",sum);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值