1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[学習備忘録AtCoder C++] ABC089 B - Hina Arare, ABC080 B - Harshed Number 2025.5.15

Last updated at Posted at 2025-05-15

for文とbreak

Hina Arare

 abc089_b.cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N ;
  cin >> N;
  string check = "Three";
  for (int i = 0; i < N; i++){
    string x;
    cin >> x;
    if (x == "Y"){
      check = "Four";
      break;
    }
  }
   cout << check << endl;
}

Yが一つでも入っていたらFourになる。最初はそれに気づかず時間が掛かってしまった。また、最後のcheck出力をfor文の中に入れてしまっていたためWAとなってしまった。

/の特性と余剰を利用

Harshed Number

abc080_b.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin >> N;
    int original_N = N;

    int sum = 0;
    while (N > 0) {
        sum += N % 10;
        N /= 10;
    }

    // 判定する(N の値は変わってしまっているので、予めコピーした値 original_N で判定)
    if (original_N % sum == 0) {
        cout << "Yes" << endl;
    }
    else {
    cout << "No" << endl;
    }
}

要はNの1の位をどのように取り出していくかということ、単純に10で割りつづけてそのあまりをsumに加算しつづける。Nは10で割った時の商を代入し続けていく。
最後はoriginal_Nsumを用いて割り切れるか判定

各桁を加算するアルゴリズムは他でも使えそうなので備忘録として記録しておく。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?