03-guess-boj

来源:互联网 发布:cms官网 编辑:程序博客网 时间:2024/06/03 01:42

boj上的guess:

写了一天终于调出来,题目本身不难,主要是让你预测一个排名是否可行,可行的话输出最后一名的最高得分。

这个题目主要考虑以下两点:

1.如何考虑是否可行及最高得分的计算。我用的方法依然比较笨的,即从第一名开始,记录其最高分为三题总和,之后每个人有8中可能得分(0,。。。),按排名顺序,计算第二名最高得分,这里主义考虑可能与第一名分数相同,只要该分数在其8种之内,且id小于前者。依次计算之后的人员的可能最高分,如果出现连续两个零,且后者id小于前者,则该排名不可能。因为大家都按可能最高分来排,都不能为后面的挤出得分的空来,哈哈。

2.浮点数的限制。这里要求输入为最多两位小数,输出最多两位小数,注意浮点数的比较。看下面的小程序的结果:

  while( cin>>a>>b)  {             int aa = (int)(a*100 + 0.001);    if( a==b )      cout<<"相等"<<endl;    else      cout<<"不想等"<<endl;     double f;     f=0.1;     if( f*10.0 == 1.0 )      cout<<"f-equal"<<endl;    else     cout<<"f-not-equal"<<endl;  }
结果:
1.9 1.9000000000000000000000000001 
相等
f-not-equal
至于原理我也不太清楚。这个算法最后转换为整数处理。
以下是愚蠢的个人代码:

#include "iostream"#include "stdio.h"#include "vector"#include "set"#include <iomanip>using namespace std;int getHighScore1(const vector<int> &);int getHighScore2(const vector<int> & , int maxHighScore , int , int );int main(){  int count = 1;  int playerNum = 0;    while(cin>>playerNum,playerNum != 0)  {    vector< vector<int> >scoreVec;    vector<int>rankVec;    double tempa = 0;    double tempb = 0;    double tempc = 0;    for(int i = 0; i < playerNum; i++)    {      cin>>tempa>>tempb>>tempc;      vector<int> tempVec;      tempVec.push_back((int)(tempa*100 + 0.001));      tempVec.push_back((int)(tempb*100 + 0.001));      tempVec.push_back((int)(tempc*100 + 0.001));      scoreVec.push_back(tempVec);     //printf("%f%f%f", tempa , tempb , tempc);    }    int tempPlayer;    for(int i = 0; i < playerNum; i++ )    {      cin>>tempPlayer;      rankVec.push_back(--tempPlayer);    }    if( playerNum != 0 )    {      int preId = rankVec[0];      int maxHighScore;      maxHighScore = scoreVec[rankVec[0]][0] + scoreVec[rankVec[0]][1] + scoreVec[rankVec[0]][2];      for( int i = 1; i < playerNum; i++)      {        int rankScore = scoreVec[rankVec[i]][0] + scoreVec[rankVec[i]][1] + scoreVec[rankVec[i]][2];        if( rankScore < maxHighScore )        {          maxHighScore = rankScore;          preId = rankVec[i];        }        else if( rankScore == maxHighScore)        {          if( rankVec[i] > preId )          {         maxHighScore = rankScore;             preId = rankVec[i];          }          else          {            if( maxHighScore == 0 && preId > rankVec[i])            {              maxHighScore = -1;              break;            }             maxHighScore = getHighScore1( scoreVec[rankVec[i]]);            preId = rankVec[i];          }        }        else        {          if( maxHighScore == 0 && preId > rankVec[i])          {              maxHighScore = -1;              break;          }          maxHighScore = getHighScore2( scoreVec[rankVec[i]], maxHighScore, preId , rankVec[i]);          preId = rankVec[i];        }      }      if( maxHighScore == -1 )          printf("Case %d: No solution\n",count++);      else      {           printf("Case %d: %.2f\n",count++,(double)maxHighScore/100.00);      }    }  }  return 0;}int getHighScore1(const vector<int> &score){  int maxScore = 0;  int sum1 = score[0] + score[1];  int sum2 = score[0] + score[2];  int sum3 = score[1] + score[2];  maxScore = sum1;  if( maxScore < sum2 )  {    maxScore = sum2;   }  if( maxScore < sum3 )    maxScore = sum3;  return maxScore;}int getHighScore2(const vector<int> & score ,int maxHighScore , int curId , int rankId ){  int maxScore = 0;  set<int> sSet;  sSet.insert(score[0]);  sSet.insert(score[1]);  sSet.insert(score[2]);  sSet.insert(score[0] + score[1]);  sSet.insert(score[0] + score[2]);  sSet.insert(score[1] + score[2]);  sSet.insert(score[1] + score[2] + score[0] );  //if( aSet.size() <   set<int>::iterator iter = sSet.find(maxHighScore);  if( iter!= sSet.end() )  {    if( rankId >curId )    {      maxScore = maxHighScore;    }    else    {      if( iter != sSet.begin() )      {    iter--;    maxScore = *iter;      }      else      {    maxScore = 0;      }    }  }  else  {    sSet.insert(maxHighScore);    set<int>::iterator it = sSet.find(maxHighScore);    if( it != sSet.begin() )    {      it--;      maxScore = *it;    }    else    {      maxScore = 0;    }  }  return maxScore;}