首页 > 代码库 > 今日头条2017校招编程题

今日头条2017校招编程题

技术分享

技术分享

贪心?瞎搞  先排个序,然后扫一遍,边扫边维护一个数组v   v的长度为3    设vs数组的最后一个是x 如果abs(a[i]-x)大于10 那么贪心的策略我就 添加个v+10相应的ans++.

然后注意一些细节就好了...  我代码写的好挫

/* ***********************************************Author        :guanjunCreated Time  :2016/9/21 19:50:13File Name     :1001.cpp************************************************ */#include <iostream>#include <cstring>#include <cstdlib>#include <stdio.h>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <iomanip>#include <list>#include <deque>#include <stack>#define ull unsigned long long#define ll long long#define mod 90001#define INF 0x3f3f3f3f#define maxn 10010#define cle(a) memset(a,0,sizeof(a))const ull inf = 1LL << 61;const double eps=1e-5;using namespace std;priority_queue<int,vector<int>,greater<int> >pq;struct Node{    int x,y;};struct cmp{    bool operator()(Node a,Node b){        if(a.x==b.x) return a.y> b.y;        return a.x>b.x;    }};bool cmp(int a,int b){    return a>b;}int a[100010];int v[100010];int main(){    #ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);    #endif    //freopen("out.txt","w",stdout);    int n;    while(cin>>n){        for(int i=1;i<=n;i++){            scanf("%d",&a[i]);        }        sort(a+1,a+1+n);        int ans=0;        int cnt=0;        for(int i=1;i<=n;i++){        //    cout<<a[i]<<endl;        int mark=0;            if(cnt>=1&&cnt<3){                int tmp=v[cnt];                if(abs(tmp-a[i])>10){                    ans++;v[++cnt]=tmp+10;                    mark=1;                }                else{                    v[++cnt]=a[i];                }            }            else if(cnt==0){                v[++cnt]=a[i];            }            if(cnt==3){                if(mark)i--;                cnt=0;            }        }        if(cnt>0)ans+=3-cnt;        printf("%d\n",ans);    }    return 0;}

 

今日头条2017校招编程题