题目:
有n个人,每个人都拥有一个喜悦值,接下来,任意两个人之间会进行一次交流,任意两个人交流所获得的贡献为两个人的喜悦值的和,求任意两个人交流所获得的贡献之和
输入格式:
第一行一个整数n
接下来n行,每行一个整数a,表示第i个人的喜悦值
输出格式:
按题目描述输出
样例输入1:
3
2
3
4
样例输出1:
18
约定:
n<=1000
1<=a<=10^100
这是我的代码是方法那里搞错了吗
#include<bits/stdc++.h>
using namespace std;
string str;
void q(string a,string b){
int a1[1001]={},b1[1001]={},c1[1001]={};
int lena=a.size(),lenb=b.size();
int len=max(lena,lenb);
for(int i=0;i<lena;i++){
a1[i]=a[lena-i-1]-'0';
}
for(int i=0;i<lenb;i++){
b1[i]=b[lenb-i-1]-'0';
}
int x=0;
for(int i=0;i<=len;i++){
c1[i]=a1[i]+b1[i]+x;
if(c1[i]>=10){
c1[i]-=10;
x=1;
}else{
x=0;
}
}
if(c1[len]==0) len--;
for(int i=len;i>=0;i--){
str[len-i]=c1[i]+48;
}
}
int main(){
int n;
string s;
cin>>n;
for(int i=0;i<n;i++){
cin>>s;
q(str,s);
}
cout<<str;
return 0;
}
反正莫输出,help me!
#include <iostream>
#include <string>
#include <vector>
class BigInt
{
public:
BigInt(unsigned int n = 0)
{
if (n == 0)
_data.push_back('0');
else
{
std::string str;
while (n)
{
str.push_back(n % 10 + '0');
n /= 10;
}
_data.assign(str.rbegin(), str.rend());
}
}
BigInt(const std::string &s) : _data(s) {}
private:
std::string _data;
friend BigInt operator+(const BigInt &lsh, const BigInt &rhs);
friend std::istream &operator>>(std::istream &is, BigInt &a);
friend std::ostream &operator<<(std::ostream &os, const BigInt &a);
};
BigInt operator+(const BigInt &lhs, const BigInt &rhs)
{
std::string r;
int carry = 0;
auto itr1 = lhs._data.rbegin();
auto itr2 = rhs._data.rbegin();
while (itr1 != lhs._data.rend() && itr2 != rhs._data.rend())
{
int a = *itr1 - '0';
int b = *itr2 - '0';
int c = a + b + carry;
if (c >= 10)
{
carry = c / 10;
c %= 10;
}
else
{
carry = 0;
}
r.push_back(c + '0');
++itr1;
++itr2;
}
while (itr1 != lhs._data.rend())
{
int a = *itr1 - '0' + carry;
if (a >= 10)
{
carry = a / 10;
a %= 10;
}
else
{
carry = 0;
}
r.push_back(a + '0');
++itr1;
}
while (itr2 != rhs._data.rend())
{
int a = *itr2 - '0' + carry;
if (a >= 10)
{
carry = a / 10;
a %= 10;
}
else
{
carry = 0;
}
r.push_back(a + '0');
++itr2;
}
if (carry > 0)
r.push_back(carry + '0');
return BigInt(std::string(r.rbegin(), r.rend()));
}
std::istream &operator>>(std::istream &is, BigInt &a)
{
is >> a._data;
return is;
}
std::ostream &operator<<(std::ostream &os, const BigInt &a)
{
os << a._data;
return os;
}
int main()
{
int n;
std::cin >> n;
std::vector<BigInt> a(n);
for (int i = 0; i < n; i++)
std::cin >> a[i];
BigInt sum = 0;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
sum = sum + a[i] + a[j];
std::cout << sum;
return 0;
}
可能是因为你用的void没有返回值