毅想天开

导航

成都某游戏开发的三道编程题

1. 打印杨辉三角;

 1 void printYangHuiTriangle(int nLine)
 2 {
 3     int **arrayTriangle = NULL;
 4     arrayTriangle = new int*[nLine];
 5     for (int x = 0; x < nLine; ++x)
 6         arrayTriangle[x] = new int[x + 1];
 7 
 8     for (int x = 0; x < nLine; ++x)
 9     {
10         arrayTriangle[x][0] = 1;
11         arrayTriangle[x][x] = 1;
12         for (int y = 1; y < x; ++y)
13         {
14             arrayTriangle[x][y] = arrayTriangle[x-1][y-1] + arrayTriangle[x-1][y];
15         }
16     }
17 
18     printf("%d\t\n", arrayTriangle[0][0]);
19     for (int x = 0; x < nLine; ++x)
20     {
21         for (int y = 0; y < x + 1; ++y)
22         {
23             printf("%d\t", arrayTriangle[x][y]);
24         }
25         printf("\n");
26     }
27 
28     for (int x = 0; x < nLine; ++x)
29         delete [] arrayTriangle[x];
30 
31     delete []arrayTriangle;
32 }

 

2. 实现两个大数据之间的相加(数据位数可能很大比方100位)

void addBigs(const char *pStr1, const char *pStr2)
{
    if ((pStr1 == NULL) || (pStr2 == NULL) || (*pStr1 =='\0') || (*pStr2 =='\0'))
    {
        printf("Result=0\n");
        return;
    }

    int nLen1 = strlen(pStr1);
    int nLen2 = strlen(pStr2);
    const char *pStr1End = pStr1 + nLen1 - 1;
    const char *pStr2End = pStr2 + nLen2 - 1;
    int nMin = (nLen1 < nLen2) ? nLen1 - 1 : nLen2 - 1;
    int nMax = (nLen1 > nLen2) ? nLen1 : nLen2;
    char *pResult = new char[nMax + 2];
    memset(pResult, 0, nMax + 2);
    char *pEnd = pResult + nMax;
    int nCarry = 0;
    int nIndex = nMin;
    while (nIndex >= 0)
    {
        int nTemp = nCarry + (*pStr1End - '0') + (*pStr2End - '0');
        nCarry = nTemp / 10;
        nTemp = nTemp % 10;
        *pEnd = nTemp + 0x30;

        --nIndex;
        --pStr1End;
        --pStr2End; 
        --pEnd;
    }

    while (pStr1End >= pStr1)
    {
        int nTemp = nCarry + (*pStr1End - '0');
        nCarry = nTemp / 10;
        nTemp = nTemp % 10;
        *pEnd = nTemp + 0x30;
        --pStr1End;
        --pEnd;
    }
    *pEnd = nCarry + 0x30;

    while (pStr2End >= pStr2)
    {
        int nTemp = nCarry + (*pStr2End - '0');
        nCarry = nTemp / 10;
        nTemp = nTemp % 10;
        *pEnd = nTemp + 0x30;
        --pStr2End;
        --pEnd;
    }
    *pEnd = nCarry + 0x30;

    printf("Reslt=%s\n", pResult);

    delete []pResult;
}

 

3.有100,0000个0~100的随机数,算出每个数字出现的次数。

posted on 2017-02-26 21:59  CodeForJoy  阅读(229)  评论(0编辑  收藏  举报