zoukankan      html  css  js  c++  java
  • 找“1”的个数

    问题:给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数。

    课上先试数然后总结规律最后编程。

    代码:

     1 #include<iostream>
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     int i;
     7     int N;
     8     int temp;
     9     int num = 0;
    10 
    11     cout << "cin a num :   ";
    12     cin >> N;
    13     for (i = 1; i <= N; i++)
    14     {
    15         temp = i;
    16         while (temp != 0)
    17         {
    18             if (temp % 10 == 1)
    19             {
    20                 num++;
    21             }
    22             temp = temp / 10;
    23         }
    24     }
    25     cout << num << endl;
    26 }

    感想:代码虽短但算法,这种思考不易。且行且思考。。。

  • 相关阅读:
    css圆,背景,img填满等样式
    MySQL双日志
    MySQL分层和查询数据的流程
    ZJNU 2345
    ZJNU 2342
    ZJNU 2340/2341/2343
    ZJNU 2235
    ZJNU 2226
    ZJNU 2212
    ZJNU 2208
  • 原文地址:https://www.cnblogs.com/mtant/p/4474397.html
Copyright © 2011-2022 走看看