AOJ : 0226 - Hit and Blow (ヒットアンドブロー)
問題概要
http://rose.u-aizu.ac.jp/onlinejudge/ProblemSet/description.jsp?id=0226&lang=jp
日本語の問題文なので説明省略です
アルゴリズム
ヒットとブローの関数作って, それを呼び出すだけです.
2分で書いて, みごと出だしだけ1位をゲットしました.
プログラム
#include <iostream> using namespace std; int hit(string a,string b){ int res = 0; for(int i=0;i<4;i++){ if(a[i] == b[i]) res++; } return res; } int blow(string a,string b){ int res = 0; for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(i != j && a[i] == b[j]){ res++; break; } } } return res; } int main(void){ string a,b; while(cin>>a>>b, a!="0"){ cout<<hit(a,b)<<" "<<blow(a,b)<<endl; } return 0; }