2014上机

2019-02-27  本文已影响0人  Chris_PaulCP3

2014

1、质因数分解(✔)
2、利用一维数组打印杨辉三角(✔)

int main()
{
    int rows,space, i, j;
    int res = 1;
    printf("行数: ");
    scanf("%d",&rows);
    for(i=0; i<rows; i++)
    {
        for(space = 0;space <= rows - i;space++)
            printf("  ");
        for(j = 0;j<=i;j++)
        {
            if(i == 0 || j == 0)
                res = 1;
            else
                res = res*(i - j + 1)/j;
            printf("%4d",res);
         } 
        
        printf("\n");
    }
 
    return 0;
}

3、两个一百位以内的大数相加(✔)

void add(char str1[],char str2[],char str3[])
{
    int ns1[100] = {0};
    int ns2[100] = {0};
    int len1 = strlen(str1);
    int len2 = strlen(str2);
    int i = 0,j = 0,k = 0,t = 0;
    if(len1 > len2)
    {
/****
ns1[0]为空的原因是防止次高位进位
ns1 [   ,   1   4   5   6]
ns2 [   ,       3   7   2]
**/ 
        while(i < len1)
        {
            ns1[i+1] = str1[i] - '0';
            ++i;
        }
        i = 0;
        while(i < len2)
        {
            ns2[i+len1-len2+1] = str2[i] - '0';
            ++i;
        }
    }
    else
    {
        while(i < len1)
        {
            ns1[i+len1-len2+1] = str1[i] - '0';
            ++i;
        }
        
        i = 0;
        while(i < len2)
        {
            ns2[i+1] = str2[i] - '0';
            ++i;
        }
    }
    k = i = len1>len2?len1:len2;
    while(i > 0)
    {
        //t表示进位 
        t = (ns1[i] + ns2[i])/10;
        ns1[i] = (ns1[i] + ns2[i])%10;
        --i;
        ns1[i] += t;
    }
    if (ns1[0] != 0)
        while (i<=k)
            str3[i] = ns1[i]+'0', ++i;
    else
        while (i+1<=k)
            str3[i] = ns1[i+1]+'0', ++i;
        str3[i] = '\0';
}
上一篇下一篇

猜你喜欢

热点阅读