天天看点

2012北航机试

代码均为自做

题目一

某些整数能分解成若干个连续整数的和的形式,例如

15 = 1 + 2+3+4+5 

15 = 4 + 5 + 6

15 = 7 + 8

某些整数不能分解为连续整数的和,例如:16

输入:一个整数N(N <= 10000)

输出:整数N对应的所有分解组合,按照每个分解中的最小整数从小到大输出,每个分解占一行,每个数字之间有一个空格(每行最后保留一个空格);如果没有任何分解组合,则输出NONE。

#include <iostream>

using namespace std;
void print(const int &a, const int &b) {
    for (int i = a;i <= b; i++) {
        cout << i << ' ';
    }
    cout << endl;
}
int main()
{
    int n;
    cin >> n;
    bool haveOutput = false;
    for (int i = 1; i <= n/2; i++) {
        for (int j = i+1; j <= n/2+1; j++) {
            if ((i+j)*(j-i+1)/2 == n) {
                print(i, j);
                haveOutput = true;
            }
        }
    }
    if (haveOutput == false) cout << "NONE" << endl;
    return 0;
}
           

题目二

小岛面积

1 1 1 1 1 1

1 1 0 0 0 1

1 0 0 0 1 0

1 1 0 1 1 1

0 1 0 1 0 0

1 1 1 1 1 1

上面矩阵的中的1代表海岸线,0代表小岛。求小岛面积(即被1中包围的0的个数)。注意:仅求这样的0,该0所在行中被两个1包围,该0所在列中被两个1包围。

输入:

第一行输入一个整数N,表示输入方阵的维数

输入一个N维方阵

输出:

小岛面积

样例输入:

6

1 1 1 1 1 1

1 1 0 0 0 1

1 0 0 0 1 0

1 1 0 1 1 1

0 1 0 1 0 0

1 1 1 1 1 1

样例输出:

8

#include <iostream>
#include <cstdlib>
using namespace std;
int N;
int **island;
bool **visit;
int **board;
int dir[4][2] = { {0, 1}, {0, -1}, {-1, 0}, {1, 0} };
int main()
{
    cin >> N;
    island = new int*[N];
    visit = new bool*[N];
    board = new int*[N]; // board 记录第 n 行 左右1所在列,和第n列上下的1所在的行
    for (int i = 0; i < N; i++) {
        island[i] = new int[N];
        visit[i] = new bool[N];
        board[i] = new int[4]; // 上下左右  o123
    }
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            island[i][j] = -1;
            visit[i][j] = false;
        }
        for (int j = 0; j < 4; j++) {
            board[i][j] = -1;
        }
    }
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cin >> island[i][j];
            if (island[i][j] == 1) {
                if (board[i][2] == -1) board[i][2] = j;
                if (board[j][0] == -1) board[j][0] = i;
                board[i][3] = j;
                board[j][1] = i;
            }
        }
    }
    cout << endl;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < N; j++) {
            cout << board[j][i] << ' ';
        }
        cout << endl;
    }
    int all = 0;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (island[i][j] == 0) {
                if (board[i][2] < j && board[i][3] > j && board[j][0] < i && board[j][1] > i) all++;
            }
        }
    }
    cout << all << endl;
    return 0;
}
/*
6
1 1 1 1 1 1
1 1 0 0 0 1
1 0 0 0 1 0
1 1 0 1 1 1
0 1 0 0 0 0
1 1 1 1 1 1
*/
           

题目三

输入一行C语言代码,查找关键字if,while,for并按照出现顺序输出。

输出格式:

关键字:位置

关键字:位置

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
bool fun(char ch) {
    if (ch >= 'a' && ch <= 'z') return false;
    if (ch >= 'A' && ch <= 'Z') return false;
    if (ch >= '0' && ch <= '9') return false;
    if (ch == '_') return false;
    return true;
}
int main()
{
    char str[101];
    gets(str);
    // cout << str <<endl;
    int len = strlen(str);
    for (int i = 0; i < len; i++) {
        if (str[i] == 'i') {
            if (i+1 >= len) continue;
            if (str[i+1] == 'f') {
                if (i-1 >= 0 && fun(str[i-1]) == false) continue;
                if (i+2 < len && fun(str[i+2]) == false) continue;
                cout << "if : " << i << endl;
            }
        }
        else if(str[i] == 'w') {
            if (i+4 >= len) continue;
            if (str[i+1] == 'h' && str[i+2] == 'i' && str[i+3] == 'l' && str[i+4] == 'e') {
                if (i-1 >= 0 && fun(str[i-1]) == false) continue;
                if (i+5 < len && fun(str[i+5]) == false) continue;
                cout << "while : " << i << endl;
            }
        }
        else if (str[i] == 'f') {
            if (i+2 >= len) continue;
            if (str[i+1] == 'o' && str[i+2] == 'r') {
                if (i-1 >= 0 && fun(str[i-1]) == false) continue;
                if (i+3 < len && fun(str[i+3]) == false) continue;
                cout << "for : " << i << endl;
            }
        }
    }
    return 0;
}
/*
for (int i = 0; i < len; i++) if(while1 == true);if (a == false) while(hoho) {}
*/