科大讯飞编程题
发布于 2017-09-16 16:34 5101 次浏览 0 赞 来自 我要提问  

科大讯飞 2018校招 技术综合方向试卷在线考试

编程题|20.0分3/3

课程冲突

时间限制:C/C++语言 2000MS;其他语言 4000MS
内存限制:C/C++语言 65536KB;其他语言 589824KB

题目描述:

小明是一名学生,又到了学校的选课时间,他想选一些课程学习,已知课程开课时间都在每周一到周五之内,早上4讲课,下午4讲课,晚上2讲课。

小明担心选课时间上有所冲突。所以他希望可以对课程时间进行检查。

输入

首先输入一个整数n(0<n<=100),表示小明选课总数。

之后输入n行选课信息,每行选课信息有2个数字。

第一个数字表示开课时间,开课时间用2位数表示,前一位用0到4表示周一至周五,后一位用0到9表示从早到晚顺序第几讲课,如12表示礼拜二第三讲课。01表示礼拜一第二讲课。

每行第二个数字表示课程代码,如:204521。课程代码为6位数字。输入课程代码均不重复。

样例输入

5

01 204521

23 204523

22 204526

01 204528

22 204527

<div class="outputarea yangli ng-scope" ng-if="model.ques.questype==6 && model.ques.outputSample != null && model.ques.outputSample!=''" "="" style="box-sizing: border-box; margin: 0px; padding: 0px;">

样例输出

01 204521 204528

22 204526 204527


Hint

Input Sample 2
3
11 204521
23 204522
43 204531

Output Sample 2
YES


26 条回复

package dddd;




import java.util.*;


public class Main {


    public static void main(String[] args){

    Scanner in = new Scanner(System.in);

    int n=in.nextInt();

    int[] a=new int[n*2];

    for(int i=0;i<n*2;i++){

    a[i]=in.nextInt();

    }

    int j=0;

    TreeSet<Integer>set=new TreeSet<>();

    HashMap<Integer,ArrayList<Integer>>map=new HashMap<>();

    while(j<a.length){

    ArrayList<Integer>list=new ArrayList<>();

    if(map.containsKey(a[j])){

    map.get(a[j]).add(a[j+1]);

    }else{

    list.add(a[j+1]);

    map.put(a[j],list);

    set.add(a[j]);

     

    }

    j=j+2;

    }

    int count=0;

    for(int i:set){

    if(map.get(i).size()>1){

    count++;

    if(i==1){

    System.out.print("0"+i+" ");

    for(int j1=0;j1<map.get(i).size();j1++){

   

    if(j1==map.get(i).size()-1){

      System.out.println(map.get(i).get(j1));

    }else{

          System.out.print(map.get(i).get(j1)+" ");}

   

       }

    }else{

    System.out.print(i+" ");

        for(int j1=0;j1<map.get(i).size();j1++){

       

        if(j1==map.get(i).size()-1){

          System.out.println(map.get(i).get(j1));

        }else{

              System.out.print(map.get(i).get(j1)+" ");}

    }

    }

    }

    }

    if(count==0){

    System.out.println("YES");

    }

     

     

    }

}



2017-09-16 16:49
2


package com.wyb.kedaxunfei;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main2 {


	public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {//注意while处理多个case
        	int n=Integer.parseInt(in.nextLine());
        	Course []A=new Course[n];

        	for(int i=0;i<n;i++){
        		String str=in.nextLine();
        	//	System.out.println(str.substring(0, 1));
        		A[i]=new Course();
        		A[i].xingqi=Integer.parseInt(str.substring(0, 1));
        		A[i].dijijie=Integer.parseInt(str.substring(1, 2));
        		A[i].code=str.substring(3, str.length());
        		A[i].hasDo=false;
       		
        	}
        	boolean hasConflict=false;
        	for(int j=0;j<n;j++){
    			List<Course> list=new ArrayList<Course>();
    			list.add(A[j]);
    			A[j].hasDo=true;
        		for(int k=j+1;k<n;k++){
        			if(A[k].hasDo==false&&A[j].xingqi==A[k].xingqi&&A[j].dijijie==A[k].dijijie){
        				list.add(A[k]);
        				A[k].hasDo=true;
        				hasConflict=true;
        			}
        		}
        		
        		if(list.size()>1){
        			Course course=list.get(0);
        			System.out.print(course.xingqi);
        			System.out.print(course.dijijie);
        			
        			for(int l=0;l<list.size();l++){
        				Course course1=list.get(l);
        				System.out.print(" "+course1.code);
        			}
        			
        		}
        		System.out.println();
        	}
        	
        	if(hasConflict==false){
        		System.out.println("YES");
        	}
        	
        	
       // 	System.out.println(A[0].xingqi+","+A[0].dijijie+","+A[0].code);

        }

	}

}

class Course{
	public int xingqi;
	public int dijijie;
	public String code;
	public boolean hasDo;
	Course(){
		
	}
	
	
}

本地通过,测试30%,额,有大神吗?

2017-09-16 17:03

AC代码

#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main()
{
	int n = 0;
	while (scanf("%d", &n) != EOF)
	{
		string str;
		getline(cin, str);
		map<string, vector<string>> table;
		for (int i = 0; i < n; i++)
		{
			getline(cin, str);
			string courseTime = str.substr(0, 2);
			string courseName = str.substr(2, str.size() - 2);
			table[courseTime].push_back(courseName);
		}
		bool mark = true;
		for (map<string, vector<string>>::iterator iter = table.begin(); iter != table.end(); iter++)
		{
			if (iter->second.size() > 1)
			{
				mark = false;
				cout << iter->first << " ";
				for (int i = 0; i < iter->second.size() - 1; i++)
					cout << iter->second[i] << " ";
				cout << iter->second[iter->second.size() - 1];
			}
			cout << endl;
		}
		if (mark)
			cout << "YES";
	}

	return 0;
}


2017-09-16 17:05
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] sch = new String[n];
        String a = sc.nextLine();
        for (int i = 0; i < sch.length; i++) {
            if(sc.hasNextLine()){
            sch[i]=sc.nextLine();
            }
        }
        getConfict(sch);
    }
    public static void getConfict(String[] sch){
        String[] result = new String[50];
        for (int i = 0; i < sch.length; i++) {
            switch (sch[i].substring(0,1)){
                case "0":
                    addstr(Integer.parseInt(sch[i].substring(1,2)),0,result,sch[i].substring(3));
                    break;
                case "1":
                    addstr(10+Integer.parseInt(sch[i].substring(1,2)),1,result,sch[i].substring(3));
                    break;
                case "2":
                    addstr(20+Integer.parseInt(sch[i].substring(1,2)),2,result,sch[i].substring(3));
                    break;
                case "3":
                    addstr(30+Integer.parseInt(sch[i].substring(1,2)),3,result,sch[i].substring(3));
                    break;
                case "4":
                    addstr(40+Integer.parseInt(sch[i].substring(1,2)),4,result,sch[i].substring(3));
                    break;
            }
        }
        boolean flag = false;
        for (int i = 0; i < result.length; i++) {
            if(result[i]!=null&&result[i].length()>9){
                flag= true;
                System.out.println(result[i]);
            }
        }
        if(!flag)
            System.out.println("YES");

    }
    public static void addstr(int i,int j,String[] result,String str){
        if(result[i]==null){
            result[i]=""+j+i%10;
        }
        result[i]+=" "+str;
    }
}

AC了

2017-09-16 17:05

我也是,还增加了冲突三个的情况,本地也是测试通过,但是提交只有30%,哎,实在不知道哪儿没有考虑到

2017-09-16 17:05

package main;


import java.util.ArrayList;

import java.util.Scanner;


public class Main 

{


public static void main(String[] args) 

{

ArrayList<Integer>[] array=new ArrayList[50];

for(int i=0;i<array.length;i++)

{

array[i]=new ArrayList<Integer>();

}

        Scanner scanner=new Scanner(System.in);

        int n=scanner.nextInt();

        while(n>0)

        {

        int p=scanner.nextInt();

        int q=scanner.nextInt();

        array[p].add(q);

        n--;

        }

        scanner.close();

        int k=0;

        for(int i=0;i<array.length;i++)

        {

        if(array[i].size()>1)

        {

        k++;

        if(i<10)

        {

        System.out.print(0);

              System.out.print(i);

        }

        else

        {

        System.out.print(i);

        }

        for(int j=0;j<array[i].size();j++)

        {

        System.out.print(" "+array[i].get(j));

        }

        if(i!=array.length-1)

        {

        System.out.println();

        }

        }

        }

        if(k==0)

        {

        System.out.println("YES");

        }

        

}


}

本地完全没问题,提交怎么都是0%。。。。。。

2017-09-16 17:07
coder_yYpE1vJ5 回复 coder_SR9K3NS9

我发现了赛码网的题只要一涉及到取String类型,split,不论是用Scanner,还是BufferedReader都是错的,直接无语

2017-09-16 17:07

#include<stdio.h>

#include<iostream>

#include<iomanip>

using namespace std;

#include<math.h>



int main()

{

    int n;

    cin>>n;

    int timeline[n];

    int classcode[n];

    int i,j;

    for(i=0;i<n;i++)

    {

        cin>>timeline[i];

        cin>>classcode[i];

    }

    cout<<endl;


    int code[50][n];

    for(i=0;i<50;i++)

        for(j=0;j<n;j++)

            code[i][j]=0;


    int num[50];

    for(i=0;i<50;i++)

        num[i]=0;

    for(i=0;i<n;i++)

    {


            code[timeline[i]][num[timeline[i]]]=classcode[i];

            num[timeline[i]]++;

    }

    int flag=0;

    for(i=0;i<50;i++)

    {

        if(num[i]>1)

        {

            flag++;

            if(i<10)

            {

                cout<<"0"<<i;

            }

            else

            {

                cout<<i;

            }

            cout<<" ";

            for(j=0;j<num[i];j++)

            {

                cout<<code[i][j];

                cout<<" ";

            }

            cout<<endl;

        }

    }

    if(flag==0)

    {

        cout<<"YES"<<endl;

    }

}


2017-09-16 17:07
#include<iostream>
#include<vector>
#include<string>
using namespace std;
using std::stoi;
struct Lesson {
	string Time;
	int code;
};
int main() {
	int n;
	cin >> n;
	vector<Lesson> kb(n);
	for (int i = 0; i < n; ++i) {
		cin >> kb[i].Time >> kb[i].code;
	}

	vector<vector<int>> count(45);
	for (int i = 0; i < n; ++i) {
		int row = stoi(kb[i].Time)/10;
		int col = stoi(kb[i].Time)%10 - 1;
		count[5 * row + col].push_back(i);
	}

	int tag = 0;
	for (int j = 0; j < 5; ++j) {
		for (int i = 0; i < 9; ++i) {
			if (count[5 * i + j].size() > 1) {
				tag = 1;
				cout << kb[count[5 * i + j][0]].Time;
				for (auto r : count[5 * i + j]) {
					cout << " " << kb[r].code;
				}
				cout << endl;
			}
		}
	}
	if (tag == 0)
		cout << "YES" << endl;
	return 0;
}


2017-09-16 17:08
1
coder_NIskgbo1 回复 coder_SR9K3NS9

当时试了一下 全输出YES 也是30%…

2017-09-16 17:10

import numpy as np


while True:

   try:

        day_course_crash = np.zeros((1, 50))[0]

        num_of_course = int(raw_input())

        course = []

        day_time = []

        for i in range(0, num_of_course):

            string_input = raw_input().split()

            day_course_each = int(string_input[0])

            course_name = string_input[1]

            day_time.append(day_course_each)

            course.append(course_name)

            day_course_crash[day_course_each] += 1

        num = 0

        for m in range(0, 50):

            kk = 0

            if day_course_crash[m] > 1:

                num += 1

                if m < 10:

                    print '0'+str(m),

                else:

                    print str(m),

                for j in range(0, num_of_course):

                    if day_time[j] == m:

                        kk += 1

                        if kk == day_course_crash[m]:

                            print course[j]

                        else:

                            print course[j],

                    else:

                        pass

            else:

                pass

        if num == 0:

            print "YES"

    except EOFError, e:

        break

#提交是40%,本地运行没问题

2017-09-16 17:10
#include<iostream>
#include<map>
#include<utility>
#include<string>
using namespace std;
int main(){
	multimap<string,string> c,record;
    int n;
    string x,y;
    cin >> n;
    for(int i = 0; i<n;++i){
    	cin >> x >> y;
        c.insert(make_pair(x,y));
    }
    int sum = 0;
    for(multimap<string,string>::iterator iter = c.begin(); iter != c.end();++iter){
        if(record.find(iter->first)== record.end()){
            int count = c.count(iter->first);
            sum += count-1;
            if(count>1){
                cout << iter->first<<' ';
                pair<multimap<string,string>::iterator,multimap<string,string>::iterator> r = c.equal_range(iter->first);
                for(multimap<string,string>::iterator it = r.first; it !=r.second; ++it){
                    cout<<it->second<<' ';
                }
                //cout << (r.second)->second <<endl;;
                record.insert(make_pair(iter->first,iter->second));
            }
        }
       
    }
	 if(sum == 0)
            cout << "YES" << endl;
 return 0;
}


2017-09-16 17:10
#include<map>
#include<string>
#include<iostream>
#include<vector>
using namespace std;

int main() {
	map<string, vector<string>>m;
	int num = 0;
	int n;
	string s1, s2;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> s1 >> s2;
		m[s1].push_back(s2);
	}
	bool f = true;
	for (auto &e : m) {
		if (e.second.size() > 1) {
			f = false;
			int no = 0;
			cout << e.first;
			for (auto k : e.second) {
				cout << " " << k;
			}
			cout << endl;
		}
	}
	if (f) cout << "YES" << endl;

	return 0;
}

最后都没来的及,输出yes的情况,只差3秒钟。。。。。。。

2017-09-16 17:10
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
	int n;
	while (cin >> n)
	{
		string str;
		int code;
		map<string, int> m;
		map<string, vector<int>> res;
		for (int i = 0; i < n; i++)
		{
			cin >> str >> code;
			if (m[str] == 0)
				m[str] = code;
			else if (code != m[str])
			{
				if (res[str].empty())
				{
					res[str].push_back(m[str]);
					res[str].push_back(code);
				}
				else
				{
					res[str].push_back(code);
				}
			}
		}
		if (res.empty())
			cout << "YES" << endl;
		else
		{
			for (auto it = res.begin(); it != res.end(); it++)
			{
				cout << it->first;
				for (int j = 0; j < res[it->first].size(); j++)
				{
					cout << " " << res[it->first][j];
				}
				cout << endl;
			}
		}
	}
	return 0;
}


2017-09-16 17:11

直接一个map记录一个vector链就好了。


AC代码:

#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <map>
#include <vector>
#include <cstdio>

using namespace std;

const int maxn = 100;
map<int, vector<string> > maps;
int n;
char code[maxn];
int main() {
//    freopen("in.txt", "r", stdin);
    while (scanf("%d", &n) != EOF) {
        maps.clear();
        for (int i = 0; i < n; i++) {
            char s[5];
            scanf("%s%s", s, code);
            int day = s[0] - '0';
            int num = s[1] - '0';
            int id = day * 10 + num;
            string str_code(code);
            if (maps.count(id)) {
                maps[id].push_back(str_code);
            }
            else {
                vector<string> code_list;
                maps[id] = code_list;
                maps[id].push_back(str_code);
            }
        }
        int flag = 0;
        for (int i = 0; i < 50; i++) {
            if (maps.count(i)) {
                if (maps[i].size() > 1) {
                    int day = i / 10;
                    int num = i % 10;
                    printf("%d%d", day, num);
                    for (int j = 0; j < maps[i].size(); j++) {
                        cout << " " << maps[i][j];
                    }
                    cout << endl;
                    flag = 1;
                }

            }
        }
        if (!flag) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


2017-09-16 17:11
coder_FYY2Z35H 回复 coder_yYpE1vJ5

赛码上只要涉及到String的都没对过,本地调试结果各种美滋滋,提交就是不过

2017-09-16 17:14
numCourses = int(input())
xuanke = {}
while numCourses:
    temp1,temp2 = input().strip().split()
    if temp1 in xuanke.keys():
        xuanke[temp1] = xuanke[temp1]+[temp2]
    else:
        xuanke[temp1] = [temp2]
    numCourses -=1 
    
chongtu = {}
flag = 0
for key,value in xuanke.items():
    if len(value)>1:
        chongtu[key]=value
        flag = 1

if flag == 0:
    print('YES')
else:
    chongtu = sorted(chongtu.items(),key=lambda d:int(d[0]))
    for key,value in chongtu:
        mstr = key
        for i in value:
            mstr += ' '+str(i)  
        print(mstr)

考完之后写的 不知道对不对

2017-09-16 17:19
coder_K5CRWHMA 回复 coder_FYY2Z35H

真的吗?赛码新手…有没有破解方法

2017-09-16 17:24
coder_27EJCdfX 回复 coder_yYpE1vJ5

用scanner 的nextline 没问题的

2017-09-16 17:33

Map<String, List<String>>,key是时间,value是课程id。filter一下size>1,然后排序输出就结了

2017-09-16 17:37
#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct stru{
	string str;
	int course;
	int flag;//是否重复标记
	int fl;//是否已输出标记
};
int main()
{
	stru arr[100];
	int n;
	cin>>n;
	int i,j;
	for(i=0;i<n;i++)
	{
		cin>>arr[i].str>>arr[i].course;
		arr[i].flag=0;
		arr[i].fl=0;
	}
	int count=0;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(arr[i].str[0]==arr[j].str[0] && arr[i].str[1]==arr[j].str[1] )
			{
				count=1;
				arr[i].flag=1;
			    arr[j].flag=1;
			}
		}
	}
	if(count==0)
	{
		cout<<"YES"<<endl;
		return 0;
	}
	for(i=0;i<n;i++)
	{
		if(arr[i].flag==1 && arr[i].fl==0)
		{
			cout<<arr[i].str<<" "<<arr[i].course<<" ";
			for(j=0;j<n;j++)
			{
				if(i!=j && arr[i].str[0]==arr[j].str[0] && arr[i].str[1]==arr[j].str[1])
				{
					arr[i].fl=1;
					arr[j].fl=1;
					cout<<arr[j].course<<" ";
				}
			}
			cout<<endl;
		}
	}
	return 0;
}


2017-09-16 17:41
coder_8QvukiK1 回复 coder_FYY2Z35H

30%就是能输出YES的结果,70是有重复课程的

2017-09-16 17:42


import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

[@SuppressWarnings](/user/SuppressWarnings)("resource")

Scanner scanner=new Scanner(System.in);


while (scanner.hasNext()) {

List<String[]> list=new ArrayList<>();

int count =0;

boolean flag=false;

int n=Integer.parseInt(scanner.nextLine());

while (n>0) {

String string=scanner.nextLine();

String[] strings= string.split(" ");

list.add(strings);

n--;

}

String  result="";

for (int i = 0; i < list.size(); i++) {

for (int j = i+1; j < list.size(); j++) {

if (list.get(i)[0].equals(list.get(j)[0])) {

if (count==0) {

result=list.get(i)[0]+" "+list.get(i)[1];

count ++;

}

result+=" "+list.get(j)[1];

}

}

if (count!=0) {

System.out.println(result);

count=0;

flag=true;

}

}

if (!flag) {

System.out.println("YES");

}


}

}

}


2017-09-16 18:20

#include <iostream>

#include<string>

#include <vector>


using namespace std;


struct mp

{

public:

mp(int n, int m){

xy = n;

x = n/10;

y = n % 10;

z = m;

}

int xy;

int x;

int y;

int z;

};


int main()

{

int n;

cin >> n;

vector<mp> stu;

for (int i = 0; i < 5; i++)

{

int x, y;

cin >> x >> y;

stu.push_back(mp(x, y));

}

vector<mp> day[5];

vector<mp> time[5][10];

for (int i = 0; i < 5; i++)

{

day[stu[i].x].push_back(stu[i]);

}

for (int i = 0; i < 5; i++)

{

if (day[i].size() >= 2)

{

int j = 0;

while (j < day[i].size())

{

time[i][day[i][j].y].push_back(day[i][j]);

j++;

}

}

}

int result = 0;

for (int i = 0; i < 5; i++)

{

for (int j = 0; j < 10; j++)

{

if (time[i][j].size() >= 2)

{

result += time[i][j].size();

if (time[i][j][0].x==0)

cout <<0<< time[i][j][0].y << " ";

else


   cout << time[i][j][0].xy << " ";

int jj = 0;

for ( jj = 0; jj < time[i][j].size()-1; jj++)

{

cout << time[i][j][jj].z << " ";

}

cout << time[i][j][jj].z << endl;

}

}

}

if (result == 0)

{

cout << "YSE" << endl;

}


system("pause");

return 0;

}



2017-09-16 20:54


#include <iostream>

#include<string>

#include <vector>


using namespace std;


struct mp

{

public:

mp(int n, int m){

xy = n;

x = n/10;

y = n % 10;

z = m;

}

int xy;

int x;

int y;

int z;

};








int main()

{

int n;

cin >> n;

vector<mp> stu;

for (int i = 0; i < n; i++)

{

int x, y;

cin >> x >> y;

stu.push_back(mp(x, y));

}

vector<mp> day[5];

vector<mp> time[5][10];

for (int i = 0; i < n; i++)

{

day[stu[i].x].push_back(stu[i]);

}

for (int i = 0; i < 5; i++)

{

if (day[i].size() >= 2)

{

int j = 0;

while (j < day[i].size())

{

time[i][day[i][j].y].push_back(day[i][j]);

j++;

}

}

}

int result = 0;

for (int i = 0; i < 5; i++)

{

for (int j = 0; j < 10; j++)

{

if (time[i][j].size() >= 2)

{

result += time[i][j].size();

if (time[i][j][0].x==0)

cout <<0<< time[i][j][0].y << " ";

else


   cout << time[i][j][0].xy << " ";

int jj = 0;

for ( jj = 0; jj < time[i][j].size()-1; jj++)

{

cout << time[i][j][jj].z << " ";

}

cout << time[i][j][jj].z << endl;

}

}

}

if (result == 0)

{

cout << "YSE" << endl;

}


system("pause");

return 0;

}



2017-09-16 21:15



#include <iostream>

#include<string>

#include <vector>


using namespace std;


struct mp

{

public:

mp(int n, int m){

xy = n;

x = n/10;

y = n % 10;

z = m;

}

int xy;

int x;

int y;

int z;

};


int main()

{

int n;

cin >> n;

vector<mp> stu;

for (int i = 0; i < n; i++)

{

int x, y;

cin >> x >> y;

stu.push_back(mp(x, y));

}

vector<mp> data[59];//最多五天每天10节课

for (int i = 0; i < stu.size(); i++)

{

data[stu[i].xy].push_back(stu[i]);

}

int result = 0;

for (int j = 0; j < 59; j++)

{

if (data[j].size() >= 2)

{

result = 1;

if (data[j][0].x==0)

cout <<0<< data[j][0].y << " ";

else

   cout << data[j][0].xy << " ";

int jj = 0;

for (jj = 0; jj < data[j].size()-1; jj++)

{

cout << data[j][jj].z << " ";

}

if (jj = data[j].size() - 1)

cout << data[j][jj].z << endl;

}

}

if (result == 0)

cout << "YES" << endl;

system("pause");

return 0;

}



2017-09-16 21:36
添加回复
回到顶部