洛谷 P1148 拱猪计分

  • admin
  • 2026-06-28 05:29:43

【题目链接】

洛谷 P1148 拱猪计分

题目补充说明:

D10与D11都可能出现,D10作用与D11相同。如果D10与D11同时出现,只算有1张D牌。

当H1~H13都有,但D或S只有1个时:D牌作用:-100分,S牌作用:+100分。

【题目考点】

1. 模拟

【解题思路】

根据题意直接模拟

设布尔数组h,用来记录是否有牌Hi。设布尔变量s12, d10, c10,表示是否存在S、D和C牌。

按照题目描述,根据该玩家手中存在的牌的种类,计算分数。

具体解释见注释。

【题解代码】

解法1:模拟

cpp

复制代码

#include

#define INF 0x3f3f3f3f

using namespace std;

#define N 105

int proc(int n)//n:有n张牌

{

string s;

int score = 0;

bool h[20] = {}, s12 = false, d10 = false, c10 = false;//h[1]~p[13]:H1~H13 是否存在

int pt[15] = {0,-50,-2,-3,-4,-5,-6,-7,-8,-9,-10,-20,-30,-40};//pt[i]:牌Hi的分数

for(int i = 1; i <= n; ++i)

{

cin >> s;

if(s[0] == 'H')

h[stoi(s.substr(1))] = true;//标记h[i]为真

else if(s[0] == 'S')

s12 = true;

else if(s[0] == 'D')//D10与D11都认为是D10

d10 = true;

else if(s[0] == 'C')

c10 = true;

}

bool hasAllH = true;//是否有所有13张H牌

for(int i = 1; i <= 13; ++i)

if(h[i] == false)

{

hasAllH = false;

break;

}

if(hasAllH)//如果有全部13张红心牌

{

score = 200;//全红心牌加200

if(s12 && d10)//如果同时有S牌和D牌

score = 500;

else if(s12)//如果只有S,那么减100

score -= 100;

else if(d10)//如果只有D,那么加100

score += 100;

if(c10)//如果有C牌,分数乘2

score *= 2;

}

else//如果并没有持有所有的红心牌

{

for(int i = 1; i <= 13; ++i)

if(h[i])//如果有Hi这张牌

score += pt[i];

if(score == 0 && !s12 && !d10 && c10)//如果只有C牌没有其它牌

score += 50;

else

{

if(s12)//如果有S牌

score -= 100;

if(d10)//如果有D牌

score += 100;

if(c10)//如果有C牌

score *= 2;

}

}

return score;

}

int main()

{

bool isOver;

int n, score[5];//score[i]:某局游戏后第i名玩家的得分

while(true)

{

isOver = true;

for(int i = 1; i <= 4; ++i)

{

cin >> n;

if(n != 0)

isOver = false;

score[i] = proc(n);//求出第i名玩家的得分

}

if(isOver)//如果输入4个0,则跳出

break;

for(int i = 1; i <= 4; ++i)//输出每位玩家的得分

cout << (score[i] > 0 ? "+" : "") << score[i] << ' ';//如果是正数则输出正号

cout << endl;

}

return 0;

}