算法面试题

给定一组数字,一组有9个数字,将这9个数字填写到3*3的九宫格内;使得横,竖,斜对角一条线上的三个数字之和相等;如果无解则打印无解;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

#include <iostream>

#include <algorithm>

using namespace std;

 

//a[9]为从小到大排好序的一维数组,b[3][3]为九宫格的二维数组

int jiuGongGe(int b[][3],int a[9])

{

    int x = 0, y = 1;

    b[x][y] = a[0]; //将最小的数字赋值给b[0][1],九宫格中第一行第二列的位置

    for (int k = 1; k < 9; k++)

    {

        int xNew = x - 1;

        int yNew = y + 1;   //依次向右向上寻找,将下一个数字放在该位置

        if (xNew < 0)

            xNew = 2;

        if (yNew > 2)

            yNew = 0;

        if (b[xNew][yNew] != 0){    //若该位置有数字了,则向下寻找,将下一个数字放在该位置

            xNew = x + 1;

            yNew = y;

        }

        b[xNew][yNew] = a[k];

        x = xNew;

        y = yNew;

    }

    int row1 = b[0][0] + b[0][1] + b[0][2];     //计算九宫格中每一行,列,斜对角线上的值

    int row2 = b[1][0] + b[1][1] + b[1][2];

    int row3 = b[2][0] + b[2][1] + b[2][2];

    int col1 = b[0][0] + b[1][0] + b[2][0];

    int col2 = b[0][1] + b[1][1] + b[2][1];

    int col3 = b[0][2] + b[1][2] + b[2][2];

    int dig1 = b[0][0] + b[1][1] + b[2][2];

    int dig2 = b[2][0] + b[1][1] + b[0][2];

    int flag = 0;   //比较横竖斜方向上的的值是否相等

    if (row1 == row2&&row1 == row3&&row1 == col1&&row1 == col2&&row1 == col3&&row1 == dig1&&row1 == dig2)

        flag = 1;

    return flag;

}

 

int main()

{

    int a[9];

    for (int i = 0; i < 9; i++)  //输入九个数字

        cin >> a[i];

    sort(a, a + 9); //对九个数字按照从小到大的顺序排序

    for (int i = 0; i < 9; i++)  //输出排好序的九个数字

        cout << a[i] << " ";

    cout << endl;

    int b[3][3] = { 0 };

    int flag = jiuGongGe(b, a);

    if (flag)   //若满足要求,则输入九宫格中的数组,否则输出无解

        for (int i = 0; i < 3; ++i){

            for (int j = 0; j < 3; ++j){

                cout << b[i][j] << "\t";

            }

            cout << endl;

        }

    else

        cout << "无解" << endl;

    system("pause");

    return 1;

}

《算法面试题》

《算法面试题》

《算法面试题》

#include <string.h>
#include <iostream>
using namespace std;

#define N 100

void tuse(int *p, int i, int j, int k)   //将数组a[0][0]的地址传给指针p,其位置为i,j,连通区域的标号k
{                                         //判断为小岛的点赋值为2,并进入该点上下左右的点,递归的进行扩展,将连通在一起的点,都赋值为k
    *(p + i*N + j) = k;
    if (*(p + i*N + j - 1) == 0)//左边
        tuse(p, i, j - 1, k);
    if (*(p + (i - 1)*N + j) == 0)//上面
        tuse(p, i - 1, j, k);
    if (*(p + i*N + j + 1) == 0)//右边
        tuse(p, i, j + 1, k);
    if (*(p + (i + 1)*N + j) == 0)//下面
        tuse(p, i + 1, j, k);
    return;
}

void computeArea(int *p, int br, int *p1, int *p2)    //计算第一大区域的面积与第二大区域的面积,返回给指针p1,p2
{
    for (int i = 0; i < br; i++)
        for (int j = 0; j < br; j++)
        {
            if (*(p + i*N + j) == 2)
                (*p1)++;
            if (*(p + i*N + j) == 3)
                (*p2)++;
        }
}

int main()
{
    int br, i, j, num = 0;
    int row[100][2];
    int col[100][2];
    int k = 2, area1 = 0, area2 = 0; //第一大岛的值全部为2
    int a[100][100] = { { 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 } };
    memset(row, -1, sizeof(row));
    memset(col, -1, sizeof(col));//compared with number 0
    br = 6;
    for (i = 0; i<br; i++)    //判断每行中最左边的1和最右边的1的标号row[i][0],row[i][1],每列中最上边的1和最下边的1的标号col[i][0],col[i][1]
    {
        for (j = 0; j<br; j++)
        {
            //scanf("%d", &a[i][j]);
            if (a[i][j] == 1)
            {
                if (row[i][0] == -1)
                    row[i][0] = j;
                if (col[j][0] == -1)
                    col[j][0] = i;
                row[i][1] = j;
                col[j][1] = i;
            }
        }
    }
    for (i = 0; i<br; i++)
    {
        for (j = 0; j<br; j++)
        {
            if (a[i][j] == 0)
            {
                if (j>row[i][0] && j<row[i][1] && i>col[j][0] && i<col[j][1]) //如果该点左边,右边,上边,下边有1,则判断该点为岛
                {
                    tuse(&a[0][0], i, j, k);        //进入该点,递归,将该点的连通区域都标记为k
                    k++;
                }
            }
        }
    }
    for (i = 0; i < br; i++)
    {
        for (j = 0; j < br; j++)            //输出更改标记后的矩阵a
            cout << a[i][j] << " ";
        cout << endl;
    }
    computeArea(&a[0][0], br, &area1, &area2);
    if (area2 != 0)                //如果有第二大小岛,则输出area2的面积
        cout << area2 << endl;
    else
        cout << area1 << endl;    //否则,输出最大小岛的面积area1
    system("pause");
    return 0;
}

 


 

《算法面试题》

 《算法面试题》

《算法面试题》

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

int maxProfit(vector<int> &prices) {
    int len = prices.size();
    if (len == 0)
        return 0;
    vector<int> pre(len);
    vector<int> post(len);
    int minPrice = prices[0];
    for (int i = 1; i<len; i++){                //计算第i点之前的最大利润pre
        minPrice = min(minPrice, prices[i]);
        pre[i] = max(pre[i - 1], prices[i] - minPrice);
    }
    int maxPrice = prices[len - 1];
    for (int i = len - 2; i >= 0; i--){        //计算第i点之后的最大利润post
        maxPrice = max(maxPrice, prices[i]);
        post[i] = max(post[i + 1], maxPrice - prices[i]);
    }
    int maxProfit = 0;
    for (int i = 0; i<len; i++){            //计算第i点的,pre[i]与post[i]之和的最大值,赋值给maxProfit
        maxProfit = max(maxProfit, pre[i] + post[i]);
    }
    return maxProfit;
}

int main()
{
    vector<int> array;
    vector <int>::iterator Iter;
    int num;
    cout << "please input a number" << endl;
    cin >> num;
    while (num != 0)            //循环输入array[i]中的值,直到输入0停止
    {
        array.push_back(num);
        cin >> num;
    }
    int maxp = maxProfit(array);
    cout << "最大利润为:"<<maxp << endl;
    system("pause");
    return 1;
}

《算法面试题》

《算法面试题》

来自:https://www.cnblogs.com/k7k8k91/p/8548319.html

    原文作者:langzi7758521
    原文地址: https://blog.csdn.net/LANGZI7758521/article/details/81542941
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞