zoukankan      html  css  js  c++  java
  • 每日编程-20170308

    题目:从1开始到N个自然数中,0-9每个数字出险次数进行统计。(昨天做的牛客网统一考试题)

    输入:N

    输出要求: 输出10个数字对应的出现次数,以空格为间隔,最后一个数字后面不加空格

    解答:

     1 #include <iostream>
     2 using std::cout; using std::endl; using std::cin;
     3 int main() {
     4     int c[10] = { 0 };  //初始化数组用于存放每个数字出现次数,0开始
     5     int number;
     6     cin >> number;  //输入N
     7     for (auto i = 1; i <= number; i++)  //遍历1到N
     8     {
     9         int j = i;  //设置i的副本,避免改变i
    10         do {
    11             c[j % 10]++;
    12             j /= 10;
    13         } while (j!= 0);  //页数每次取10的余数,然后除10,直到为0
    14     }
    15     for (auto i = 0; i < 10;i++) //遍历数组
    16     {
    17         cout << c[i];  //输出
    18         if (i != 9)
    19         {
    20             cout << " ";  //除了最后一个元素,其他元素后加空格
    21         }
    22     }
    23 }

    改进:

    do while完全没必要,没有页码0

     1 #include <iostream>
     2 using std::cout; using std::endl; using std::cin;
     3 int main() {
     4     int c[10] = { 0 };
     5     int number;
     6     cin >> number;
     7     for (auto i = 1; i <= number; i++)
     8     {
     9         int j = i;
    10         while (j!=0)
    11         {
    12             c[j % 10]++;
    13             j /= 10;
    14         }
    15     }
    16     for (auto i = 0; i < 10;i++)
    17     {
    18         cout << c[i];
    19         if (i != 9)
    20         {
    21             cout << " ";
    22         }
    23     }
    24 } 
  • 相关阅读:
    jquery的全选,全不选,反选
    jquery中的on方法绑定动态元素
    IIS服务器不能下载.apk文件的解决方略
    14:堆和堆排序
    虚拟内存
    leetcode28:实现strStr()
    leetcode387:字符串中的第一个唯一字符
    leetcode344:反转字符串
    leetcode198:打家劫舍
    leetcode64:最小路径和
  • 原文地址:https://www.cnblogs.com/linhaowei0389/p/6518269.html
Copyright © 2011-2022 走看看