zoukankan      html  css  js  c++  java
  • 201. 相加

    题目描述

    华老师让你们计算一个等式,s=a+aa+aaa+aaaa+aa…a(总共有n个这样的数相加)的值,其中a是一个1~9的数字。例如当a = 2,n = 5时,s = 2+22+222+2222+22222。

    解答要求时间限制:1000ms, 内存限制:100MB
    输入

    输入一行包含两个整数a(1<=a<=9)和n(1<=n<=9),其中n为几个这样的数字相加(如描述中的例子n是5)。

    输出

    输出一行结果s。

    样例

    输入样例 1 复制

    2 5

    输出样例 1

    24690
    提示样例 1
     


    提示

    循环n的大小,每次在之前得到sum上乘10再加上a

    思路:循环得到每个数相加
    代码:
    // we have defined the necessary header files here for this problem.
    // If additional header files are needed in your program, please import here.
    
    int main()
    {  
      // please define the C++ input here. For example: int a,b; cin>>a>>b;;  
      // please finish the function body here.  
      // please define the C++ output here. For example:cout<<____<<endl; 
       int M,N;
       cin>>M>>N;
        int sum = 0;
         int t = 1;
        int res = M;
        while(N--)
        {
            sum = sum+res;
            t = t*10;
            res = M*t+res;
            //cout<<res<<endl;
           
        }
        cout<<sum<<endl;
      return 0;
    }
     
     
    以大多数人努力程度之低,根本轮不到去拼天赋~
  • 相关阅读:
    Dos命令大全(收藏)
    asp.net读写Cookies
    asp.net文件下载
    使用存储过程分页
    (十)死锁检测算法
    poj1664
    一席话惊醒梦中人
    深入了解scanf()/getchar()和gets()/cin等函数
    小结《malloc与new之区别》
    (六)文件管理
  • 原文地址:https://www.cnblogs.com/gcter/p/15469943.html
Copyright © 2011-2022 走看看