AOJ0186 Aizu Chicken

解法
地鶏を上限まで買えるだけ買った後、鶏肉の地鶏と普通の鶏との合計が足りるように地鶏を減らしながら調整する。地鶏が1匹も購入できない場合は "NA" と出力。

ポイント

  • 購入できる地鶏の数を予算から単価を整数除算することで見積もること
  • 購入する地鶏の分の料金を予算から引けば、普通の鶏にかけられるお金が決まるので、普通の鶏の単価で整数除算する
#include <bits/stdc++.h>
using namespace std;
 
void failed() {
  cout << "NA" << endl;
}
  
int main() {
    
  int Q1, B, C1, C2, Q2;
    
  while(cin >> Q1 >> B >> C1 >> C2 >> Q2) {
    if(Q1 == 0) break;
    int AizuCh = min(B/C1, Q2);
    int NCh = 0;
    for(;;) {
      if(AizuCh <= 0) { failed(); break; }
      NCh = (B - AizuCh*C1)/C2;
      if(AizuCh + NCh >= Q1) {
        cout << AizuCh << ' ' << NCh << endl; break;
      }
      else AizuCh --;
    }
  }
    
  return 0;
}