跳到正文

个人记录

G - German Conference for Public Counting

ytck约 2 分钟
原文
文章信息与版本

文章信息

发布
2023年8月4日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 2 分钟

版本

  1. v1当前

找需要的数字个数

输入整数n,其长度为len

如果n长度为1时直接输出n+1

否则判断长度为len所有位次全是n最高位的整数tmp(如n==897就是tmp==888)

如果n<tmp

(map<int,int> ma存的时我们每个数字用的几次)

如果n>=tmp

最后遍历输出

C++
#include"bits/stdc++.h"
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define endl '\n'
map<int, int> ma;

int calc(int len, int first)
{
	//cout << len << ' ' << first << endl;
	int ans = 0;
	for (int i = 0; i < len; i++) ans = ans * 10 + first;
	return ans;
}

signed main()
{
	int n;
	cin >> n;
	int len = log10(n) + 1;
	if (len == 1) return cout << n + 1 << endl, 0;
	//cout << len << endl;

	int first = to_string(n)[0] - '0';

	int tmp = calc(len, first);

	//cout << tmp << endl;

	if (n < tmp)
	{
		for (int i = 1; i < first; i++) ma[i] = len;
		for (int i = first; i <= 9; i++) ma[i] = len - 1;
		ma[0] = len - 1;
	}
	else
	{
		for (int i = 1; i <= first; i++) ma[i] = len;
		for (int i = first + 1; i <= 9; i++) ma[i] = len - 1;
		ma[0] = len - 1;
	}

	int ans = 0;
	for (auto item : ma) ans += item.second;
	cout << ans;
}
原文