PAT-BASIC-1021-個位元統計

來源:互聯網
上載者:User

標籤:

給定一個k位整數N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0),請編寫程式統計每種不同的個位元字出現的次數。例如:給定N = 100311,則有2個0,3個1,和1個3。

輸入格式:

每個輸入包含1個測試案例,即一個不超過1000位的正整數N。

輸出格式:

對N中每一種不同的個位元字,以D:M的格式在一行中輸出該位元字D及其在N中出現的次數M。要求按D的升序輸出。

輸入範例:

100311

輸出範例:

0:21:33:1

int存不下,用字串存
#include <bits/stdc++.h>#define MAXN 1000+50using namespace std;char str[MAXN];int digit[10];int main(){    memset(digit, 0, sizeof(digit));    scanf("%s", str);    int len = strlen(str);    for(int i = 0; i < len; ++i){        digit[str[i]-‘0‘]++;    }    for(int i = 0; i < 10; ++i){        if(digit[i]){            printf("%d:%d\n", i, digit[i]);        }    }    return 0;  }
CAPOUIS‘CODE

 

PAT-BASIC-1021-個位元統計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.