2016 360校招笔试编程题

来源:互联网 发布:淘宝怎么改评价用手机 编辑:程序博客网 时间:2024/06/05 00:31

360校招编程题

第一题

题目

      有一个小镇选举镇长,满足条件如下:
1. 每个人都认识自己;
2. 镇长必须不认识除了自己以外的任何人;
3. 每个人都必须认识镇长。

      输入:

  • 第一行为数据个数n,表示之后将有n组样本;
  • 接下来就是第一组数据,m:镇上的人数,k:下面将要写的人际关系;
  • 接下来就是人际关系,“q p”:第q个人认识第p个人(并不能表示p认识q);

      输出:

  • 该镇镇长候选人的个数,单独一行;
  • 每个候选人,分别用空格分开。

      i.e.

输入
3
2 0
3 2
2 1
3 1
4 5
1 1
2 1
3 1
4 1
3 2

输出
0

1
1
1
1

C语言代码

#include <stdio.h>#include <stdlib.h>typedef struct people_s{    unsigned int beKnown;    unsigned int knowWho;}people_t;void chooseCaptain();int main(){    int count = 0;    scanf("%d",&count);    while(count--)        chooseCaptain();    return 0;}void chooseCaptain(){    unsigned int numberOfPeople = 0;    unsigned int numberOfRelationship = 0;    unsigned int *captain;    unsigned int numberOfCaptain = 0;    int i = 0,pIndex = 0;    scanf("%d%d",&numberOfPeople,&numberOfRelationship);    captain = (unsigned int *)calloc(numberOfPeople,sizeof(unsigned int));    people_t *people = (people_t *)calloc(numberOfPeople,sizeof(people_t));    for(i=0,pIndex=0;i<numberOfRelationship;++i){        scanf("%d",&pIndex);        int tmp;        scanf("%d",&tmp);        if(tmp == pIndex)   continue;        else{            ++people[pIndex-1].knowWho;            ++people[tmp-1].beKnown;        }    }    //find the person    for(i=0;i<numberOfPeople;++i){        //printf("%d %d %d\n",i,people[i].beKnown,people[i].knowWho);        if(people[i].beKnown == numberOfPeople-1 && people[i].knowWho == 0)            captain[numberOfCaptain++] = i+1;    }    //print    printf("%d\n",numberOfCaptain);    for(i=0;i<numberOfCaptain;++i)        printf("%d ",captain[i]);    printf("\n");}

第二题

题目

      找出一个字符串中只出现一次的字符,且是第一个出现。

      输入:

  1. n:一共有n组数据;
  2. 一行一个字符串。

      i.e.

输入
2
asdssabass
helloworld

输出
d
h

C++代码

#include <iostream>#include <map>#include <string.h>using namespace std;class Solution{    public:        char firstSingleChar(string str);    private:        map<char,int> dict;};char Solution::firstSingleChar(string str){    dict.clear();    for(int i=0;i<str.length();++i){        if(dict[str[i]] == -1)            continue;        else if(dict[str[i]] == 0)            dict[str[i]] = i;        else            dict[str[i]] = -1;    }    char minChar = 0;    int minIndex = 0x7fffffff;    for(map<char,int>::const_iterator beg = dict.begin();            beg != dict.end();            ++beg){        if(beg->second == -1)            continue;        else            if(beg->second < minIndex){                minIndex = beg->second;                minChar = beg->first;            }    }    return minChar;}int main(){    Solution s;    int count = 0;    cin>>count;    string str = "";    while(count--){        cin>>str;        cout<<s.firstSingleChar(str)<<endl;    }    return 0;}
0 0
原创粉丝点击