2018年小米校招笔试题

来源:互联网 发布:word数据转换成excel 编辑:程序博客网 时间:2024/05/15 23:43

我们设计了整数三角形,每一行都比上一行多出一个数,而每个数字都等于它上方与左上方两个数字之和,给出一个数字,求最先出现在哪一行

#include<iostream>#include<stdlib.h>#include<string.h>#define maxn 30int a[maxn][maxn];int main(){    using namespace std;    int z=0, x, y,tot=1;    int n;    memset(a, 0, sizeof(a));    for (int i = 1; i < maxn; ++i)    {        a[i][1] = tot;    }    for (int x = 2; x < maxn; ++x)    {        for (int y = 2; y < maxn; ++y)            a[x][y] = a[x - 1][y] + a[x - 1][y - 1];    }    cin >> n;    for (int c = 0; c < maxn; ++c)    {        for (int b = 0; b < maxn; ++b)        {            if (a[c][b] == n)            {                z = c;                break;            }            else continue;        }        if (z)        {            cout << z; break;        }        else continue;    }    system("pause");    return 0;}
原创粉丝点击