今日头条的笔试答案分享一下
发布于 2017-03-30 21:42 2398 次浏览 0 赞 来自 笔试面试  
#include<iostream>
#include<vector>
using namespace std;

void find_s(int* arr, int n, int* s1, int* s2)
{
	vector<int> max_v;
	vector<int> s1_v;
	vector<int> s2_v;
	int max=0;
	int falg = 0;
	for (int i = 1;i < n;i++)
	{
		int temp = 0;
		if (arr[i] > arr[i - 1])
		{
			s1_v.push_back(i - 1);
			int j = i;
			while (arr[j + 1] > arr[j] && j++ < n) {}
			if (j == n)
			{
				max_v.push_back((j - i));
				s2_v.push_back(n - 1);
				break;
			}
			while (arr[j + 1] < arr[j] && j++ < n) {}
			if (j == n)
			{
				max_v.push_back((j - i));
				s2_v.push_back(n - 1);
				break;
			}
			else
			{
				max_v.push_back((j - i+2));
				s2_v.push_back(j);
			}
		}
	}
	for (int i{};i < max_v.size();i++)
	{
		if (max_v.at(i) > max)
		{
			max = max_v.at(i);
			falg = i;
		 }
	}
	if (falg != 0)
	{
		*s1 = s1_v.at(falg);
		*s2 = s2_v.at(falg);
	}
	else
	{
		*s1 = -1;
		*s2 = -1;
	}
}

int main()
{
	int n{}, s1{}, s2{};
	int * p_arr;
	cin >> n;
	if (n > 0)
		p_arr = new int[n];
	else
		return 0;
	for (int i = 0;i < n;i++)
		cin >> p_arr[i];
	
	n = 10;
	find_s(p_arr, n, &s1, &s2);
	
	cout << s1 << " " << s2 << endl;

	delete[]  p_arr;
	cin.get();
	return 1;
}


3 条回复
#!/usr/bin/env python
# _*_coding:utf-8_*_
# __author__ = 'maple'
ls = {}

def process_strs(num_dict,num,cha_dict,cha):
    for key in cha_dict.keys():
        lists = cha_dict[key]
        ls.update({key:{}})
        for re in num_dict.keys():
            record = num_dict[re]
            ls[key].update({re:(len(set([ss.lower() for ss in lists if ss in record])))})


def print_data():
    for key in cha_dict.keys():
        maxs = ls[key]
        sf = max(maxs.items(), key=lambda x: x[1])[0]
        print ' '.join(num_dict[str(sf)])



if __name__ == '__main__':
    num = int(raw_input())
    cha = int(raw_input())
    i = 0
    j = 0
    num_dict = {}
    cha_dict = {}
    while i<num:
        strs = raw_input()
        num_dict.update({str(i+1):list(strs.split())})
        i += 1
    while j<cha:
        strs = raw_input()
        cha_dict.update({str(j+1):list(strs.split())})
        j += 1
    process_strs(num_dict,num,cha_dict,cha)
    print_data()


2017-03-30 21:43

原谅我写的比较烂,最后还因为考试系统让我晚进去半个小时,没有写完最后一个编程题

2017-03-30 21:54

第二题思路是啥?代码看不懂

2017-03-30 22:01
添加回复
回到顶部