刷题总结——ball(ssoj)

题目:

题目背景

SOURCE:NOIP2015-SHY-9

题目描述

Alice 与 Bob 在玩游戏。他们一共玩了 t 轮游戏。游戏中,他们分别获得了 n 个和 m 个小球。每个球上有一个分数。每个人的得分都为他所获得所有小球分数的乘积,分数小者获胜。问每轮游戏谁会获胜?请输出每轮游戏的胜者。数据保证不会出现平局,且两个人分数差异大于任意一个人分数的 1% 。

输入格式

第一行为两人玩的轮数 t(1≤t≤10)。
每一轮游戏的输入中:
第一行一个整数 n,代表 Alice 获得球的个数。
第二行为 n 个整数 ai,代表 Alice 每个球的分数。
第三行一个整数 m,代表 Bob 获得球的个数。
第四行为 m 个整数 bi,代表 Bob 每个球的分数。

输出格式

输出共 t 行,每行为该轮胜者的名字“Alice”或“Bob”。

样例数据 1

输入  [复制]

 

 



2 3 4 

1 3 4 5

输出

Alice

备注

【样例说明】
Alice:2 * 3 * 4 = 24
Bob: 1 * 3 * 4 * 5 = 60

【数据范围】
对于 40% 的数据:n,m,ai,bi≤10;
对于 100% 的数据:1≤n,m≤100000;-10000≤ai,bi≤10000。

题解:

由于乘积过大容易想到把乘积转换为加减形式····所以联想到了对每一个乘数取log然后相加比较··

注意判断负数和0的情况

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
inline int R()
{
  char c;int f=0,i=1;
  for(c=getchar();(c<'0'||c>'9')&&c!='-';c=getchar());
  if(c=='-')  c=getchar(),i=-1;
  for(;c<='9'&&c>='0';c=getchar())  f=(f<<3)+(f<<1)+c-'0';
  return f*i;
}
int T,n,m,cnt1,cnt2;
double tot1,tot2;
inline double Abs(int a)
{
  double t=a;
  return t<0?-t:t;
}
int main()
{
  //freopen("a.in","r",stdin);
  T=R();double a;  
  while(T--)
  {
    bool flag1=false,flag2=false;
    tot1=tot2=0;
    n=R();cnt1=0;
    for(int i=1;i<=n;i++)
    {  
      a=R();if(a<0) cnt1++;
      else if(a==0) flag1=true;
      tot1+=log(Abs(a));
    }
    m=R();cnt2=0;
    for(int i=1;i<=m;i++)
    {
      a=R();if(a<0) cnt2++; 
      else if(a==0) flag2=true;
      tot2+=log(Abs(a));
    }
    if(flag1==true&&cnt2%2==0)  cout<<"Alice"<<endl;
    else if(flag1==true&&cnt2%2==1)  cout<<"Bob"<<endl;
    else if(flag2==true&&cnt1%2==0)  cout<<"Bob"<<endl;
    else if(flag2==true&&cnt1%2==1)  cout<<"Alice"<<endl;
    else if(cnt1%2==1&&cnt2%2==0)  cout<<"Alice"<<endl;
    else if(cnt1%2==0&&cnt2%2==1)  cout<<"Bob"<<endl;
    else if(cnt1%2==0&&cnt2%2==0)
    {
      if(tot1<tot2)  cout<<"Alice"<<endl;
      else cout<<"Bob"<<endl;
    }
    else if(cnt1%2==1&&cnt2%2==1)
    {
      if(tot1<tot2)  cout<<"Bob"<<endl;
      else cout<<"Alice"<<endl;
    }
  }
  return 0;
}

 

posted @ 2017-10-08 19:04  AseanA  阅读(213)  评论(0编辑  收藏  举报