2016 360校招笔试编程题

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值