加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数运算——加法

发布时间:2020-12-14 02:46:24 所属栏目:大数据 来源:网络整理
导读:作为一个对编程没有很深研究的初学者,对于这些虽然没有任何算法的题仍然觉得很难很难,但是或许都是需要一个过渡时期吧,这是这几天的结果,很多有自己的考虑,自己的想法在里面,但是也百度查了很多,也看了很多别人写的关于这方面的。 先看一下关于大数方

作为一个对编程没有很深研究的初学者,对于这些虽然没有任何算法的题仍然觉得很难很难,但是或许都是需要一个过渡时期吧,这是这几天的结果,很多有自己的考虑,自己的想法在里面,但是也百度查了很多,也看了很多别人写的关于这方面的。
先看一下关于大数方面的知识:

bool型为布尔型,占1个字节,取值0或1。

     BOOL型为int型,一般认为占4个字节,取值TRUE/FALSE/ERROR。

     sbyte型为有符号8位整数,占1个字节,取值范围在128~127之间。

     bytet型为无符号16位整数,占2个字节,取值范围在0~255之间。

     short型为有符号16位整数,占2个字节,取值范围在-32,768~32,767之间。

     ushort型为无符号16位整数,占2个字节,取值范围在0~65,535之间。

     int型为有符号32位整数,占4个字节,取值范围在-2,147,483,648~2,647之间。

     uint型为无符号32位整数,占4个字节,取值范围在0~4,294,967,295之间。

     long型为64位有符号整数,占8个字节,取值范围在9,223,372,036,854,775,808~9,807之间。

     ulong型为64位无符号整数,占8个字节,取值范围在0~18,446,744,073,709,551,615之间。

     float型为32位单精度实数,占4个字节,取值范围3.4E+10的负38次方~3.4E+10的38次方之间。

     double型为64位实数,占8个字节,取值范围1.7E+10的负308次方~1.7E+10的正308次方。

这几天一直在做关于大数运算的题,就两道题我也是醉了,第一道是字符串型的,用了好久好久,提交了好多次,最后知道是哪错了吗,原来是flag这个变量没给初值,等具体的原因会在下面细说,第二道是多次大数相加,因为是我是大一新生么,对于编程很多复杂的东西还是不熟悉的,好比函数,我直到现在对它还是很模糊,现在对它真的是又爱又恨啊,因为我驾驭不了它,但是我会好好努力的。

其实关于大数加法思路其实比较简单的,但是有几点要注意的:1.要先给把存大数的数组要先归零;2.要把大数倒着存放到一个数组里面(这要避免了因为大数的长度不同而要考虑的所有问题,记得刚开始做的时候就不是这种思维);3.倒着输出即可;4.有时候要考虑0的问题。

所以让我们来看两个例题吧

1.Martian Addition

In the 22nd Century,scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year,they would hold an Arithmetic Contest on Mars (ACM). The task of the contest is to calculate the sum of two 100-digit numbers,and the winner is the one who uses least time. This year they also invite people on Earth to join the contest.
As the only delegate of Earth,you’re sent to Mars to demonstrate the power of mankind. Fortunately you have taken your laptop computer with you which can help you do the job quickly. Now the remaining problem is only to write a short program to calculate the sum of 2 given numbers. However,before you begin to program,you remember that the Martians use a 20-based number system as they usually have 20 fingers.

Input:
You’re given several pairs of Martian numbers,each number on a line.
Martian number consists of digits from 0 to 9,and lower case letters from a to j (lower case letters starting from a to present 10,11,…,19).
The length of the given number is never greater than 100.

Output:
For each pair of numbers,write the sum of the 2 numbers in a single line.

Sample Input:

1234567890
abcdefghij
99999jjjjj
9999900001

Sample Output:

bdfi02467j
iiiij00000

#include<stdio.h>
#include<string.h>
int main()
{
    int q[150],p[150],sum[150],i,j,len1,len2,flag,count;
    char a[150],b[150];
    while(scanf("%s %s",a,b)==2)
    {
        count=0;
        len1=strlen(a);
        len2=strlen(b);
        flag=101;//一定要初始化,否则当sum[100]!=0时候就不会执行flag=i这一步了,就会对后面的for循环产生影响
        memset(q,0,sizeof(q));
        memset(p,sizeof(p));
        memset(sum,sizeof(sum));//把要存数的数组归零处理
        for(i=len1-1,j=0;i>=0;i--,j++)//倒着存数
        {
            if(a[i]>='0'&&a[i]<='9')
            {
                p[j]=a[i]-'0';
            }
            else if(a[i]>='a'&&a[i]<='j')
            {
                p[j]=a[i]-'a'+10;
            }
        }
        for(i=len2-1,j++)
        {
            if(b[i]>='0'&&b[i]<='9')
            {
                q[j]=b[i]-'0';
            }
            else if(b[i]>='a'&&b[i]<='j')
            {
                q[j]=b[i]-'a'+10;
            }
        }
        for(i=0;i<101;i++)
        {
            sum[i]+=p[i]+q[i];
            if(sum[i]>19)
            {
                sum[i]-=20;
                sum[i+1]++;
            }
        }
        for(i=100;i>=0;i--)
        {
            if(sum[i]!=0)
            {
                break;
            }
            else
            {
                count++;
            }
            flag=i;
        }
        if(count==101)
        {
            printf("0");//0特殊处理(如果有好方法的话求告诉)
        }
        for(i=flag-1;i>=0;i--)
        {
            if(sum[i]>=0&&sum[i]<=9)
            {
                printf("%d",sum[i]);
            }
            else
            {
                if(sum[i]==10)
                {
                    printf("a");
                }
                else if(sum[i]==11)
                {
                    printf("b");
                }
                else if(sum[i]==12)
                {
                    printf("c");
                }
                else if(sum[i]==13)
                {
                    printf("d");
                }
                else if(sum[i]==14)
                {
                    printf("e");
                }
                else if(sum[i]==15)
                {
                    printf("f");
                }
                else if(sum[i]==16)
                {
                    printf("g");
                }
                else if(sum[i]==17)
                {
                    printf("h");
                }
                else if(sum[i]==18)
                {
                    printf("i");
                }
                else if(sum[i]==19)
                {
                    printf("j");
                }
            }
        }
        printf("n");
    }
    return 0;
}

就这个题,真的做了好久好久,当过的那一刹那,真的好激动,好兴奋,好高兴,虽然,自己现在是很差,但是努力ing。

2.Integer Inquiry

One of the first users of BIT’s new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.

This supercomputer is great,'' remarked Chip.I only wish Timothy were here to see these results.” (Chip moved to a new apartment,once one became available on the third floor of the Lemon Sky apartments on Third Street.)

Input

The input will consist of at most 100 lines of text,each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length,and will only contain digits (no VeryLongInteger will be negative).

The final input line will contain a single zero on a line by itself.

Output

Your program should output the sum of the VeryLongIntegers given in the input.

This problem contains multiple test cases!

The first line of a multiple input is an integer N,then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input
1

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output
370370367037037036703703703670

#include<stdio.h>
#include<string.h>
int sum[150];
char s[150];
void add(char a[])
{ 
    int len,j;
    len=strlen(a);
    for(i=len-1,j++)
    {
        sum[j]+=(a[i]-'0');
        sum[j+1]+=sum[j]/10;
        sum[j]%=10;
    }
}
int main()
{
    int n,count;
    while(~scanf("%d",&n))
    {
        while(n--)
        {
            memset(sum,sizeof(sum));
            while(scanf("%s",s)==1)
            {
                add(s);
                if(strcmp(s,"0")==0)
                {
                    break;
                }
            }
            flag=0;
            for(i=100;i>=0;i--)
            {
                if(flag==0&&sum[i]==0)
                {
                    continue;
                }
                else
                {
                    flag=1;
                    printf("%d",sum[i]);
                }
            }
            if(n!=0)
            {
                printf("n");
            }
            printf("n");
        }
    }
    return 0;
}

这个是利用函数做的,以前,还没什么感觉,觉得函数没什么很大的用处,现在才小有体会,觉得函数真的很好,所以呢,函数一定要学好,它真的很有用。

体会:

这几天做完了这两道关于大数的题,我真的受益颇丰,我会因为不知道思路而苦恼,会因为不知道哪错了而采取很多方法,我会因为这道题 过了而哈哈哈大笑,真的,那时候才知道为完成一件事而去奋斗的滋味,也尝到了自己努力后的甘果,我知道对于编程方面任重道远,而且曾经有人答应过我会尽全力帮我,但是,他食言了,不知道有什么难言之隐,反正变化好快,但是,随他去吧,顺其自然吧。在编程方面我需要学的还有很多很多,我会继续努力的,加油。———他人成十,我便做百;他人成百,我便做千。加油!!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读