網易 2019校招C++研發工程師筆試卷(有道)編程題-2018.09.08

1
1-2.png
1-3.png

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin >> n;
    if(n <= 5){
        cout << 1 << endl;
        return 0;
    }
    int res = n/5;
    if(n%5 != 0)
        res++;
    cout << res << endl;
    return 0;
}

2-1.png
2-2.png
2-3.png
這道題是找規律的題,沒法詳細講解,只能舉例子挨個列出來觀察
1. 只要行或者列其中有一個是2,輸出0
2. 行或者列都是1,輸出1
3. 行或者列其中一個是1,另一個大數大於3的話,輸出大數-2
4. 行或列均大於2,除了周圍一圈是朝上,其餘均朝下,輸出row*column - 2*row - 2*column + 4

#include <iostream>
#include <vector>
using namespace std;
void foo(long long row, long long column){
    long long res;
    if(row == 1){
        if(column >= 3)
            res = column - 2;
        if(column == 1)
            res = 1;
    }
    if(column == 1 && row >= 3){
        res = row - 2;
    }
    if(row==2 || column==2){
        res = 0;
    }
    if(row>2 && column>2){
        res = row*column - 2*row - 2*column + 4;

    }
    cout << res << endl;
}
int main()
{
    int t;
    cin >> t;
    for(int i=0; i<t; i++){
        long long row, column;
        cin >> row >> column;
        foo(row, column);
    }
    return 0;
}

3-1.png
3-2.png
3-3.png
3-4.png
後續補代碼

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