Problem
https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/count-digits-1/
Solution
https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/count-digits-1/
Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
#include <string> | |
using namespace std; | |
int main(int argc, char** argv) { | |
string s; | |
cin>>s; | |
int i=0; | |
int c[10]={0,0,0,0,0,0,0,0,0}; | |
while(s[i]!='\0') | |
{ | |
switch(s[i]) | |
{ | |
case 48:c[0]++;i++;break; | |
case 49:c[1]++;i++;break; | |
case 50:c[2]++;i++;break; | |
case 51:c[3]++;i++;break; | |
case 52:c[4]++;i++;break; | |
case 53:c[5]++;i++;break; | |
case 54:c[6]++;i++;break; | |
case 55:c[7]++;i++;break; | |
case 56:c[8]++;i++;break; | |
case 57:c[9]++;i++;break; | |
} | |
} | |
// print count of each digit | |
for(int j=0;j<=9;j++) | |
{ | |
cout<<j<<" "<<c[j]<<endl; | |
} return 0; | |
} |
No comments:
Post a Comment