编程练习 15.12.15~15.12.22

来源:互联网 发布:苹果医药软件 编辑:程序博客网 时间:2024/06/14 04:40

Set and sort

Input a couple of numbers (0~50) and end of EOF.

You should put them into a set that means remove all the duplicate(重复的,博主注) numbers.

And then sort the set,output all the numbers in the set by ascending oder.
Output format: behind the numbers, there is a space ’ ’ except the last number. There should be a ‘\n’ behind the last number.
For example:

[Input]

1 2 3 4 4 3 2 1 5 6 7 8 9 10
[Output]

1 2 3 4 5 6 7 8 9 10

读题

这道题就是要我们把一组数据去重,排序输出.
由于数据范围是0~50;刚好可以用数组下标表示各个数.
我选择了修改版的桶排序.

my answer

#include<stdio.h>int main() {    int a[51] = {0};    int temp, i, max = 0;    while (scanf("%d", &temp) != EOF) {        a[temp] = 1;        max = max < temp ? temp : max;    }    for (i = 0; i < max; i++) {        if (a[i] == 1)        printf("%d ", i);    }    printf("%d\n", max);    return 0;}

standard answer

#include <stdio.h>int main() {    int n, i, j, temp;    int count = 0;    int set[100] = {0};    while (scanf("%d", &n) != EOF) {        for (i = 0; i < count; ++i) {  // 查找是否在集合中            if (n == set[i])                break;        }        if (i == count) {  // 如果不在集合中,加入集合            set[count] = n;            count++;        }    }    for (i = 0; i < count - 1; ++i)  //  选择排序        for (j = i + 1; j < count; ++j)            if (set[i] > set[j]) {                temp = set[i];                set[i] = set[j];                set[j] = temp;            }    printf("%d", set[0]);  // 输出,可通过其他方式输出    for (i = 1; i < count; ++i)        printf(" %d", set[i]);    printf("\n");    return 0;}

反馈

难得啊,课堂实验题居然让我抢到了前十
答案这种做法比较具有普适性.
如果数据范围不是题中这么凑巧,只能用答案的做法. 去重再排序

或者像我在天梯上做过的一道题的思路, 排序再去重
将全部放在一个数组里,
输出的时候如果遇到相同的数就不再重复输出.

性能评估

题目描述:Amtel has announced that it will release a 128-bit computer chip by 2010, a 256-bit computer by 2020, and so on, continuing its strategy of doubling the word-size every ten years. (Amtel released a 64-bit computer in 2000, a 32-bit computer in 1990, a 16-bit computer in 1980, an 8-bit computer in 1970, and a 4-bit computer, its first, in 1960.)

Amtel will use a new benchmark - the Factstone - to advertise the vastly improved capacity of its new chips. The Factstone rating is defined to be the largest integer n such that n! can be represented as an unsigned integer in a computer word.

输入:Given a year 1960 ≤ y ≤ 2160, what will be the Factstone rating of Amtel’s most recently released chip?

输出:For test, there is one line of input containing y. For each test case, output a line giving the Factstone rating.

示例:

输入: 1960

输出: 3

(解释:因为1960年计算机是支持4位,其中的最大的无符号数为(全为1):15;3的阶乘为6,4的阶乘为24,故这里的n=3)

Hint:

1.取对数避免大数计算,log(n*m) = log(n) + log(m)。

2.准确理解题意。

读题

my answer

#include<stdio.h>#include<math.h>int main() {    int year, i = 2;    double temp = 0;    scanf("%d", &year);    int n = (year - 1960) / 10;    double bit = pow(2, n + 2);    while (temp <= bit) {        temp += log2(i++);    }    printf("%d\n", i - 2);    return 0;}

standard answer

// from younglee.#include<stdio.h>#include<math.h>int main(void) {    int year, num, n = 2;    double sum = 0;    // for input    scanf("%d", &year);    // for processing.    num = pow(2, (year - 1960)/10 + 2);    while (sum <= num) {        sum += log(n) / log(2);        ++n;    }    // for output.    printf("%d\n", n - 2);    return 0;}

反馈

1.以x为底,y的对数的求法 logx(y)
2.因为求的数是对数,所以数据类型用double(易错之一
3.最大数本应该减一,但是当数据的时候,减一对结果的影响非常小,所以可以忽略.这样可以避免先求幂再取对数这一步, 从而防止溢出(否则真的会溢出

How many ‘1’s are there

Description:
Description:

第一行输入数字n(n<=50),表示有n组测试用例,第2到第n+1行每行输入数m(m为整数),统计并输出m用二进制表示时,1的个数。

例如:m=9时,二进制表示为1001,则输出2.

Input:

2
3
7

Output:

2
3

Hint:
利用位运算

位运算

1
(1)、按位与(&),将两个操作数化为二进制后并将对应的每一位分别进行逻辑与操作。(a%(2^n)=a&(2^n-1))

(2)、按位或(|),将两个操作数化为二进制后并将对应的每一位分别进行逻辑或操作。

(3)、按位异或(^),和以上同,异或是指对应位相同则运算结果为0,否则为1。

(4)、按位取反(~),对每一位进行取反。(求x的相反数:x=(~x+1))

(5)、移位。分为左移(<<)和右移(>>),左移是按照指定的位数将一个数的二进制值向左移位,左移后,低位补0,移除的高位舍去,右移相反。

等价转换:a<

#include<iostream>using namespace std;int n=7,m=9;int main(){ cout<<(n&m)<<endl;     /********************     7: 0 0 0 0 0 1 1 1    9:(&) 0 0 0 0 1 0 0 1    _____________________          0 0 0 0 0 0 0 1     所以输出结果为1。     ********************/     cout<<(n|m)<<endl; //输出(1111)2=15     cout<<(n&1)<<endl; //判断一个数是否为奇数,n&1=1则为奇数,否则为偶数    cout<<(~n)<<endl; //(11111000)2=-8,前面的的1为符号位    cout<<(n^m)<<endl; //(1110)2=14    cout<<(n<<2)<<endl; //(11100)2=28    cout<<(m>>2)<<endl; //(10)2=2     n=n^m,m=m^n,n=n^m; //可以用来交换两个数值    /**********************************    n=n^m,所以m=m^n=m^(n^m)=(m^m)^n=7    n=n^m=(n^m)^(m^m)^n=(n^n)^(m^m)^m=9    **********************************/     cout<<n<<" "<<m<<endl;    return 0; }

位运算的应用:
求平均值:求(x+y)/2时,可能x+y会超过int的最大值,可以用位运算来求:

int Ave(int x,int y){ return x&y+((x^y)>>1);}判断一个数是否能够写成2的幂次方形式。 bool comp(int x){ if((x&(x-1))==0) return true;    return false;} 统计一个数的二进制数中1的个数。利用x=x&(x-1),会将x用二进制表示时最右边的一个1变为0,因为x-1会将该位变为0. int Count(int x){ int sum=0;    while(x)    { sum++;        x=x&(x-1);    }    return sum;} 

例题1:NYOJ 222(整数中的1),求区间[a,b]内所有整数的二进制的1的个数。位运算的强大应用~使用上面的代码的话肯定超时~~a,b的范围太大了呵呵,还是贴下,有些肯定用的上~~

#include<iostream>#include<cstdio> using namespace std;int Count(int x){ int sum=0;    while(x)    { sum++;        x=x&(x-1);    }    return sum;} int main(){ int a,b;    while(scanf("%d%d",&a,&b)!=EOF)    { int sum=0;        for(int i=a;i<=b;i++)            sum+=Count(i);        printf("%d\n",sum);     }     return 0;} 

方法2:打表法,都知道是以空间换时间,直接调用效率肯定是最高的~首先我们可以保存从0~2^32中每个数的二进制1的个数,然后直接调用即可,但是这样打表似乎太困难了点,这个时候我们可以值保存0~255中的每个数的二进制值中1的个数,然后进行分段查询即可。至于这个打0~255的表的话可以利用上面的代码打出~打表的程序:

#include<iostream>using namespace std;int Count(int x){ int sum=0;    while(x)    { sum++;        x=x&(x-1);    }    return sum;} int main(){ for(int i=0;i<256;i++)        cout<<Count(i)<<",";    cout<<endl;     return 0;} 

然后直接全选复制保存到一个数组中即可。将32位整形分为四段,求出每一段的1的个数和相加即为该数二进制值的1的个数,问题是如何求出每一段中的1的个数,比方说我要求后面k位数的1的个数,可以这样做:
//x * * * * * * * * * *
//y(&) ….1 1 1 1 1(y=2^k-1),有k个1
//_____________
//不管x的最后k位是什么,总之只有1&1=1,所以后面k位数与n&(2^k-1)这个数化为2进制的结果相同

下面的代码只是分为了四段,那么数组大小定义为256=2^8即可,这个时候每次应该移走8位了~当然你还可以取其它的数,关于位运算的一些公式:

(1)、取末尾k位:x&(2^k-1);
(2)、在最后一位加一个1:x<<1+1;
(3)、把最后一位变为1:x|1;把最后一位变为0:x|1-1;
(4)、把右数第k位变为1:x|(1<<(k-1)),变为0:x&~(1<<(k-1));
(5)、把末尾k位变为1:x|(1<

 #include<iostream>using namespace std;int a[256]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,            4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,            4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,            4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,            4,5,5,6,5,6,6,7,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,            4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,            4,5,5,6,4,5,5,6,5,6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,            4,5,4,5,5,6,4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,            4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8           };int Count(int n)//计算n的二进制值中1的个数 { int sum=0;    for(;n>0;n>>=8)        sum+=a[n&255];    return sum; } /**********************************************int Count(int n){ unsigned char *ptr=(unsigned char *)&n;     return a[ptr[0]+a[ptr[1]+a[ptr[2]+a[ptr[3]; }**********************************************/ int main(){ int a,b;    cin>>a>>b;    int sum=0;    for(int i=a;i<=b;i++)        sum+=Count(i);    cout<<sum<<endl;     return 0;} 

使用这个方法的话真的是险过,但是你可以把数组做适当的调整,调大点,增加空间来降低时间,恩~~这个数组要能够写成2的幂次方的形式~

方法3:并行计算,利用二分法的思想求解~~难得想到,呵呵~我是没有想到,先贴出代码:暂时不懂,只先贴下代码,

 #include<iostream>using namespace std;int Count(int x){ x=(x&0x55555555)+((x>>1)&0x55555555);     x=(x&0x33333333)+((x>>2)&0x33333333);     x=(x&0x0F0F0F0F)+((x>>4)&0x0F0F0F0F);     x=(x&0x00FF00FF)+((x>>8)&0x00FF00FF);     x=(x&0x0000FFFF)+((x>>16)&0x0000FFFF);     return x;} int main(){ int a,b,sum=0;    cin>>a>>b;     for(int i=a;i<=b;i++)        sum+=Count(i);    cout<<sum<<endl;     return 0;} 

方法4:来源于这道题目后面的讨论,这种做法就更牛了,还是看不懂,贴下~~直接刷到0秒!!!NYOJ 281(整数中的1-2)可以用这个方法过了

#include<iostream>using namespace std;int Count(int n,int m){ if((n>>m)==0) return 0;    int d=(n>>m)&1;    if(d==1) return (n>>(m+1))*(1<<m)+(n&((1<< m)-1))+1+Count(n,m+1);    else return (n>>(m+1))*(1<<m)+Count(n,m+1);}int main(){ int a,b;    cin>>a>>b;    int n=(a==0?1:a);    cout<<Count(b,0)-Count(n-1,0)<<endl;     return 0;} 

例题2:NYOJ 528(找球号),充分利用^的作用:x^x=0,0^x=x。具体见代码:

#include<iostream>#include<cstdio> using namespace std;int num,value;int main(){ while(scanf("%d",&num)!=EOF)    { int sum=0;        for(int i=0;i<num;i++)        { scanf("%d",&value);            sum^=value;        }         printf("%d\n",sum);    }    return 0; } 

读题

通过位运算求1出现的次数

my answer

#include<stdio.h>int main() {    int n, x, sum;    scanf("%d", &n);    while (n--) {        scanf("%d", &x);        int sum = 0;        while (x) {            sum++;            x = x & (x-1);        }        printf("%d\n", sum);    }    return 0;}

standard answer

#include<stdio.h>int bitcount(int x) {        int count = 0;        while (x != 0) {                x &= (x-1);                count++;        }        return count;}int main() {        int num;        int x;        scanf("%d", &num);        while (num--) {                scanf("%d", &x);                printf("%d\n", bitcount(x));        }        return 0;}

反馈

仍然不懂原理.!!!

singledog

假如没有情侣存在,那么单身狗一定是一个非常恐怖的生物。他们可以去电影院最多一人占据3个座位(大概是因为他们不愿意和别人坐在相邻的位置)难以想象他们为什么会有这么强大的战斗力。

那么问题来了。
(以上都是废话)

假如一只单身狗的ID十进制下表示为N,在D进制下,他的战斗力为数字x出现的次数,而他的最大战斗力,则是所有进制下的战斗力中最大的那个。

n不大于10000, x为0到9中的一个数。D大于等于2.

给出n和x,问这只单身狗的战斗力。

样例输入:

3 1

样例输出:

2

读题

my answer

#include<stdio.h>int main() {    int n, num, sign, result = 0, sum, i;    scanf("%d%d", &num, &sign);    for (i = (sign == 0 ? 2 : sign + 1); i < num; i++) {        n = num;        sum = 0;        while (n != 0) {            if (n % i == sign) sum++;            n /= i;        }        result = result < sum ? sum : result;    }    printf("%d\n", result);    return 0;}

standard answer

#include<stdio.h>2.int MAX(int a, int b) {3.    return a > b ? a : b;4.}5.int sol(int n, int d, int x) {6.    int tmp, sum = 0;7.    while (n > 0) {8.        tmp = n % d;9.        if (tmp == x) sum++;10.        n /= d;11.    }12.    return sum;13.}14.int main() {15.    int n, x, i;16.    scanf("%d%d", &n, &x);17.    int ans = 0;18.    for (i = 2; i <= n+1; i++) {19.        ans = MAX(sol(n, i, x), ans);20.    }21.    printf("%d\n", ans);22.    return 0;23.}

反馈

又一个前十哈哈哈
有一个易错点:
找大于等于一的数,从该数加一的进制开始寻找就可以了.
但是如果是0,则要从2开始.
这个bug我找了很久.最后测试了一下发现没有输出,才知道错误原因

School idol project

Bad news! It is said that School of Software is going to be repealed because of low amount of new student. Many people says that TAs always give assignments which are too difficult to finish. Most high school graduates decide not to choose SS after hearing that.

To attract new students and save our school, some SSers decide to start a school idol project. They plan to set up an idol group which contains 9 members ( it seems that 9 is a popular size of school idol group ).

Now there are N students who wants to join the project, numbered from 0 to N-1. Please write a program to output all the possible combinations of the 9 school idols.

The input contains only one number N.

You should output at most 1000 lines, each line is one of the combinations. You should output them in alphabet order. In each line, numbers should be separated by space. There shouldn’t be any spaces at the end of a line. And there should be an empty line at the end of your output.

If C(N, 9) > 1000, output the first 1000 combinations.

Sample Input

10

Sample Output

0 1 2 3 4 5 6 7 8

0 1 2 3 4 5 6 7 9

0 1 2 3 4 5 6 8 9

0 1 2 3 4 5 7 8 9

0 1 2 3 4 6 7 8 9

0 1 2 3 5 6 7 8 9

0 1 2 4 5 6 7 8 9

0 1 3 4 5 6 7 8 9

0 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

For all test cases, 9 <= N <= 10000

读题

my answer

#include<stdio.h>int main() {    int n, a1, a2, a3, a4, a5, a6, a7, a8, a9, count = 0;    scanf("%d", &n);    for (a1 = 0; a1 <= n - 9; a1++)    for (a2 = a1 + 1; a2 <= n - 8; a2++)    for (a3 = a2 + 1; a3 <= n - 7; a3++)    for (a4 = a3 + 1; a4 <= n - 6; a4++)    for (a5 = a4 + 1; a5 <= n - 5; a5++)    for (a6 = a5 + 1; a6 <= n - 4; a6++)    for (a7 = a6 + 1; a7 <= n - 3; a7++)    for (a8 = a7 + 1; a8 <= n - 2; a8++)    for (a9 = a8 + 1; a9 <= n - 1; a9++) {        printf("%d %d %d %d %d %d %d %d %d\n",        a1, a2, a3, a4, a5, a6, a7, a8, a9);        count++;        if (count == 1000) return 0;    }    return 0;}

standard answer

#include <stdio.h>2. 3.int counter = 0;4. 5.void dfs(const int index, int *s, const int n) {6.    if (index == 9) {7.        int i;8.        for (i = 0; i < 8; ++i)9.            printf("%d ", s[i]);10.        printf("%d\n", s[8]);11.        ++counter;12.    } else {13.        for (s[index] = s[index-1]+1; s[index] < n-8+index && counter < 1000;14.                ++s[index])15.            dfs(index+1, s, n);16.    }17.}18. 19.int main() {20.    int s[9], n;21.    scanf("%d", &n);22.    for (s[0] = 0; s[0] < n-8; ++s[0])23.        dfs(1, s, n);24.    return 0;25.}

反馈

应该用DFS

structInMemory(for hw)

定义一个结构体类型T包含三个变量分别是double,char,和int类型。实例化一个T,输出此实例在内存中所占空间大小(使用sizeof),输出3个变量分别所占内存大小之和以及3个变量在内存中距离此实例的首地址的长度(以byte为单位)。

注意3个变量必须以double,char,int的顺序排列,比如:

struct T {

double x;char y;int z;

}

根据所输出内容理解struct在内存中的组织形式。

输出格式:实例所占内存大小以及3个变量所占内存大小之和在一行,以空格间隔;3个变量在内存中距离实例的首地址长度为一行,以空格间隔。

Hint:
struct在内存中存在自动对齐的功能,所以有些时候并不是其中的每个变量都是紧密排列的,会有一些内存空隙。

读题

my answer

#include<stdio.h>int main() {    struct T {        double x;        char y;        int z;    } eg;    char * p1 = (char *)&eg.x;    char * p2 = (char *)&eg.y;    char * p3 = (char *)&eg.z;    printf("%d %d\n", sizeof(eg), sizeof(eg.x) + sizeof(eg.y) + sizeof(eg.z));    printf("%d %d %d\n", p1 - p1, p2 - p1, p3 - p1);     return 0;}

standard answer

#include<stdio.h>2.typedef struct T {3.    double x;4.    char y;5.    int z;6.} T;7.int main() {8.    T one;9.    char* xpt = (char*)(&one.x);10.    char* ypt = (char*)(&one.y);11.    char* zpt = (char*)(&one.z);12.    char* onept = (char*)(&one);13.    int leng = sizeof(one.x) + sizeof(one.y) + sizeof(one.z);14.    printf("%d %d\n", sizeof(one), leng);15.    printf("%d %d %d\n", xpt - onept, ypt - onept, zpt - onept);16.    return 0;17.}

反馈

不同类型的指针不能进行加减运算,必须通过强制类型转换.

初识链表(for hw)

Jeremy最近学习了指针和链表的概念,而链表可以使用结构体实现。他知道链表中有一种叫做循环链表例如1->2->3->4->5->1(其中第一个1和最后一个1是同一个)。

期中考试后,老师让Jeremy做一个学生的成绩统计,Jeremy想使用结构体实现简单的循环链表来完成。他计划读入同学的学号和成绩等级,例如学号1,等级b,学号2,等级a……

这个程序需要实现下面的功能:

例如有4个同学,他们的学号等级分别为:“12 a”,”3 b”, “57 c”, “1 d”;他们的关联关系为”12 a”->”3 b”->”57 c”->”1 d”->”12 a”。

程序需要实现的功能是当输入一个学号时,输出上一个同学的等级,例如输入3,应该输出a,输入12应该输出d。

当输入的学号在这些同学中出现多次时,输出学号第一次出现时上一个同学的等级,例如:“12 a”,”12 b”, “57 c”, “1 d”,应输出d

若查找不到该学号,则不输出

有其他坑爹地方请相互在评论区提示,并邮件TA,谢谢。

Input

第一行为两个整数num和m,num代表学生的总数,m代表输入的学号。

下面num行,每一行包含一个整数代表学号和一个字符代表成绩等级

Output

上一个同学的成绩等级(换行)

Input
3 2

1 a

2 b

3 c

Output

a

Hint:
链表模板如下:

typedef struct node *link;

typedef struct node {

    int student_num;    link next;

}node;

尽管可以用数组完成,但希望同学们可以掌握一下如何写链表和结构体,今后笔试面试都有很大机会用得上。

读题

my answer

#include<stdio.h>#include<stdlib.h>int main() {    int num = 0, m = 0;    struct student {        int num;        char score;        struct student * next;    };    scanf("%d %d", &num, &m);    struct student * p;    p = (struct student *)malloc((num + 1) * sizeof(struct student));    struct student * head;    head = p;    int i;    for (i = 0; i < num; i++, p++) {        scanf("%d %c", &(p -> num), &(p -> score));        p -> next = p - 1;    }    *p = *head;    if (m == head -> num) {        printf("%c\n", (head + num - 1) -> score);    } else {        for (i = 0, p = head + 1; i <= num + 1; i++, p++) {            if (m == p -> num) {                printf("%c\n", (p - 1) -> score);                break;            }        }    }    return 0;}

standard answer

#include<stdio.h>2.typedef struct node *link;3.typedef struct node {4.        int student_num;5.        char grade;6.        link next;7.}node;8. 9.int main(void) {10.        int num, n;11.        scanf("%d %d", &num, &n);12.        node list[100];13.        int i;14.        for (i = 0; i < num; i++) {15.                scanf("%d %c", &list[i].student_num, &list[i].grade);16.                if (i == 0)17.                        list[i].next = NULL;18.                else19.                        list[i].next = &list[i-1];20.        }21.        list[0].next = &list[num-1];22.        for (i = 0; i < num; i++) {23.                if (n == list[i].student_num) {24.                        printf("%c\n", list[i].next->grade);25.                        break;26.                }27.        }28.        return 0;29.}

反馈

这里有个坑.
因为第一个学号是这个学号第一次出现,所以如果m是第一个学号,则要输出倒数第二个同学的成绩….- - 无奈.

hard A+B (自己添加的选做题

Constraints

Time Limit: 1 secs, Memory Limit: 8 MB

Description

Input A and B, output A+B

Input

Input two values, A and B.(0<=A,B<10^100)

Output

Output the result of A+B.

Sample Input

1234567890123456789

987654321

Sample Output

1234567891111111110

Hint:
A and B are large numbers,please use high precision computation.

读题

my answer

#include<stdio.h>#include<string.h>int main() {    int i, j, k;    char a[102];    char b[102];    int sum[105] = {0};    scanf("%s%s", a, b);    int lena = strlen(a);    int lenb = strlen(b);    for (i = lena - 1, j = lenb - 1, k = 0; i >= 0 || j >= 0; i--, j--, k++) {        if (i >= 0 && j >= 0) {            sum[k] = a[i] + b[j] - '0' - '0';        } else if (i >= 0 && j < 0) {            sum[k] = a[i] - '0';        } else if (i < 0 && j >= 0) {            sum[k] = b[j] - '0';        }    }    int len = lena > lenb? lena : lenb;    for (i = 0; i < len; i++) {        sum[i + 1] += sum[i] / 10;        sum[i] %= 10;    }    if (sum[i] != 0)    printf("%d", sum[i]);    for (i--; i >= 0; i--)    printf("%d", sum[i]);    printf("\n");    return  0;}

standard answer

#include <stdio.h>2.#include <string.h>3.void init(int *arr, int *len) {4.  int i;5.  char st[101];6.  scanf("%s", st);7.  *len = strlen(st);8.  for (i = 0; i < *len; i++)9.  arr[i] = st[*len - 1 - i] - '0';10.}11.int main() {12.  int a[100], b[100], c[101], m, n, i, j, k, tmp;13.  init(a, &n);14.  init(b, &m);15.  k = m > n ? m : n;16.  for (i = 0; i <= k; i++) c[i] = 0;17.  for (i = n; i < k; i++) a[i] = 0;18.  for (i = m; i < k; i++) b[i] = 0;19.  for (i = 0; i < k; i++) {20.    tmp = a[i] + b[i] + c[i];21.    c[i] = tmp % 10;22.    c[i+1] = tmp / 10;23.  }24.  if (c[k] > 0) k++;25.  for (i = k; i > 0; i--) printf("%d", c[i - 1]);26.  puts("");27.  return 0;28.}

反馈



  1. 来自网络 ↩
0 0
原创粉丝点击