C++ 1022 D进制的A+B(20 分)

自写(采用向量和数组两种方式保存数据)

#include<iostream>
#include<vector>
using namespace std;

int main()
{
  int a,b,c,he,result,k=0;
  cin>>a>>b>>c;
  he =a+b; 
  vector <int> v;  //向量的方式
  int vv[100];   //数组的方式
  
  if(he==0) cout<<0;  //A B都为0的情况
  else {
    while(he!=0)
    {
    result = he%c; 
    v.push_back(result);  //向量的方式
    he = (he -result)/c;
   // vv[k]=he%c;
   // he = (he -vv[k])/c;    //数组的方式
    k++;
    }
  
  for(int i=0;i<k;i++){
    cout<<v[k-1-i];   //向量的方式
    //cout<<vv[k-1-i];    //数组的方式
    }
  }
  
  return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36122764/article/details/82181112