EX8 - たこ焼きセット 解説

実行時間制限: 2 sec / メモリ制限: 256 MB

説明ページに戻る

問題文

A君はたこ焼きの情報を処理するプログラムを書いています。
このプログラムは以下の2パターンの入力を処理します。


パターン1

入力

1
priceprice
NN

1行目で、パターンを表す整数1が入力されます。
2行目で、たこ焼き1個あたりの値段pricepriceが入力されます。
3行目で、たこ焼き1セットあたりの個数NNが入力されます。

出力

たこ焼き1セットあたりの値段(=N×price=N \times price)を出力します。


パターン2

入力

2
texttext
priceprice
NN

1行目で、パターンを表す整数2が入力されます。
2行目で、たこ焼きセットの説明文texttextが入力されます。
3行目で、たこ焼き1個あたりの値段pricepriceが入力されます。
4行目で、たこ焼き1セットあたりの個数NNが入力されます。

出力

1行目で、たこ焼きセットの説明文texttextの末尾に!をつけて出力します。
2行目で、たこ焼き1セット辺りの値段(=N×price=N \times price)を出力します。


A君はこの通りの動作をするプログラムを書いたつもりでしたが、プログラムを実行してみるとエラーが発生しました。
A君が書いたプログラムのエラーを修正し、正しく動作するようにしてください。

A君が書いたプログラム
Copy
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int p;
  6. cin >> p;
  7.  
  8. // パターン1
  9. if (p == 1) {
  10. int price;
  11. cin >> price;
  12. }
  13.  
  14. // パターン2
  15. if (p == 2) {
  16. string text;
  17. int price;
  18. cin >> text >> price;
  19. }
  20.  
  21. int N;
  22. cin >> N;
  23.  
  24. cout << text << "!" << endl;
  25. cout << price * N << endl;
  26. }
#include <bits/stdc++.h>
using namespace std;

int main() {
  int p;
  cin >> p;

  // パターン1
  if (p == 1) {
    int price;
    cin >> price;
  }

  // パターン2
  if (p == 2) {
    string text;
    int price;
    cin >> text >> price;
  }

  int N;
  cin >> N;

  cout << text << "!" << endl;
  cout << price * N << endl;
}

もしプログラムを修正した結果、A君が書いたプログラムとの違いが大きくなってしまったとしても、ACができればOKです。


制約

  • 0price,N1000≦price, N≦100
  • price,Nprice, Nは整数
  • texttextは半角英数字からなる
  • texttextは20文字以内

ジャッジでは以下の入力例以外のケースに関してもテストされることに注意。

入力例1 Copy

Copy
1
80
5

出力例1 Copy

Copy
400

入力例2 Copy

Copy
2
umai
150
3

出力例2 Copy

Copy
umai!
450

入力例3 Copy

Copy
2
good!
30
8

出力例3 Copy

Copy
good!!
240

テスト入出力

書いたプログラムがACにならず、原因がどうしてもわからないときだけ見てください。

クリックでテスト入出力を見る

テスト入力1
1
2
3
テスト出力1
6

テスト入力2
2
yeah!...!ok
100
100
テスト出力2
yeah!...!ok!
10000


解答例

必ず自分で問題に挑戦してみてから見てください。

クリックで解答例を見る

Copy
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int p;
  6. cin >> p;
  7.  
  8. // パターン2
  9. if (p == 2) {
  10. string text;
  11. cin >> text;
  12. cout << text << "!" << endl;
  13. }
  14.  
  15. int price, N;
  16. cin >> price >> N;
  17. cout << price * N << endl;
  18. }
#include <bits/stdc++.h>
using namespace std;

int main() {
  int p;
  cin >> p;

  // パターン2
  if (p == 2) {
    string text;
    cin >> text;
    cout << text << "!" << endl;
  }

  int price, N;
  cin >> price >> N;
  cout << price * N << endl;
}



2022-01-02 (日)
21:23:17 +00:00