傳送門
https://pintia.cn/problem-sets/994805260223102976/problems/994805271455449088
題目
給定N個非0的個位數字,用其中任意2個數字都可以組合成1個2位的數字。要求所有可能組合出來的2位數字的和。例如給定2、5、8,則可以組合出:25、28、52、58、82、85,它們的和為330。
輸入格式:
輸入在一行中先給出N(1<N<10),隨后是N個不同的非0個位數字。數字間以空格分隔。
輸出格式:
輸出所有可能組合出來的2位數字的和。
輸入樣例:
3 2 8 5
輸出樣例:
330
分析
先讀入數據到數組,再遍歷數組,計算方法以樣例為例:2 8 5
int sum = 0;
// 2 and 8
sum = sum + 2 * 10 + 8; //28
sum = sum + 8 * 10 + 2; //82
// 2 and 5
sum = sum + 2 * 10 + 5; //25
sum = sum + 5 * 10 + 2; //52
// 8 and 5
sum = sum + 8 * 10 + 5; //85
sum = sum + 5 * 10 + 8; //58
源代碼
//C/C++實現
#include <iostream>
#include <vector>
using namespace std;
int main(){
int n;
scanf("%d", &n);
vector<int> v(n);
for(int i = 0; i < n; ++i){
scanf("%d", &v[i]);
}
int sum = 0;
for(int i = 0; i < n; ++i){
for(int j = i + 1; j < n; ++j){
sum += v[i] * 10 + v[j];
sum += v[j] * 10 + v[i];
}
}
printf("%d\n", sum);
return 0;
}