Niuke network: Huawei 2016 R & D Engineer programming problem (C + +)

Keywords: Programming less

1,

Delete number

Time limit: 1 second

Space limit: 32768K

There is an array a[N] to store 0~N-1 in order. It is required to delete one number every two numbers. At the end of the array, cycle to the beginning to continue, and find the original subscript position of the last deleted number. Take 8 numbers (N=7) for example: {0, 1, 2, 3, 4, 5, 6, 7}, 0 - > 1 - > 2 (delete) - > 3 - > 4 - > 5 (delete) - > 6 - > 7 - > 0 (delete), so cycle until the last number is deleted.
 

Enter a description:

Each group of data is a n integer n (less than or equal to 1000), which is the number of array members. If it is greater than 1000, a[999] is calculated.

 

Output Description:

One line outputs the original subscript position of the last deleted number.

 

Input example 1:

8

 

Output example 1:

6
#include<iostream>
using namespace std;
int main()
{
    struct ListNode
    {
        int val;
        ListNode* next;
        ListNode(int x):val(x),next(NULL){}
    };
    int n;
    while(cin>>n)
    {
        if(n>1000)
        {
            n=999;
        }
        ListNode* phead=new ListNode(0);  
        ListNode* p=phead;
        for(int i=1;i<n;i++)
        {
            p->next=new ListNode(i);
            p=p->next;
        }
        p->next=phead;
        p=p->next;
        while(n)
        {
            p->next->next=p->next->next->next;
            p=p->next->next;
            n--;
        }
        cout<<p->val<<endl;       
    }
    return 0;  
}

2,

Character set

Time limit: 1 second

Space limit: 32768K

Enter a string to find the set of characters it contains
 

Enter a description:

Enter a string for each group of data. The maximum length of the string is 100, and it only contains letters. It cannot be an empty string. It is case sensitive.

 

Output Description:

One line for each group of data, output the character set according to the original character sequence of the string, that is, the letters that appear repeatedly and come back do not output.

 

Input example 1:

abcqweracb

 

Output example 1:

abcqwer
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    string s;
    while(cin>>s)
    {
        int n=s.length();
        vector<int> tmp(52,0);
        string res="";
        for(int i=0;i<n;i++)
        {
            if(s[i]>='a' && s[i]<='z')
            {
                if(0==tmp[s[i]-'a'])
                {
                    res+=s[i];
                    tmp[s[i]-'a']=1;
                }
            }
            else
            {
                if(0==tmp[s[i]-'A'+26])
                {
                    res+=s[i];
                    tmp[s[i]-'A'+26]=1;
                }
            }
        }
        cout<<res<<endl;
    }
    return 0;
}

 

Posted by collette on Wed, 04 Dec 2019 17:19:13 -0800