在C中大数量减去 [英] Big Number Subtraction in C

查看:149
本文介绍了在C中大数量减去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成我的介绍C语言课程考试约20分钟前。在考试的第一个问题抓住我有点猝不及防,并参与寻找差异两个大的数字。

I just finished my exam in an introductory C course about 20 minutes ago. The first question on the exam caught me somewhat off guard, and involved finding the difference two large numbers.

目标是采取两个结构(N1和N2)由值,并存储在参考(N3)通过的结构的差异。我们被允许承担N3与全0开始。最大尺寸可以是任何东西,因此该解决方案仍然要工作,如果数字是100位。

The goal was to take two structures (N1 and N2) by value, and store the difference in a structure passed by reference (N3). We were allowed to assume N3 was initiated with all '0's. The MAX size can be anything, so the solution still has to work if the numbers are over 100 digits.

下面的底座code(原可能略有不同,这是从内存)

Here's the base code (original may be slightly different, this is from memory)

#include <stdio.h>
#include <stdlib.h>
/* MAX can be any length, 10, 50, 100, etc */
#define MAX 10

struct bignum
{
    char digit[MAX];
    char decimaldigit[MAX/2];
};
typedef struct bignum bigNum;
void difference(bigNum, bigNum, bigNum *);

/*
    Original values in N1 and N2

    N1.digit = { '0', '0', '0', '5', '4', '8', '2', '0', '9', '0'};
    N1.decimaldigit { '0', '0', '0', '4', '9' };

    N2.digit = { '0', '0', '0', '4', '8', '1', '3', '1', '4', '5'};
    N2.decimaldigit { '8', '0', '1', '2', '0' };
*/

/*
    Result would be:
    N3.digit = { '0', '0', '0', '0', '6', '6', '8', '9', '4', '4'}
    N3.decimaldigit { '1', '9', '9', '2', '9' }
*/

这个问题是不是在寻找解决这个问题这么多,但只有约〜都提供完整的答案20行。我的方法用于解决涉及减去转换为整数,再进行适当的后数字一次一个携带如果结果为阴性。这一时间比提供了什么实质上更多的空间。

The problem isn't so much in finding a solution to this problem, but that only about ~20 lines were provided for the complete answer. My method for solving involved subtracting the digits one at a time after converting to integers, and then making appropriate carries if the result was negative. This took substantially more space than what was provided.

,我是铅相信,还有的认为,我没有看到一个相当琐碎的解决方案。它是什么?现在我已经完成了课程,但这个问题仍然困扰着我!

Based on the small amount of marks and the space provided for this question, I'm lead to believe that there's a fairly trivial solution that I'm not seeing. What is it? I have now finished the course but this question is still bothering me!

一个完整的解决方案不是必需的,该功能只是内部工作的区别

A complete solution isn't required, just the inner workings of the function difference.

没有位运算符的使用,以防万一。

No bitwise operators are to be used, just in case.

推荐答案

这应该工作,如果 N1&GT; = N 。这还假定阵列布局像 DN ... d2d1d0.e0e1 ... EM

This should work if N1 >= N2. This also assumes that the arrays are laid out like dn...d2d1d0.e0e1...em.

char digchr(int); // Converts a single-digit integer into a character.

void difference(bigNum N1, bigNum N2, bigNum *N3) {
    int carry = 0;

    for (int i = MAX / 2 - 1; i >= 0; i--) {
        int diff = N1.decimalDigits[i] - N2.decimalDigits[i] - carry;
        if (diff < 0) { 
            diff += 10;
            carry = 1;
        } else {
            carry = 0;
        }

        N3->decimalDigits[i] = digchr(diff);
    }

    for (int i = 0; i < MAX; i++) {
        int diff = N1.digits[i] - N2.digits[i] - carry;
        if (diff < 0) {
           diff += 10;
           carry = 1;
        } else {
            carry = 0;
        }

        N3->digits[i] = digchr(diff);
    }
}

这篇关于在C中大数量减去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆