2017 51信用卡秋招笔试题——JAVA研发

编程1

具体题目意思应该是,有一群候选人和一群握有投票权的人,不同的人投票的权重不一样,比如,如果一个人投票权重是2,则相当于是投了两票。要求当选的候选人ID,注:相同权重和的候选人,得票数多的获胜。也就是说如果候选人1,拿了2票权重都是1;候选人2,拿了一票权重是2,则候选人为1,并且不存在权重和票数都一样的两个候选人。

这里写图片描述
分析:咋一看这题目觉得很简单,但是真正在做的时候费了好多时间,主要问题出在,候选人的ID不确定,例如:可能有两个人,一个候选人ID为1,另一个ID为1000。因此在求得时候不能用数组下标表示候选人ID。
楼主采用的数据结构是:采用了一个vector保存所有候选人id,同时建立了一个结构体

struct personInfo
{
    int id;
    int num;//保存此候选人的得票数
    int sum;//保存此候选人的得票总和
}

然后使用sort针对结构体排序,重写cmp

bool cmp(const personInfo& a, const personInfo& b)
{
    if(a.sum != b.sum)
        return a.sum > b.sum;
    else
        return a.num > b.num;
}

完整代码如下:

#include<bits/stdc++.h>
using namespace std;
const int maxN = 10005;
int V[maxN];
int W[maxN];
vector<int> id;
personInfo pi[maxN];
struct personInfo
{
    int id;
    int num;
    int sum;
};
bool cmp(const personInfo& a, const personInfo& b)
{
    if (a.sum != b.sum)
        return a.sum > b.sum;
    else
        return a.num > b.num;
}

int main()
{

    vector<int>  buffVec;
    int temp;
    while (cin >> temp)
    {
        buffVec.push_back(temp);
    }
    int len = buffVec.size();
    int lim = len / 2;
    int position = 0;
    //输入处理前半部分为选票,后半部分为权重
    for (int i = 0; i < lim; i++)
    {
        V[position++] = buffVec[i];
    }
    position = 0;
    for(int i = lim; i < len; i++)
    {
        W[position++] = buffVec[i];
    }
    vector<int>::iterator it;
    //统计所有候选人的总得票数和总得票权重
    for (int x = 0; x < position; x++)
    {
        //如果此候选人已得过票则直接在此基础上修改;否则添加新的候选人信息
        it = find(id.begin(), id.end(), V[x]);
        if (it == id.end())
        {
            id.push_back(V[x]);
        }
        it = find(id.begin(), id.end(), V[x]);
        pi[*it].id = *it;
        pi[*it].num++;
        pi[*it].sum += W[x];
    }
    sort(pi, pi + maxN, cmp);
    cout << pi[0].id << endl;
}

编程题2

这里写图片描述
一道简单的多线程题

//C++版本
#include<bits/stdc++.h>
#include<condition_variable>
#include<thread>
using namespace std;
queue<string> Q;
int c;
condition_variable_any m_t;
mutex loc;
int i = 1;
void foo()
{
    string str;
    lock_guard<mutex> locker(loc);
    while (i == 0)
    {
        m_t.wait(loc);
    }
    i = 0;
    while (!Q.empty())
    {
        str = Q.front();
        Q.pop();
        string::size_type idx = str.find("u51");
        if (idx != string::npos)
            c++;
    }
    i = 1;
}

int main()
{
    string s;
    while (cin >> s)
    {
        Q.push(s);
    }
    thread t1(foo);
    thread t2(foo);
    thread t3(foo);
    t1.join();
    t2.join();
    t3.join();
    cout << c << endl;

}
//java版本
package wzsb;
import java.util.LinkedList;
import java.util.Scanner;
class FindU51 extends Thread{

    static int count;
    static LinkedList<String> arrList = null;

    public FindU51(LinkedList<String> arrs) {
        super();
        this.arrList = arrs;
    }
    public FindU51(){}

    public synchronized void findU51(){
        while(arrList.size()>0){
            String str = arrList.removeFirst();
            if(str.contains("u51"))
                count++;
        }

    }
    @Override
    public void run() {
        super.run();
        findU51();
    }

}
public class Sbwz {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        LinkedList<String> arrList = new LinkedList<>();
        while(sc.hasNext()){
            arrList.addLast(sc.next());
        }   
        FindU51 fU51 = new FindU51(arrList);    
        Thread t1 = new Thread(fU51,"thread1");
        Thread t2 = new Thread(fU51,"thread2");
        Thread t3 = new Thread(fU51,"thread3");
        t1.start();
        t2.start();
        t3.start();
        try {
            t1.join();
            t2.join();
            t3.join();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        System.out.println(fU51.count);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值