Collegiate Programming Contest 2020 @ MUST Problem A

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)))
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值