LoginSignup
3
2

More than 3 years have passed since last update.

今更きけない桁数の求め方

Posted at

はじめに

どうも僕は衝撃を受けました。こんな桁数の求め方があったのかと。とても恥ずかしくて周りには言えないです。でも、僕と同じ境遇に立っている人がいるかもしれません。
そいう人たちのために僕が犠牲となって、共有しようと思います。

扱う言語はC++です。

桁数の求め方

簡単にいうと、整数を文字列にして文字列の長さを求めれば桁数が出る。それだけです。
簡単ですね。

それではC++を使って実際にコードを書きます。

sample.cpp

#include <iostream>
#include<string>


using namespace std;

int main(){


    int n1 = 987654321;
    int n2 = 12345;
    int n3 = 159;

    string s1 = to_string(n1);
    string s2 = to_string(n2);
    string s3 = to_string(n3);

    int ans1 = s1.length();
    int ans2 = s2.length();
    int ans3 = s3.length();

    cout << ans1 << endl << ans2 << endl << ans3 << endl;
}

はい。簡単ですね。こんなこと今更知ったわけですよ。

以上です。最後までお付き合いいただきありがとうございます。

3
2
9

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
3
2