L1-003 個位數統計 (15分)
請撰寫程式統計每種不同的個位數字出現的次數,例如:給定 N=100311,則有 2 個 0,3 個 1,和 1 個 3,
輸入格式:
每個輸入包含 1 個測驗用例,即一個不超過 1000 位的正整數 N,
輸出格式:
對 N 中每一種不同的個位數字,以 D:M 的格式在一行中輸出該位數字 D 及其在 N 中出現的次數 M,要求按 D 的升序輸出,
輸入樣例:
100311
輸出樣例:
0:2
1:3
3:1
有以下兩種的思路,僅供參考
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
string str;
int ans[15];
int main(){
char ch;
while((ch = getchar())!='\n')
ans[ch-'0']++;
for(int i = 0; i < 10; i ++)
if(ans[i] != 0)
cout << i << ":" <<ans[i] << endl;
return 0;
}
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
string str;
int ans[15];
int main(){
getline(cin, str);
for(int i = 0; i < str.length(); i ++)
ans[str[i]-'0']++;
for(int i = 0; i < 10; i ++)
if(ans[i] != 0)
cout << i << ":" <<ans[i] << endl;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/157521.html
標籤:其他
上一篇:CocosCreator技能冷卻效果 超方便哦 只需復制粘貼
下一篇:前端基礎演算法題解法
