zoukankan      html  css  js  c++  java
  • c语言中数字字符计数

    c语言中数字字符计数

    1、

    #include <stdio.h>
    
    int main(void)
    {
        int i, ch;
        int cnt[10] = {};
        
        while((ch = getchar()) != EOF)
        {
            switch(ch)
            {
                case '0': cnt[0]++; break;
                case '1': cnt[1]++; break;
                case '2': cnt[2]++; break;
                case '3': cnt[3]++; break;
                case '4': cnt[4]++; break;
                case '5': cnt[5]++; break;
                case '6': cnt[6]++; break;
                case '7': cnt[7]++; break;
                case '8': cnt[8]++; break;
                case '9': cnt[9]++; break;
            }
        }
        
        puts("show the times.");
        for(i = 0; i < 10; i++)
        {
            printf("'%d' = %d
    ", i, cnt[i]);
        }
        
        return 0;
    }

     

    2、

    #include <stdio.h>
    
    int main(void)
    {
        int i, ch;
        int cnt[10] = {};
        
        while((ch = getchar()) != EOF)
        {
            switch(ch)
            {
                case 48: cnt[0]++; break;
                case 49: cnt[1]++; break;
                case 50: cnt[2]++; break;
                case 51: cnt[3]++; break;
                case 52: cnt[4]++; break;
                case 53: cnt[5]++; break;
                case 54: cnt[6]++; break;
                case 55: cnt[7]++; break;
                case 56: cnt[8]++; break;
                case 57: cnt[9]++; break;
            }
        }
        
        puts("show the times.");
        for(i = 0; i < 10; i++)
        {
            printf("'%d' = %d
    ", i, cnt[i]);
        }
        
        return 0;
    }

    3、

    #include <stdio.h>
    
    int main(void)
    {
        int i, ch;
        int cnt[10] = {};
        
        while((ch = getchar()) != EOF)
        {
            if(ch >= 48 && ch <= 57)
            {
                cnt[ch - 48]++;
            }
        }
        
        puts("show the times.");
        for(i = 0; i < 10; i++)
        {
            printf("'%d' = %d
    ", i, cnt[i]);
        }
        
        return 0;
    }

    4、

    #include <stdio.h>
    
    int main(void)
    {
        int i, ch;
        int cnt[10] = {};
        
        while((ch = getchar()) != EOF)
        {
            if(ch > '0' && ch < '9')
            {
                cnt[ch - '0']++;
            }
        }
        
        puts("show the times.");
        for(i = 0; i < 10; i++)
        {
            printf("'%d' = %d
    ", i, cnt[i]); 
        }
        
        return 0;
    }

  • 相关阅读:
    眼手组合-眼低手低者
    MongoDB、redis、memcached
    CAS单点登录配置 .
    Static attribute must be a String literal
    myeclipse 导包快捷键
    java生成json总结
    weblogic部署错误:weblogic.xml.jaxp.RegistrySAXParserFactory cannot be cast to javax.xml.parsers.SAXParserFactory
    xfire+spring webservice
    java中获取当前路径(转)
    MyEclipse使用指南(精简版) (转)
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14803469.html
Copyright © 2011-2022 走看看