您的位置:首页 > 其它

Help Johnny解题报告

2013-07-14 10:43 106 查看
题目摘要: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;

}

解题感想:将红球和篮球的值分开储存然后操作比较简单,这题也不算难。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  解题报告