九度 Online Judge 算法 刷题 题目1051:数字阶梯求和

题目1051:数字阶梯求和

题目描述:
给定a和n,计算a+aa+aaa+a…a(n个a)的和。
输入:
测试数据有多组,输入a,n(1<=a<=9,1<=n<=100)。
输出:
对于每组输入,请输出结果。
样例输入:
1 10
样例输出:
1234567900
来源:
2010年哈尔滨工业大学计算机研究生机试真题

code

C++ 实现

#include <iostream>
#include<vector>
#include<algorithm>
#include <math.h> 

using namespace std;

//int divisor(int a) {
//  int sum = 0;
//  for (int i=1; i<sqrt((float)a); i++) {
//      if (a % i == 0)
//          ++sum;
//  }
//  sum*=2;
//  if(a == pow((int)sqrt((float)a),2))
//      ++sum;
//  cout<<"sqrt((float)a)"<<sqrt((float)a)<<endl;
//  cout<<"pow((int)sqrt((float)a),2))"<<pow((int)sqrt((float)a),2)<<endl;
//  cout<<"pow(sqrt((float)a),2))"<<pow(sqrt((float)a),2)<<endl;
//  
//  return sum;
//}

int main() {
    int a,n;
    while (cin>>a>>n) {
        int sum = 0;
        vector<int> vecInt(120);
        int temp = 0 ;
        for (int i = n; i >0; --i) {
            vecInt[n+1-i] = (temp+(a * i)) % 10 ;
            temp = (temp+(a * i)) / 10;
        }   
        if (temp != 0)  
            cout<<(int)temp;
        for(int i = n; i > 0; --i) 
            cout<<vecInt[i];
        cout<<endl;

    }

//      vector<long> vecInt(n);
//      for (int i = 0; i < n; i++) {
//          int temp;
//          cin>>temp;
//          vecInt[i] = temp;
//      }
//  
//      for(vector<long>::iterator it = vecInt.begin();it != vecInt.end();it++) {
//          cout<<divisor(*it)<<endl;
//      
//      }
//  }


    return 0;
}
/**************************************************************
    Problem: 1051
    User: langzimaizan
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值