厦大C语言上机 1388 高精度乘法

1388.高精度乘法


时间限制: 1000 MS          内存限制: 65536 K
        
提交数: 564 (0 users)          通过数: 237 (226 users)


问题描述
之前做过了一道关于高精度加法的,想必难不到大家,那么请大家现在尝试一下高精度乘法吧!


输入格式
输入只包含一行,包括两个整数A和B,A、B用空格隔开。(0<=A,B<=10^1000)


输出格式
输出一行,A和B的乘积。


样例输入
111111111111 111111111111


样例输出
12345679012320987654321


来源

xmu

#include <stdio.h>
#include <string.h>

void reverse(char *s, int len)
{
    int i, j;
    char temp;

    for (i = 0, j = len-1; i < j; ++i, --j)
    {
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }
}

void multiply(char *a, int len_a, char *b, int len_b, char *product)
{
    int ptr_a, ptr_b, ptr_int_prod, ptr_prod, len_prod, carry, temp;
    int int_prod[2005] = { 0 };

    reverse(a, len_a);
    reverse(b, len_b);

    ptr_int_prod = 0;
    for (ptr_b = 0; ptr_b < len_b; ++ptr_b)
    {
        ptr_int_prod = ptr_b;
        for (ptr_a = 0; ptr_a < len_a; ++ptr_a, ++ptr_int_prod)
            int_prod[ptr_int_prod] += (a[ptr_a] - '0') * (b[ptr_b] - '0');
    }
    len_prod = ptr_int_prod;
    carry = 0;
    for (ptr_int_prod = 0; ptr_int_prod < len_prod; ++ptr_int_prod)
    {
        temp = (int_prod[ptr_int_prod] + carry) / 10;
        int_prod[ptr_int_prod] = (int_prod[ptr_int_prod] + carry) % 10;
        carry = temp;
    }
    while (carry > 0)
    {
        temp = carry / 10;
        int_prod[ptr_int_prod] = carry % 10;
        carry = temp;
        ptr_int_prod++;
        len_prod++;
    }

    ptr_int_prod = len_prod - 1;
    while (ptr_int_prod >= 0 && int_prod[ptr_int_prod] == 0)
    {
        ptr_int_prod--;
        len_prod--;
    }
    if (ptr_int_prod < 0)
    {
        product[0] = '0';
        product[1] = '\0';
        return;
    }
    ptr_prod = 0;
    while (ptr_int_prod >= 0)
        product[ptr_prod++] = (char)(int_prod[ptr_int_prod--] + '0');
    product[ptr_prod] = '\0';
}

int main()
{
    char a[1005] = { 0 };
    char b[1005] = { 0 };
    char product[2005] = { 0 };
    int len_a, len_b;

    scanf("%s %s", a, b);
    len_a = (int)strlen(a);
    len_b = (int)strlen(b);
    multiply(a, len_a, b, len_b, product);
    printf("%s\n", product);

    return 0;
}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25. 输入说明 The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9. 输出说明 The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer. 输入样例 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 输出样例 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 小提示 If you don't know how to determine wheather encounted the end of input: s is a string and n is an integer C++ while(cin>>s>>n) { ... } c while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want /*while(scanf(%s%d",s,&n)!=EOF) //this also work */ { ... } 来源 East Central North America 1988 北大OJ平台(代理
厦大c语言上机1370题是厦门大学计算机系开设的一门课程考试题目。这道题目主要考察学生对于C语言的掌握程度以及编写程序的能力。根据我对于这道题目的理解,我将尽力回答这个题目。 首先,题目要求我们编写一个C程序,实现对输入的多个正整数进行排序,并按照升序输出。在解决这个问题时,我们可以使用一种常见的排序算法,例如冒泡排序、选择排序或者插入排序。 具体实现上机1370的方法如下: 1. 首先,我们需要利用scanf函数从用户输入中读取多个正整数,并将其存储在一个数组中。 2. 接下来,我们可以选择适合的排序算法对这个数组进行排序。这里我们以冒泡排序来进行举例,它的实现思路是从数组的第一个元素开始,依次比较相邻的两个元素,如果顺序不对则交换位置,这样通过多次迭代,最终达到排序的效果。 3. 最后,我们使用printf函数按照升序输出排序后的数组元素。 例如,对于输入的正整数序列[5, 2, 4, 3, 1],经过冒泡排序后,输出的结果为[1, 2, 3, 4, 5]。 在完成程序编写后,我们可以通过编译器进行编译,然后使用输入数据进行测试。如果程序能够正确输出按升序排列的正整数序列,则说明我们顺利完成了上机1370的任务。 总之,厦大c语言上机1370题目要求我们编写一个C程序,能够实现对多个输入正整数的排序,并按照升序输出结果。正确完成这个题目可以展示我们对C语言的掌握以及程序设计的能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值