深信服实习生笔试题-20190315

好久没写编程题了,手生的厉害,记录一下。

首先做的第二题,比较简单,ac。

题目要求:

比较简单,用结构体记录前后数字区间,按照start排序,当start相同时,按照end排序。然后循环合并即可。

测试用例:

4
3 8
3 7
4 6
7 9

5
3 8
3 7
4 6
7 9
1000 1100

4
0 1
3 7
10 11
8 9
View Code

代码:

#include <iostream>
#include <algorithm>
using namespace std;

struct st{
    long long  a,b;
};
bool cmp(st aa, st bb){
    if(aa.a==bb.a)
        return aa.b<bb.b;
    else
        return aa.a<bb.a;
}
int main()
{
    int n;
    while(cin >> n)
    {
        st arr[n];
        for(int i=0;i<n;i++)
        {
            cin >> arr[i].a >> arr[i].b;
        }
        sort(arr,arr+n,cmp);

//         for(int i=0;i<n;i++)
//        {
//            cout << arr[i].a <<" " << arr[i].b << endl;
//        }
//        cout << endl;

        st tem;
        tem.a=arr[0].a;
        tem.b=arr[0].b;
        for(int i=0;i<n;i++)
        {
            if(arr[i].a<=tem.b)
            {
                if(tem.b<arr[i].b){
                    tem.b=arr[i].b;
                }
            }
            else{
                cout << tem.a << " " << tem.b << endl;

                    tem.a=arr[i].a;
                    tem.b=arr[i].b;

            }
        }
         cout << tem.a << " " << tem.b << endl;


    }
    return 0;
}

然后做的第一题,第一题比第二题简单,不过需要注意格式空间等问题,因为选择填空浪费了太多时间,没有全过。

第二题是给定几个字符串,找出所有重复的字符串的序号

输入:

扫描二维码关注公众号,回复: 5547038 查看本文章

第一行为n,字符串个数

第二行为m,字符串中数字的个数

第三行为字符。

可以把一行数字当做字符串处理,然后判断字符串是否相等。

测试用例:魔改版本,方便测试进行了一些修改,m值对不上

11
0
195 946
1
427
2
367 718 202 187 683 321 831 
3
1023 78 310 816 158 500 518 705 553 470
4
205 190 306 492 166 49 791 961
5
665 211 1009 614 15 683
6
195 946
7
678 198 495
8
205 190 306 492 166 49 791 961
9
83 74 1023 453 692
10
195 946
View Code

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

char arr[10000][10010];
int state[10010];

int main()
{
    int n;
    while(cin >> n)
    {
        memset(state,0,sizeof(state));
        int num;
        for(int i=0;i<n;i++)
        {
            cin >> num;
            char c;
            scanf("%c",&c);
            gets(arr[i]);
        }
        for(int i=0;i<n;i++)
        {
            if(state[i]!=0) continue;
            state[i]=1;
            int a=0;
            for(int j=i+1;j<n;j++)
            {
                if(state[j]!=0) continue;
                //cout << i << " " << j << " " << strcmp(arr[i],arr[j]) << endl;
                if(strcmp(arr[i],arr[j])==0)
                {
                    state[j]=1;
                    //cout << "state " << j << endl;
                    if(a==0){
                        cout << i;
                        a=1;
                    }
                    cout <<  " " << j;
                }
            }
            if(a==1)
                cout <<endl;
            a=0;

        }
    }
    return 0;
}

用这个方法要注意输入格式,不要有多余的空格。

记录一个愚蠢的版本,把state[i]==0放到了for循环中,导致当i标记为0时,运行到此便停止,应该是跳过标记为0的点,简直愚蠢。

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

char arr[10000][10010];
int state[10010];

int main()
{
    int n;
    while(cin >> n)
    {
        memset(state,0,sizeof(state));
        int num;
        for(int i=0;i<n;i++)
        {
            cin >> num;
            char c;
            scanf("%c",&c);
            //cout << num << "num " << endl;

            gets(arr[i]);
            //cout << arr[i];
        }
        for(int i=0;i<n&&state[i]==0;i++)
        {
            
            state[i]=1;
            int a=0;
            for(int j=i+1;j<n&&state[j]==0;j++)
            {
                //cout << i << " " << j << " " << strcmp(arr[i],arr[j]) << endl;
                if(strcmp(arr[i],arr[j])==0)
                {
                    state[j]=1;
                    //cout << "state " << j << endl;
                    if(a==0){
                        cout << i;
                        a=1;
                    }
                    cout <<  " " << j;
                }
            }
            if(a==1)
                cout <<endl;
            a=0;

        }
    }
    return 0;
}

跟同学投的不是一个公司,自己做的,应该是凉了

猜你喜欢

转载自www.cnblogs.com/vactor/p/10539927.html
今日推荐