Last active
August 29, 2015 14:06
-
-
Save kazmura11/961acd5dc9585f0542ff to your computer and use it in GitHub Desktop.
C++入出力1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <sstream> | |
| #include <string> | |
| #include <climits> | |
| using namespace std; | |
| template <typename T> | |
| string numToString(const T &num) | |
| { | |
| ostringstream oss; | |
| oss << num; | |
| return oss.str(); | |
| } | |
| int main() | |
| { | |
| unsigned long ul; | |
| cout << "正の整数の上限を設定してください: "; | |
| cin >> ul; | |
| if (ul < 0 || ul > ULONG_MAX) | |
| { | |
| cout << "範囲外の数値です" << endl; | |
| return 1; | |
| } | |
| int idx = 0; | |
| size_t npos = 0; | |
| int cnt = 0; | |
| for (unsigned long i = 1; i <= ul; i++) | |
| { | |
| string s = numToString(i); | |
| while (idx < s.length()) | |
| { | |
| if ((npos = s.find("7", idx)) != string::npos) | |
| { | |
| idx += (npos+1); | |
| cnt++; | |
| } | |
| else | |
| { | |
| idx++; | |
| } | |
| } | |
| npos = 0; | |
| idx = 0; | |
| } | |
| cout << "7の個数: " << cnt << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment