在C#非常大数字的数字之和 [英] In C# Sum Of Digits For Very Big Number

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

问题描述

C#如何在文本框4534534534543533453432432434324324324234中为非常大的数字进行数字求和,结果在其他文本框中显示为140/14 / 5.如果数字很小,则为ex。 45345345343结果文本框显示为43 / 7.更小的ex.45显示为9.请任何身体帮助我的项目紧急。

这不适合我的学校/大学项目...对不起英语不好...

到目前为止完成的代码...



C# How to do Sum of Digits for very big number in a textbox 4534534534543533453432432434324324324234 and result display in other text box as 140 / 14 / 5. if number is small ex. 45345345343 result text box display as 43 / 7. Even smaller ex.45 display as 9. Please any body help it is urgent for my project.
It is not for my school/college project ... sorry for bad English...
Code done so far...

 public long DigitSum(String BigNumberStr,int len)
    {
        int total = 0;       
        int s=BigNumberStr.Length;

        if (len == 10)
        {
            for (int i = 0; i < s; i++)
                total += int.Parse(Convert.ToString(BigNumberStr[i]));
        }
        else if (len == 100)
        {
            for (int i = 0; i < s; i++)
                total += int.Parse(Convert.ToString(BigNumberStr[i]));
        }
            
                return total;
 }
fun.DigitSum(TextBox2.Text + TextBox10.Text, 100);
fun.DigitSum(TextBox2.Text + TextBox10.Text, 1000);







但它不起作用请帮助某人......结果显示如下格式..

2132 = 8

213245 = 17/8

21324545454454545365756756 = 116/17/8




But it's not working please help somebody... Result display in following format..
2132 = 8
213245 = 17 / 8
21324545454454545365756756 = 116 / 17 / 8

推荐答案

嗯,这看起来很简单,一个简单的循环字符串字符可以解决问题。只需重现用铅笔和纸张执行相同任务的算法。
Well, it looks a pretty trivial task, a simple loop over the string characters would do the trick. Just reproduce the algorithm you would use to perform the same task with pencil and paper.


你在考虑错误。这不是一个非常大的数字。它是一串恰好是数字的字符。



因此,枚举字符串中的字符并获取每个字符的整数值并将它们相加。 br $> b $ b

不,我不是为你编写代码,因为这是一个明显的家庭作业。你已经获得了在课堂上写这篇文章所需的一切。
You're thinking about wrong. It's not a "very big number". It's a string of characters that just happen to be digits.

So, enumerate the characters in the string and get the integer value of each character and add them up.

No, I'm not writing the code for you because this is an obvious homework assignment. You've already been given everything you need to write this in your class.


我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



提示:从 BigInteger结构 [ ^ ]
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

Hint: Start with the BigInteger structure[^]


这篇关于在C#非常大数字的数字之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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