6 个解决方案
#1
int a[]={1,2,3,4,5},b[ ]={1,2,3,4,5};
for(int ib: b){
int s=0;
for(int ia: a){
if(s) cout<<'+';
cout<<ia+ib;
s +=ia+ib;
}
cout<<'='<<s<<endl;
}
#2
int a[]={1,2,3,4,5,},b[]={1,2,3,4,5};
int size_a = sizeof(a)/sizeod(int),size_b = sizeof(b)/sizeod(int);
int total_b =0;
for(int i=0;i<size_b;++i)
{
total_b+=b[i];
}
for(int j=0;j<size_a;++j)
{
printf("%d ",a[j]*size_b+total_b);
}
#3
凑个热闹:
2.2.1 :006 > a = (1..5).to_a
=> [1, 2, 3, 4, 5]
2.2.1 :007 > b = (1..5).to_a
=> [1, 2, 3, 4, 5]
2.2.1 :008 > a.each do |x|
2.2.1 :009 > c = b.map {|y|y+x}
2.2.1 :010?> total = c.inject {|s,i|s+i}
2.2.1 :011?> puts "#{c.join(" + ")} = #{total}"
2.2.1 :012?> end
2 + 3 + 4 + 5 + 6 = 20
3 + 4 + 5 + 6 + 7 = 25
4 + 5 + 6 + 7 + 8 = 30
5 + 6 + 7 + 8 + 9 = 35
6 + 7 + 8 + 9 + 10 = 40
#4
矩阵相加
#5
//先将数组b中的数字全部加起来得到一个和值,然后将数组a中的每个数乘以5加上那个和值,也得到你想要的数啦
#include<iostream>
using namespace std;
void main()
{
int a[]={1,2,3,4,5};
int b[]={1,2,3,4,5};
int lena=sizeof(a)/sizeof(int);
int lenb=sizeof(b)/sizeof(int);
int cursum=0;
for(int i=0;i<lenb;i++)
{
cursum+=b[i];
}
cout<<cursum<<endl;
for(int j=0;j<lena;j++)
{
int sum=0;
sum=cursum+a[j]*5;
cout<<sum<<endl;
}
}
#6
#include <stdio.h>
int main(void)
{
int i = 0; //for loop
int sum_b = 0;
int sum_c = 0;
int count = 0;
int a[] = {1, 2, 3, 4, 5};
int b[] = {1, 2, 3, 4, 5};
count = sizeof(b) / sizeof(b[0]);
for (i = 0; i < count; i++)
{
sum_b += b[i];
}
for (i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
sum_c = sum_b + a[i] * count;
printf("c[%d] = %d\n", i, sum_c);
}
return 0;
}
#1
int a[]={1,2,3,4,5},b[ ]={1,2,3,4,5};
for(int ib: b){
int s=0;
for(int ia: a){
if(s) cout<<'+';
cout<<ia+ib;
s +=ia+ib;
}
cout<<'='<<s<<endl;
}
#2
int a[]={1,2,3,4,5,},b[]={1,2,3,4,5};
int size_a = sizeof(a)/sizeod(int),size_b = sizeof(b)/sizeod(int);
int total_b =0;
for(int i=0;i<size_b;++i)
{
total_b+=b[i];
}
for(int j=0;j<size_a;++j)
{
printf("%d ",a[j]*size_b+total_b);
}
#3
凑个热闹:
2.2.1 :006 > a = (1..5).to_a
=> [1, 2, 3, 4, 5]
2.2.1 :007 > b = (1..5).to_a
=> [1, 2, 3, 4, 5]
2.2.1 :008 > a.each do |x|
2.2.1 :009 > c = b.map {|y|y+x}
2.2.1 :010?> total = c.inject {|s,i|s+i}
2.2.1 :011?> puts "#{c.join(" + ")} = #{total}"
2.2.1 :012?> end
2 + 3 + 4 + 5 + 6 = 20
3 + 4 + 5 + 6 + 7 = 25
4 + 5 + 6 + 7 + 8 = 30
5 + 6 + 7 + 8 + 9 = 35
6 + 7 + 8 + 9 + 10 = 40
#4
矩阵相加
#5
//先将数组b中的数字全部加起来得到一个和值,然后将数组a中的每个数乘以5加上那个和值,也得到你想要的数啦
#include<iostream>
using namespace std;
void main()
{
int a[]={1,2,3,4,5};
int b[]={1,2,3,4,5};
int lena=sizeof(a)/sizeof(int);
int lenb=sizeof(b)/sizeof(int);
int cursum=0;
for(int i=0;i<lenb;i++)
{
cursum+=b[i];
}
cout<<cursum<<endl;
for(int j=0;j<lena;j++)
{
int sum=0;
sum=cursum+a[j]*5;
cout<<sum<<endl;
}
}
#6
#include <stdio.h>
int main(void)
{
int i = 0; //for loop
int sum_b = 0;
int sum_c = 0;
int count = 0;
int a[] = {1, 2, 3, 4, 5};
int b[] = {1, 2, 3, 4, 5};
count = sizeof(b) / sizeof(b[0]);
for (i = 0; i < count; i++)
{
sum_b += b[i];
}
for (i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
sum_c = sum_b + a[i] * count;
printf("c[%d] = %d\n", i, sum_c);
}
return 0;
}