決戰21點
Time Limit:1000MS Memory Limit:65536K
Total Submit:71 Accepted:22
Description
21點是一個非常有趣的遊戲,遊戲規則是兩個人各自抽取若干張牌後
看誰點數更大(但不能超過21點,否則算0)如果兩個點數相等,則莊家獲勝
2 3 4 5 6 7 8 9點數都是各自的數碼
10 J Q K 都是10點
A既可以當成11點,也可以當成1點。
Input
每組數據包括 n m表示莊家和閒家的牌總數
接下來兩行分別表示莊家和閒家的牌
Output
輸出比分以及莊家贏(HOST WIN)還是閒家贏(GUEST WIN)
Sample Input
Sample Input
2 3
A J
A A Q
2 6
Q J
A A A A 7 K
Sample Output
Sample Output
21 vs 12 HOST WIN
20 vs 21 GUEST WIN
Source
實際上這道題是當時校賽最虧的一道題了,現在寫一遍感覺真心不難,很基礎,當時看完題目以爲不太好寫,說留最後搞,然後比完之後也沒興趣去看這題了,就一直放那了,今天點點看到了,寫了一下,不難,唉,失誤失誤。
思路簡單描述一下吧:首先輸入後把那些固定的牌的點數先加上,然後再來處理A的問題,怎麼處理?實際上題意就是這個A到底是作爲1還是11,那我們就枚舉,先0張A作爲1,其他剩的就爲11,一直到一半就行了,然後找出<=21點中的最大和,好,這就是你的點數。實際莊家和閒家的處理是一樣的,把代碼再複製一遍即可,對於超過21點的,置爲0即可。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AK1291 {
/// <summary>
/// 一開始沒考慮最小和超過21點爲0,WA了一次,然後加上就AC了
/// 感覺這題真不難,不就幾張當1,剩下當11嘛,你說比賽的時候咋就不寫呢,不能忍
/// what's the fuck
/// </summary>
class Program {
static void Main(string[] args) {
string s;
while ((s = Console.ReadLine()) != null) {
string[] ss = s.Split();
int n = int.Parse(ss[0]), m = int.Parse(ss[1]);
string[] a = Console.ReadLine().Split();
string[] b = Console.ReadLine().Split();
int acount = 0, bcount = 0;//表示a和b中有多少個A
int asum = 0, bsum = 0;
for (int i = 0; i < n; i++) {
if (a[i].Equals("A")) acount++;
else {
if (a[i].Equals("J") || a[i].Equals("Q") || a[i].Equals("K") || a[i].Equals("10"))
asum += 10;
else asum += int.Parse(a[i]);
}
}
for (int i = 0; i < m; i++) {
if (b[i].Equals("A")) bcount++;
else {
if (b[i].Equals("J") || b[i].Equals("Q") || b[i].Equals("K") || b[i].Equals("10"))
bsum += 10;
else bsum += int.Parse(b[i]);
}
}
//ok 前面處理完其他數據了,下面只要加上多少個1還是11了
int amax = asum, bmax = bsum;
if (asum + acount > 21) amax = 0;//這些牌最小和都超過21了,置0
else {
for (int i = 0; i <= acount; i++) {
int aans = i + (acount - i) * 11;//i張牌當做1,acount-i就當做11
if (aans + asum <= 21 && aans + asum > amax) amax = aans + asum;//找到符合條件的最大牌的和
}
}
if (bsum + bcount > 21) bmax = 0;
else {
for (int i = 0; i <= bcount; i++) {
int bans = i + (bcount - i) * 11;
if (bans + bsum <= 21 && bans + bsum > bmax) bmax = bans + bsum;
}
}
if (amax >= bmax) Console.WriteLine("{0} vs {1} HOST WIN", amax, bmax);
else Console.WriteLine("{0} vs {1} GUEST WIN", amax, bmax);
}
}
}
}
ok