zoukankan      html  css  js  c++  java
  • 页码数求0到9共有多少个

    1.将int转为string  string转int方法

    #include<iostream>
    #include "sstream" //
    #include<string>
    #include <stdio.h>
    
    using namespace std;
    
    int pagecount(int n){
      int num[10] = {0};
      for(int i = 1; i<=n;i++){
        stringstream ss;
        ss << i;
        string s1 = ss.str();
        cout<<endl;
        for (int j = 0;j<s1.length();j++){
          int b;
          stringstream os;
          os << s1[j];
          os >> b;
          cout<<endl;
          num[b] = num[b] + 1;
        }
      }
      for(int i = 0; i < 10;i++)
      cout<<i<<":"<<num[i]<<endl;
    }
    int main(){
    int page;
    cout<<"输入页数:";
    cin>>page;
    pagecount(page);
    }
    

      

    2.取余法

    void pageCount(){
        	int n;
        	int a[10] = {0};
        	cout << "请输入要计算的页码: ";
        	cin >> n;
        	for (int i = 1; i <= n; i++){
            	int k=i;
            	while(k)
            	{	
                	a[k%10]++;
                	k = k/10;
            	}
        	}
    
        	for (int i = 0; i < 10; i++)  cout << i << ": " << a[i] << endl;
    }
    
  • 相关阅读:
    MYSQL定时任务 触发器
    mybatis 学习
    SSM 记录
    环境变量配置
    servlet 拦截器 (filter)
    验证码
    jquery $.ajax({});参数详解
    maven打包忽略静态资源解决办法,dispatchServlet拦截静态资源请求的解决办法
    switch..case..
    HDU 1005 题解
  • 原文地址:https://www.cnblogs.com/ShanCe/p/9664931.html
Copyright © 2011-2022 走看看