PAT L1-005. 考试座位号 Java超时解决方案

2 篇文章 0 订阅
每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。

输入格式:

输入第一行给出一个正整数N(<=1000),随后N行,每行给出一个考生的信息:“准考证号 试机座位号 考试座位号”。其中准考证号由14位数字组成,座位从1到N编号。输入保证每个人的准考证号都不同,并且任何时候都不会把两个人分配到同一个座位上。

考生信息之后,给出一个正整数M(<=N),随后一行中给出M个待查询的试机座位号码,以空格分隔。

输出格式:

对应每个需要查询的试机座位号码,在一行中输出对应考生的准考证号和考试座位号码,中间用1个空格分隔。

输入样例:
4
10120150912233 2 4
10120150912119 4 1
10120150912126 1 3
10120150912002 3 2
2
3 4
输出样例:
10120150912002 2
10120150912119 1

我用Java 一直过不了 我的Java代码如下:


import java.util.*;


public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
			Scanner sc = new Scanner(System.in);
			
			int n = Integer.parseInt(sc.nextLine());
			
			String strr[] = new String[n+1];
			String strr1[][] = new String[n+1][];
			String temp = new String();
			String temp1[] = new String [3];
			for(int v = 0;v < n;v ++){
				temp = sc.nextLine();
				temp1= temp.split(" ");//
			
				strr1[Integer.parseInt(temp1[1])] = temp1;
			}
			int m = sc.nextInt();
			int m1[] = new int[m];
			
			for(int i = 0;i < m;i ++){
				int tem = sc.nextInt();
				System.out.println(strr1[tem][0]+" "+strr1[tem][2]);
			}
	}
}

估测原因是Scanner超时导致。

我用C++一次就过了 超级Nice  此外这道题不需要使用Sort修改函数做 我巧妙地用第二列的试机座位号为进行数组下标确定的依据,整个算法复杂度为O(n)。代码如下:

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;
typedef struct people{
    char a[15];
    int b;
    int c;
}p;



int main(){
  int n;

 scanf("%d",&n);
 struct people Peo[n+1];
  for(int i = 0;i < n;i ++){
    int tempb;
    int tempc;
    char strtemp[15];

    scanf("%s %d %d",strtemp,&tempb,&tempc);
    memset(Peo[tempb].a,'\0',sizeof(char)*15);
    strcat(Peo[tempb].a,strtemp);

    Peo[tempb].b=tempb;
    Peo[tempb].c=tempc;

  }

    int m;
    cin>>m;
    int d[m];
    for(int i = 0;i < m;i ++){
        cin>>d[i];
    }
    for(int i = 0;i < m;i ++){
        printf("%s %d\n",Peo[d[i]].a,Peo[d[i]].c);
    }
    return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值