- #include <iostream>
- #include <string>
- #include <sstream>
- #include <cmath>
- #include <vector>
- using namespace std;
- double string_to_num(string str)
- {
- double back;
- istringstream instr(str);
- instr>>back;
- return back;
- }
- int main()
- {
- int len;
- double mm;
- int countNum[10] = {0};
- vector<int> posNum;
- string pageNum;
- cin>>pageNum;
- double num = string_to_num(pageNum);
- len = pageNum.size();
- if( len <= 1 )
- {
- for(int i = 1; i <= int(num); i++)
- countNum[i] += 1;
- }
- else{
- const char* p = pageNum.c_str();
- for(int i = 0; i < len; i++)
- {
- int highPosNum = int( *(p+i) ) - 48;
- mm = pow(10.0,len-i-2) * (len-i-1);
- if( mm != 0)
- {
- for(int j = 0; j < highPosNum; j++)
- {
- for(int k = 0; k < 10; k++)
- countNum[k] += mm;
- countNum[j] += pow(10.0,len-i-1);
- }
- if( !posNum.empty() )
- {
- for(int m = 0; m < posNum.size(); m++)
- {
- int tt = posNum.at(m);
- countNum[tt] += ( pow(10.0,len-i-1)*highPosNum );
- }
- }
- posNum.push_back(highPosNum);
- if( i == 0)
- {
- for(int j = 0; j < len-1; j++)
- countNum[0] = countNum[0] - (len-j-1)*pow(10.0,j)*9.0;
- countNum[0] -= len;
- }
- }
- else{
- countNum[0] += 1;
- for(int m = 0; m < posNum.size(); m++)
- {
- int tm = posNum.at(m);
- countNum[tm] += 1;
- }
- for(int n = 1; n <= highPosNum; n++)
- {
- countNum[n] += 1;
- for(int mn = 0; mn < posNum.size(); mn++)
- {
- int tmn = posNum.at(mn);
- countNum[tmn] += 1;
- }
- }
- }
- }
- }
- for(int i = 0; i < 10; i++)
- cout<<countNum[i]<<endl;
- return 0;
- }
数字统计问题
本文介绍了一个C++程序,用于计算从1到任意指定页码的所有数字中每个数字(0-9)出现的次数。程序首先将输入转换为双精度浮点数,然后根据不同长度的数字采用不同的计算策略来统计0到9各数字的出现频率。

被折叠的 条评论
为什么被折叠?



