#30779: C++ 11ms, 84KB 之演算法


hugochu712@gmail.com (HugoChu)

學校 : 國立臺灣大學
編號 : 168241
來源 : [140.112.238.225]
最後登入時間 :
2023-08-29 19:30:14
a291. nAnB problem | From: [114.25.202.82] | 發表日期 : 2022-06-12 11:22

// q : 答案
// p : copy答案
// g : 猜數字
// 皆以字元存取
// 一種消去法的概念
// 若發現一樣的數字就刪掉那兩個數字
// 可以使用流水號覆蓋掉原本的數字
 
char p1, p2, p3, p4;
char q1, q2, q3, q4;
char g1, g2, g3, g4;
char A, B;
 
A = B = '0';
p1 = q1, p2 = q2, p3 = q3, p4 = q4;
if (p1 == g1) A++, p1 = g1 = 0;
if (p2 == g2) A++, p2 = g2 = 1;
if (p3 == g3) A++, p3 = g3 = 2;
if (p4 == g4) A++, p4 = g4 = 3;
if (p1 == g2) B++, p1 = g2 = 4;
else if (p1 == g3) B++, p1 = g3 = 5;
else if (p1 == g4) B++, p1 = g4 = 6;
if (p2 == g1) B++, p2 = g1 = 7;
else if (p2 == g3) B++, p2 = g3 = 8;
else if (p2 == g4) B++, p2 = g4 = 9;
if (p3 == g1) B++, p3 = g1 = 10;
else if (p3 == g2) B++, p3 = g2 = 11;
else if (p3 == g4) B++, p3 = g4 = 12;
if (p4 == g1) B++, p4 = g1 = 13;
else if (p4 == g2) B++, p4 = g2 = 14;
else if (p4 == g3) B++, p4 = g3 = 15;
 
ZeroJudge Forum