UVA 1225 Digit Counting
Answer key by ZhangXiubo
Personal blog : coldtea.tech
Key Point :
String Processing
C++ STL
Array
Use string method
string s;
### to_string()
//#include<string>
//function : converte number to string
//parameter:value
//return :converted string
//std::string to_string(int value); (1) (C++11
//std::string to_string(long value); (2) (C++11
//std::string to_string(long long value); (3) (C++11
//std::string to_string(unsigned value); (4) (C++11
//std::string to_string(unsigned long value); (5) (C++11
//std::string to_string(unsigned long long value); (6) (C++11
//std::string to_string(float value); (7) (C++11
//std::string to_string(double value); (8) (C++11
//std::string to_string(long double value); (9) (C++11
//test program:
```cpp
#include <iostream> // std::cout
#include <string> // std::string, std::to_string
using namespace std ;
int main()
{
std::string pi = "pi is " + std::to_string(3.1415926);
std::string perfect = std::to_string(1 + 2 + 4 + 7 + 14) + " is a perfect number";
std::cout << pi << '\n';
std::cout << perfect << '\n';
}
output:
pi is 3.141593
28 is a perfect number
buckets
The first hard point you need to handle is how to converge the number to a char or string.
How to connect two string will also be an important problem.
We will naturally think of a barrel, if you have learned how to sort with buckets.
You first defined 10 buckets from 0 to 10, whenever you encounter a character, naturally let its associated bucket add one.
Finally output the buckets you will solve this problem successfully.
#include<bits/stdc++.h>
using namespace std;
int n = 0;
int t = 0;
string s;
int counter[10];
signed main()
{
cin >> n;
while (n--)
{
cin >> t;
for (int i = 1; i <= t; i++)
{
s += to_string(i);
}
/*cout << s<<endl;*/
for (int i = 0; i < s.length(); i++)
{
/*cout << s[i] - 48 << endl;*/
counter[s[i] - 48]++;
}
for (int i = 0; i <= 8; i++)
{
cout << counter[i] << ' ';
counter[i] = 0;
}
cout << counter[9];
counter[9] = 0;
s = "";
cout << endl;
}
return 0;
}
And another way to solve is to call the count function.
Count function can easily output the number of occurrences of each character.
count()
count is a function in the algorithm library, which uses the help of an iterator to count the number of occurrences of a member in a container.
string mainString = "Let life be beautiful like summer flowers,death like autumn leaves";
int total = count(mainString.begin(), mainString.end(), 'u');
cout << "this char total is " << total << endl;
output: this char total is 5
#include<bits/stdc++.h>
using namespace std;
int n = 0; //enter the number of group
int t = 0;
string s;
signed main()
{
cin >> n;
while (n--)
{
cin >> t;
for (int i = 1; i <= t; i++)
{
s += to_string(i);
}
for (int i = 0; i < 9; i++)
{
cout << count(s.begin(), s.end(),i+48) << ' ';
}
cout << count(s.begin(), s.end(), 9+48);
s = "";
cout << endl;
}
return 0;
}
Use python’s Counter(same reason)
from collections import Counter
m=int(input())
for i in range (m):
n=int(input())
answer=Counter(''.join(str(i) for i in range(1,n+1) ))
print(*(answer[str(i)] for i in range (10)))