腾讯笔试

2019/9/20
只做了其中的两道题:

题目1:

题目描述:
在这里插入图片描述
输入描述:
在这里插入图片描述
输出描述:
在这里插入图片描述
实例:
在这里插入图片描述
解法:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        string s;
        cin>>s;
        int l = -1;
        for(int j=0; j<s.size(); j++)
        {
            if(s[j] == '8')
            {
                l = j;
                break;
            }
        }
        if(l == -1)
        {
            cout<<"NO"<<endl;
            continue;
        }
        else if(s.size()-l < 11)
        {
            cout<<"NO"<<endl;
            continue;
        }
        else
        {
            cout<<"YES"<<endl;
            continue;
        }
    }
    return 0;
}

题目4:

题目描述
在这里插入图片描述
输入描述:
在这里插入图片描述
输出描述:
在这里插入图片描述
示例:
在这里插入图片描述
解法1:

/*
7 5
5 8 10 3 6 10 8
 */
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
//从一个带有空格的输入字符中找出其中的数字
vector<int> get_num(string s){
    string s1 =s;
    vector<int> res;
    while (!s1.empty())
    {
        if (s1.find(" ") == string::npos)
        {
            res.push_back(stoi(s1));
            s1.clear();
            break;
        }
        string s_temp = s1.substr(0, s1.find(" "));
        res.push_back(stoi(s_temp));
        s1.erase(0, s1.find(" ") + 1);
    }
    return res;
}
//找出一个数组当中的非零最小数
int min_vt(vector<int> vt){
    vector<int> v2;
    for(int i = 0;i<vt.size();i++)
    {
        if(vt[i] > 0)
            v2.push_back(vt[i]);
    }
    int min = *min_element(v2.begin(),v2.end()) ;
    return min;
}
//数组减去其中的非零最小数
vector<int> sub_min(vector<int> vt){
    int min = min_vt(vt) ;
    for(int i = 0; i<vt.size();i++)
    {
        if((vt[i]-min)<0) vt[i] = 0;
        else vt[i] = vt[i]-min;
    }
    return vt;
}

int main()
{
    string line1,line2;
    getline(cin, line1);
    getline(cin, line2);
    vector<int> res1,res2;
    res1 = get_num(line1);
    res2 = get_num(line2);
    int min;
    for(int i = 0; i < res1[1];i++)
    {
        min =  min_vt(res2);
        cout<<min<<endl;
        res2 = sub_min(res2);
    }

    return 0;
}

解法2(同门写的解法):

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Resolution{
public:
    int num;
    int turns;
    vector<int> numArray;
    void getData(){
        numArray.clear();
        cin>>num>>turns;
        if(num>0){
            for(int i=0;i<num;i++){
                int temp;
                cin>>temp;
                numArray.push_back(temp);
            }
        }
        return;
    }
    void printData(){
        sort(std::begin(numArray), std::end(numArray));
        if(turns<=num){
            for(int j=0;j<=turns;){
                int minNum = numArray[j];
                cout<<minNum;
                for(int k=j;k<num;k++){
                    numArray[k] = numArray[k]-minNum;
                }
                while(numArray[j] ==0 ){
                    j++;
                }
            }
        }
    }
};

int main()
{
    Resolution r;
    r.getData();
    r.printData();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值