Skip to content

Instantly share code, notes, and snippets.

@jikeytang
Last active August 29, 2015 14:01
Show Gist options
  • Save jikeytang/d1a71e69592ec582280f to your computer and use it in GitHub Desktop.
Save jikeytang/d1a71e69592ec582280f to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140521-题目1
今天三题由杭州-轨道提供,他的blog:http://blog.csdn.net/chriswenwu/
求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有用户控制。
回复时注意加上下面这句话,才会有语法高亮或格式缩进。
```javascript
@mailzwj
Copy link

mailzwj commented May 21, 2014

<input type="text" id="stdin" placeholder="基数"><!-- 例:2 -->
<input type="text" id="num" placeholder="求和数个数"><!-- 例:5 -->
<input type="button" value="计算总和" id="getSum">
<script>
(function(){
    var stdinInp = document.getElementById("stdin"),
        numImp = document.getElementById("num"),
        sumBtn = document.getElementById("getSum"),
        sum = 0, stdin = 0, num = 0,
        rNum = /^\d+$/;

    function getResult(num, count) {
        var res = 0, old = new Array(count + 1).join("1");
        for (var i = 1; i <= count; i++) {
            res += old.substr(0, i) * num;
        }
        return res;
    }

    sumBtn.onclick = function(e){
        stdin = stdinInp.value;
        num = numImp.value;
        if (!rNum.test(stdin)) {
            alert("请输入正整数");
            return false;
        }
        if (!rNum.test(num)) {
            alert("请输入正整数");
            return false;
        }
        sum = getResult(+stdin, +num);
        console.log(sum);
    };
})();
</script>

@chriswenwu
Copy link

function gets_(n,size){

     var max="",i=0,s=0;
      while(i<size){
              max+=1+"";
            i++;
      } 
        size--;
        s=parseInt(max);
      if(size<1) return n*s;
      else{
      s+=arguments.callee(n,size);}
    return n*s;

}

@chriswenwu
Copy link

看看我的

/*
*   返回一个二维数组 array  
*   s=a+aa+aaa+aaaa+aa...a的值
*   a 是数字
*   n 是指有n个数
*   isShow 为1时可显详细数字 为0是不显
*/ weed 提供
function add_a_aa_aaa($a, $n, $isShow = 0)
{
    $sum = 0;$aaa = 0;$show=array();
    for($i = 0; $i < $n; $i++){
        $aaa = ($aaa * 10) + $a;
        $sum += $aaa;

        if($isShow) $show[] = $aaa;
    }
    return array($sum, $show);;
}

@caigou
Copy link

caigou commented May 21, 2014

//n为数字 l为个数
function clec(n,l){
     var a=n,res=0;
     for(var i=0; i<l;i++){
         res+=Number(n);
         n+=''+a;
     }
    return res;
}

console.log(clec(2,5));

@jikeytang
Copy link
Author

// 天牛答案:

function a(n){
    var arr = new Array(n+1).join(n),i=1,re=0;
    for(;i<=n;i++){
        re += arr.slice(0,i)-0;
    }
    return re;
}
console.log(a(5));

@chriswenwu
Copy link

function getS($n,$a){
    $sum='$sum='.$n;
    for ($i=2; $i <= $a; $i++) { 
        $sum.='+'.str_repeat($n,$i);
    }
    eval($sum.';');
    return $sum;
}

三哥答案

@rambo-panda
Copy link

var add= function(times,num){
    num = (num || 1) + '';
    var _num ='', num_arr =[];
    while(times > 0){
        _num += num;
        num_arr.push(_num);
        times--;
    }
    return new Function('return ' + num_arr.join('+'))();

       //  //  看了上述高手答案   赞一个 caigou 兄台的方法   下面是用他/她的方法进行的修改  在测试时,发现这样会更提升代码执行效率 测试浏览器===》 firefox 30 dev
    var cache_num = num,
        res = 0;
    while(--times > -1){
        res += (n-0);
        num += ("" + cache_num);
    }
    return res;
};

Copy link

ghost commented May 21, 2014

function qiuhe(a,num){
    var s=0,result=[],finalresult=0;
    for(var i=0;i<num;i++){
        s=s+a*Math.pow(10,i);
        result.push(s);
    }
    for(var j=0;j<result.length;j++){
        finalresult+=result[j];
    }
    return finalresult;
}
alert(qiuhe(3,4));

@bee0060
Copy link

bee0060 commented May 21, 2014

var sum = function(a, count){
      if(isNaN(a) || isNaN(count) || a!==parseInt(a) || count!==parseInt(count)){
          return 0;
      }
      var sum = 0;
      for(var i=1; i<=count;i++)
      {
          var currentLoop = new Array(i+1).join(a);
      sum = +sum + +currentLoop;
      }
      return sum;
}

@zjhsd2007
Copy link

function a(n,l){
    var arr = new Array(l+1).join(n),i=1,re=0;
    for(;i<=l;i++){
        re += arr.slice(0,i)-0;
    }
    return re;
}
console.log(a(2,5));

@jiangtao
Copy link

jiangtao commented Jun 4, 2014

//借助repeat方法

function sum(num, n){

    var a = [], i = 0, res = 0;
    for(; i <= n; i++){
        res += ~~repeat(num, i);
    }
    return n ? res : 0;
}

function repeat(num, n){
    return new Array(n+1).join(num);
}

console.log(sum(2, 3));

@zhanhongtao
Copy link

加法: 从低位加到高位.

function sum( m, n ) {
  m = ~~m, n = ~~n;
  if ( m < 1 || n < 1 ) return m;
  var ret = m, cur = m, i = 0;
  while ( ++i < n ) {
    cur = cur * 10 + m;
    ret += cur;
  }
  return ret;
}

@wsgouwan
Copy link

    function getValue(v, num){     //   v代表数字, num代表几个
            if( num <= 1){
                return v ;
            }else{
                return v*fn( num ) + getValue(v, num - 1);
            }
        }

        function fn( num ){
                var a = '' ;
                for(var i = 0; i < num; i ++ ){
                    a += 1 ;
                }
                return parseInt(a); 
        };

        console.log( getValue( 3,3 ) )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment