LoginSignup
4
1

More than 3 years have passed since last update.

【AtCoder/C++】3行でPlacing Marblesを解いてみた

Posted at
1 / 2

C++とアルゴリズムの勉強にAtCoderをはじめました。
PlacingMarblesをうまく解けたので、共有します。

問題

0か1が書かれたマス3つから、1が何個あるかを数えるというもの。

回答

main.cpp
#include <iostream>
using namespace std;

int main() {
  int a;
  cin >> a;
  cout << a % 9 << endl;
}

数値として扱って、余りでカウントする。

解説

111 => 3
110,101,011 => 2
100,010,001 => 1
000 => 0
なので、何個か数値を試して3の倍数が良さそうで、試したら9で行けた。

以上です。これからもAtCoderやっていきます。もっといい方法があれば教えてください。

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