2019-07-19  2024-09-15    529 字  2 分钟

Codeforces-C1157 A. Reachable Numbers

题意为按照 $_math_inline$f(x)$math_inline_$ 对某个数进行操作,在这个过程中一共会出现多少种不同的数字。

  1. 不管 n 为多少,必定可以出现 1-9 中的任意一个数字
  2. 如果 n 大于9,则需要把结果 +1 (n本身也算一次)
  3. 只统计 +1 并去掉末尾 0 之后的数字有几个

例如 n 为 121 :

  1. 那么显然 121 算一次
  2. 由于 +1 的缘故 121-129 都会出现,数量为 9 - 121 % 10 = 8
  3. 12-19 出现了 9 - 12 % 10 = 7
  4. 1-9 都可以出现
  5. 1 + 8 + 7 + 9 = 25
Code
  1/**
  2 *    author: Akvicor
  3 *    created: 2019-07-19 14-33-13
  4**/
  5
  6#include <bits/stdc++.h>
  7
  8using namespace std;
  9
 10#ifdef DEBUG
 11string to_string(string s) {
 12	return '"' + s + '"';
 13}
 14
 15string to_string(const char* s) {
 16	return to_string((string) s);
 17}
 18
 19string to_string(bool b) {
 20return (b ? "true" : "false");
 21}
 22
 23template <typename A, typename B>
 24string to_string(pair<A, B> p) {
 25	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
 26}
 27
 28template <typename A>
 29string to_string(A v) {
 30	bool first = true;
 31	string res = "{";
 32	for (const auto &x : v) {
 33		if (!first) {
 34			res += ", ";
 35		}
 36		first = false;
 37		res += to_string(x);
 38	}
 39	res += "}";
 40	return res;
 41}
 42
 43void debug_out() { cerr << endl; }
 44
 45template <typename Head, typename... Tail>
 46void debug_out(Head H, Tail... T) {
 47	cerr << " " << to_string(H);
 48	debug_out(T...);
 49}
 50#endif
 51
 52#ifdef DEBUG
 53#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
 54#else
 55#define debug(...) 17
 56#endif
 57
 58#ifdef DEBUG
 59#define FAST_IO 17
 60#else
 61#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
 62#define endl '\n'
 63#endif
 64
 65#define LL long long
 66#define ULL unsigned long long
 67#define rep(i, n) for(int i = 0; i < (n); ++i)
 68#define reep(i, n) for(int i = 0; i <= (n); ++i)
 69#define lop(i, a, n) for(int i = a; i < (n); ++i)
 70#define loop(i, a, n) for(int i = a; i <= (n); ++i)
 71#define ALL(v) (v).begin(), (v).end()
 72#define PB push_back
 73#define VI vector<int>
 74#define PII pair<int,int>
 75#define FI first
 76#define SE second
 77#define SZ(x) ((int)(x).size())
 78
 79const double EPS = 1e-6;
 80const double PI = acos(-1.0);
 81const int INF = 0x3f3f3f3f;
 82const LL LINF = 0x7f7f7f7f7f7f7f7f;
 83const int MAXN = (int)1e6 + 10;
 84const int MOD = (int)1e9 + 7;
 85
 86int n;
 87
 88int main(){
 89	FAST_IO;
 90
 91	while(cin >> n){
 92		int cnt = 9 + (n > 9);
 93		while(n > 9){
 94			cnt += 9 - (n % 10);
 95			n /= 10;
 96		}
 97		cout << cnt << endl;
 98	}
 99
100	return 0;
101}

除另有声明外本博客文章均采用 知识共享 (Creative Commons) 署名 4.0 国际许可协议 进行许可转载请注明原作者与文章出处