Help Johnny解题报告

题目摘要:Poor Johnny is so busy this term.His tutor threw lots of hard problems to him and demanded him to accomplishthose problems in a month. What a wicked tutor! After cursing his tutorthousands of times,Johnny realized that he must start his work immediately.Thevery problem Johnny should solve firstly is about a strange machine calledWarmouth. In the Warmouth there are many pairs of balls. Each pairconsists of a red ball and a blue ball and each ball is assigned a value.We canrepresent a pair in the form of (R, B) in which R is the value of the red balland B is of the blue one.

Warmouth has a generator to calculate thematch value of two pairs. The match value of (R1, B1) and (R2,B2) isR1*B2+R2*B1. Initially, Warmouth is empty. Pairs are sent into Warmouth inorder. Once a new pair comes, it will be taken into the generator with all thepairs already in Warmouth.

Johnny’s work is to tell his tutor the sumof all match values given the list of pairs in order. As the best friend ofJohnny, would you like to help him?

题目大意:给出一串数字,两个一组,前面的代表红球的值,后面的代表篮球的值。每两对数值用公式R1*B2+R2*B1求和,然后算出综合即可。

输入输出要求

Input

The first line of the input is T (no morethan 10), which stands for the number of lists Johnny received.Each list beginswith “N“(without quotes). N is the number of pairs of this list and is no morethan 100000.The next line gives N pairs in chronological order. The 2i-thnumber is the value of the red ball of the i-th pair and the (2i+1)-th numberis the value of the blue ball of the i-th pair. The numbers are positive integersand smaller than 100000.

 

Output

Please output the result in a single linefor each list.

输入输出样例

Sample Input

2

3

1 3 2 2 3 1

2

4 5 6 7

Sample Output

26

58

解题思路:建立两个数组,一个用来储存红球的数值,另一个储存篮球的数值。以第一个样例为例,红球的数值为1,2,3,篮球的数值为3,2,1,所求的和为1*2+1*1+2*3+2*1+3*3+3*2=26也就是第i个红球的值要和除i外的所有篮球的值都乘一遍,然后求和。用两个循环即可解题。

代码

#include <iostream>

using namespace std;

 

const int maxn=100000+5;

int str1[maxn];

int str2[maxn];

int main()

{

   long long sum1,sum2,ans;

       intn,m,i,j=0;

       cin>>n;

       for(j=1;j<=n;j++)

       {

           ans=0,sum1=0,sum2=0;

              cin>>m;

              for(i=1;i<=m;i++)

              {

                     cin>>str1[i]>>str2[i];

                     sum1=sum1+str1[i];

                     sum2=sum2+str2[i];

              }

       for(i=1;i<m;i++)

           {

                  sum1=sum1-str1[i];

                  sum2=sum2-str2[i];

                  ans=ans+(str1[i]*sum2+str2[i]*sum1);

           }

           cout<<ans<<endl;

       }

       return0;

}

解题感想:将红球和篮球的值分开储存然后操作比较简单,这题也不算难。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值