Skip to content

Instantly share code, notes, and snippets.

@kazmura11
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save kazmura11/961acd5dc9585f0542ff to your computer and use it in GitHub Desktop.

Select an option

Save kazmura11/961acd5dc9585f0542ff to your computer and use it in GitHub Desktop.
C++入出力1
#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