计算特定序列的总和? [英] Compute Sum of speacial Sequence ?

查看:71
本文介绍了计算特定序列的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我是新手...我有一个如下问题:

计算连续序列的总和(十进制)
示例:

n = 12 => 1 + 2 + 3 + 4 +5 +6 +7 +8 +9 +(1 + 0)+(1 + 1)+(1 + 2)= 51
n = 123吗?
n = 13579吗?
n = 123123123123?
n = 98765432100123456789?

欢迎查看您认为最佳的所有解决方案!

希望看到任何帮助!


-------------------------------------------------- ---
如果您认为这是我的家庭作业,请不要让我失败:D

Hi all !
I''m a newbie ... i have a question as follows:

Compute sum (in decimal) of consecutive sequence
Example:

n=12 => 1 + 2 + 3 + 4 +5 +6 +7 +8 +9 + (1+0) + (1+1) + (1+2)=51
n= 123 ?
n= 13579 ?
n= 123123123123?
n= 98765432100123456789 ?

Welcome to see all solutions that you think is the best !

Hope to see any aid !


-----------------------------------------------------
Don''t make me fail if you think this is my homework :D

推荐答案

周六晚上进行的很好的锻炼,谢谢:

Nice exercise for saturday night, thanks:

#include <stdio.h>
#include <tchar.h>
double Dix(const TCHAR* anum,const unsigned int nnum,const unsigned int ix)
{
  return ix<nnum?(double)(anum[ix]-'0'):0.0;
}
double HornerSchema(const TCHAR* anum,const unsigned int nnum,const unsigned int lo,const unsigned int hi)
{
  double        res = 0.0;
  unsigned int  ix;
  for(ix=hi;lo<ix;ix--) res = res*10.0 + Dix(anum,nnum,nnum-ix);
  for(;0<ix;ix--) res *= 10.0;
  return res;
}
double Upper(const TCHAR* anum,const unsigned int nnum,const unsigned int pos)
{
  return HornerSchema(anum,nnum,pos+1,nnum);
}
double Lower(const TCHAR* anum,const unsigned int nnum,const unsigned int pos)
{
  return HornerSchema(anum,nnum,0,pos);
}
double Pow10(const unsigned int pos)
{
  double        res = 1.0;
  unsigned int  ix;
  for(ix=0;ix<pos;ix++) res*=10.0;
  return res;
}
unsigned int Digit(const TCHAR* anum,const unsigned int nnum,const unsigned int pos)
{
  return pos<nnum?(unsigned int)(anum[nnum-pos-1]-'0'):0;
}
double HSum(const TCHAR* anum,const unsigned int nnum)
{
  double        sum = 0.0;
  unsigned int  i,di;
  double        hi,lo;
  double        hisum,disum,llsum;
  double        ASUM[] = { 0,0,1,3,6,10,15,21,28,36,45 };
  for(i=0;i<nnum;i++)
  {
    hi    = Upper(anum,nnum,i);
    di    = Digit(anum,nnum,i);
    lo    = Lower(anum,nnum,i);
    hisum = hi*4.5;
    disum = Pow10(i)*ASUM[di];
    llsum = di*(lo+1.0);
    sum  += hisum+disum+llsum;
  }
  return sum;
}
int _tmain(int argc, _TCHAR* argv[])
{
  const TCHAR  number[] = __T("98765432100123456789");
  double      sum;
  sum = HSum(number,(sizeof(number)/sizeof(number[0]))-1);
  if(1e15<sum) _tprintf(__T("%s = ~ %.15G"),number,sum); // double is limited to about 15 digits
  else _tprintf(__T("%s = %.0lf"),number,sum);
  _gettch();
  return 0;
}





123 = 1038
13579 = 235360
123123123123 = 6018595171770
9,87654321001235E+19 = 8,87700045735992E+21



问候.



Regards.


这似乎是家庭作业,您应该自己做...

无论如何,您都应该轻松做到这一点:只需将问题分开,然后解决方案就会自动出现.

作为一个新手,您不应该只担心如何实现该解决方案,而不必考虑伪代码解决方案,开始确定问题,然后制作一个流程图来确定所涉及的例程和变量,然后使用伪代码来实现它并最终实现功能,然后是程序本身.

您将不费吹灰之力就能得到结果.

这是给您一个类似问题的最佳建议,除此之外,您还会收到诸如这是家庭作业",我们不会帮您",...
的帖子.
进行所有步骤,如果您遇到困难,请发送一个特定的问题,然后有人会帮助您回答.

祝你好运!
This seems to be homework, you should do it by yourself...

Anyway you should do it easily: simply divide the problems and then the solution will appear automatically.

As a newbie you should only not to worry on how to implement the solution rather than thinking on a pseudocode solution, start identifying the problems, then make a flowchart identifying the involved routines and variables, then use pseudocode to implement it and at the end implement the functions and then the program itself.

You''ll get the result without much effort.

This is the best advice you''ll get here with a question like that, apart of that you''ll get posts like "this is homework", "we won''t help you", ...

MAke all the steps and if you get stuck, send a specific question and somebody will help you answering it.

Good luck!


这篇关于计算特定序列的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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