Hackerrank && Network

来源:互联网 发布:老铁seo 编辑:程序博客网 时间:2024/05/12 02:02

1.Hackerrank

一个在N个数字里,选K个数字,从N个数字里选出K个数字的子集,使得此子集中的数字之间差的最大值比其他任何子集中的相差值小。

先输入N,然后输入K,接着输入N个数字

如input:9,3,1,2,3,100,101,5,6,7,8

表面输入9个数字,子集大小为3个数字:

请找出{1,2,3,100,101,5,6,7,8}子集数为3,且“贫富差距”最小的一组。

............................

............................

开始拿到这题让我想到了Leetcode上的一道十分相似的题目,半天没什么思绪,变去Github上找到一个牛写的答案,看了之后茅塞顿开,并且见识到了工业级代码的风格,人家走来调用STL中的strol、getline,齐刷刷的函数我还得去问度娘,而且他走来是一个输入的范围判断,如果超出则抛出异常,学习咯。

思路很简单,先利用sort()对输入数组进行排序,再依次遍历。

code:

#include <cmath>#include <cstdio>#include <vector>#include <iostream>#include <algorithm>using namespace std;// It is NOT mandatory to use the provided template. You can handle the IO section differently.int main() {/* The code required to enter n,k, candies is provided*/int N, K, unfairness,temp;vector<int> candies;cin >> N >> K;for (int i = 0; i < N; i++){cin >> temp;candies.push_back(temp);}sort(candies.begin(),candies.end());unfairness = candies[K - 1] - candies[0];for (int i = 0; i <= N-K ; i++){if (unfairness>(candies[i + K - 1] - candies[i]))unfairness = candies[i + K - 1] - candies[i];}cout << unfairness << endl;return 0;system("pause");}

2.Network

Today, I studied ICMP and IP protocols. I learned that Ethernet is a kind of protocol in Data Link Layer. And there are some protocols in this layer link like Token Ring、FDDI、ARCNET. The formation of IP protocal must have at least 20 Bytes in its header, including VER(version,usually like 4 or 6) and HLEN(header length, range:5~15) and Toal Length、Identification、Flags(which determine whether it can or can't be separate into fragment and status if it's the last fragmentation of a sequence)、Fragmentation offset、Time to live(this was originally designed to hold a timestamp, a number which means at most we could transfer from a router to other router,and it was decremented by each visited router)、Protocol(the higher level protocol that uses the services of the IP layer)、Header checksumthe Source and destination IP address.

ICMP is another protocol used in the Network layer. It's mainly help the IP protocol to solve some error that happened in this layer. 


3.Leetcode

刷了道30的题,秒刷啊。。然后被一道20的题搞到现在,妈蛋,睡觉去。。fuck...

0 0