03-guess-boj
2012-11-30 16:25
274 查看
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.如何考虑是否可行及最高得分的计算。我用的方法依然比较笨的,即从第一名开始,记录其最高分为三题总和,之后每个人有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; }
相关文章推荐
- 老BOJ 03 Guess
- BOJ 1003 Guess
- Problem 1_03_guess
- BOJ 2014新生暑假个人排位赛03 整合
- 坚守主业还是多元化?-哈佛案例[03]
- 【SSH网上商城项目实战03】使用EasyUI搭建后台页面框架
- 面向对象篇(OOP)--03 static、final、对象数组以及抽象类
- 03-imp导入指令.sql
- 学会自己测天气系列八卦基础---03
- CCF认证 2014-03 相反数
- c#操作MySql.Data.DLL 分类: .NET 2013-03-08 15:49 4177人阅读 评论(2) 收藏
- Ubuntu学习总结-03 安装软件 & 技巧
- vue项目学习03
- uva 11995 - I Can Guess the Data Structure!
- python03-list_tuple_dict_set
- Python学习记录03
- BOJ 316 Password dp
- 【Sass-03】Sass官网文档
- JavaScript 从零开始_03.JavaScript作用域,运算符
- 2013 03-24