牛客網-好未來秋招

編程題一

將一句話的單詞進行倒置,標點不倒置。比如 I like beijing. 經過函數後變爲:beijing. like I

import java.util.Scanner;
public class Main {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        String a = scanner.nextLine();
        String m[] = a.split(" ");
        for(int i =m.length-1;i>=0;i--){
            if(i==0)
                System.out.print(m[i]);
            else
                System.out.print(m[i]+" ");
        }

    }
}

編程題二

輸入兩個字符串,從第一字符串中刪除第二個字符串中所有的字符。例如,輸入”They are students.”和”aeiou”,則刪除之後的第一個字符串變成”Thy r stdnts.”
有點投機取巧 尷尬

include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int main(){
    char a[500];
    char b[500];
    cin.get(a,500);
    cin.ignore();
    cin.get(b,500);
    int m = strlen(a);
    int n = strlen(b);
    for(int i =0;i < m;i++){
        for(int j =0;j<n;j++){
            if(a[i] == b[j])
                a[i]='*';
        }
    }

    for(int q = 0;q<m;q++){
        if(a[q]!='*')
            cout<<a[q];
    }
    return 0;
}

編程題三

輸入兩個整數 n 和 m,從數列1,2,3…….n 中隨意取幾個數,使其和等於 m ,要求將其中所有的可能組合列出來;
輸入5 5
輸出
1 4
2 3
5
這道題可以採用遞歸加回溯的方法解決,也不難。

#include <iostream>
#include <vector>
using namespace std;
void find(vector<vector<int>> &res, vector<int> &t, int now, int sum, int n, int m);
int main(){
    int n, m;
    cin >> n;
    cin >> m;
    vector<vector<int>> res;
    vector<int>t;
    find(res, t, 0, 0, n, m);
    for (int i = 0; i < res.size(); i++){
        for (int j = 0; j < res[i].size(); j++){
            if (j == res[i].size() - 1)
                cout << res[i][j];
            else
                cout << res[i][j] << " ";
        }
        if (i != res.size() - 1){
            cout << endl;
        }
    }
    return 0;
}
void find(vector<vector<int>> &res, vector<int> &t, int now, int sum, int n,int m){
    if (sum == m){
        res.push_back(t);
        return;
    }
    if (sum > m)
        return;
    now++;
    if (now > n)
        return;
    t.push_back(now);

    find(res, t, now,sum+now, n, m);
    t.pop_back();
    find(res, t, now, sum, n, m);
}

後記

也是因爲最近收到好未來教育的實習生筆試通知,所以也要做個題目練習一下防止到時候一道題都做不出來很尷尬。
畢竟,這家公司不算是真正的互聯網企業,因此筆試的題目也不是太難,仔細想一下還是可以琢磨出來的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章