LoginSignup
0
0

More than 1 year has passed since last update.

AtCoderログ:0039 - ABC 144 B

Last updated at Posted at 2021-07-28

問題

問題文

高橋君は九九を覚えたので、$1$ 以上 $9$ 以下の $2$ つの整数の積を計算することができます。
整数 $N$ が与えられるので、$N$ を $1$ 以上 $9$ 以下の $2$ つの整数の積として表すことができるか判定し、できるなら Yes を、できないなら No を出力して下さい。

制約

・$1 \le N \le 100$
・$N$ は整数である。

収録されている問題セット

回答

回答1 (AC)

受け取った整数 n が九九の表にあるかを調べるわけですが、九九の表を作成し、n が含まれているかをチェックしていけば良いでしょう。コードは以下のようになりました。今回は関数を利用しています。

abc144b-1.cpp
#include <bits/stdc++.h>
using namespace std;

string is_in_table( int n ) {
  for ( int i=1; i<=9; i++ ) {
    for ( int j=1; j<=9; j++ ) {
      if ( i*j==n ) {
        return("Yes");
      }
    }
  }
  return("No");
}

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

  cout << is_in_table( n ) << endl;
}

回答2 (AC)

九九の表に現れる値をあらかじめ計算してテーブルに保管し、与えられた整数 n がそのテーブルに含まれるかをチェックする方法でもコードを書いてみました。回答1の繰り返し回数は 9x9=81 回と少ないので、テーブルを使っても計算時間・メモリにほとんど差はありませんでした。

abc144b-2.cpp
#include <bits/stdc++.h>
using namespace std;

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

  vector<int> kuku{ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10,
                   12, 14, 15, 16, 18, 20, 21, 24, 25, 27,
                   28, 30, 32, 35, 36, 40, 42, 45, 48, 49,
                   54, 56, 63, 64, 72, 81}; 
  if ( find(kuku.begin(),kuku.end(),n)==kuku.end() ) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }
}

調べたこと

AtCoder の解説ユーザ回答

回答1と同じ方針でした。

AtCoder の解説コンテスト全体の解説

回答1と同じ方針でした。

学んだこと

  • vector における探索 (find 関数)

リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0