LLLYYY的数字思维
生活随笔
收集整理的這篇文章主要介紹了
LLLYYY的数字思维
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://ac.nowcoder.com/acm/contest/318/G
題解:本題考驗的是簡單的貪心思維。通過題目我們可以了解到要使得 f (x)盡可能的大,就是要使得x 中的9盡可能的多。所以對于本題,我們可以考慮構造出一個最大的a,使得這個a滿足它的每一位都是9且a<=c,接著我們再令b = c - a,將a 和 b 分別代入 f (x)中,求得最后的答案即可。
C++版本一
/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUG #define RI register int using namespace std; typedef long long ll; typedef __int128 lll; const int N=10000; const int MOD=1e9+7; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; ll t,n,m,k,q; int f(ll x){int tmp = 0;while(x != 0){tmp += x % 10;x /= 10;}return tmp; } int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endifwhile(~scanf("%lld",&n)){ll i=9;for(;i<=n;i=i*10+9);cout << f(i/10)+f(n-i/10) << endl;}//cout << "Hello world!" << endl;return 0; }C++版本二
#include<bits/stdc++.h> #define ll long long using namespace std;int main() {ll n, a, b;while(cin >> n) {ll m = n, k = 9;while(m >= k) {k = k * 10 + 9;}k /= 10;a = k;b = n - k;ll sum = 0;while(a) {sum += a % 10;a /= 10;}while(b) {sum += b % 10;b /= 10;}cout << sum << endl;}return 0; }?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的LLLYYY的数字思维的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于我转生变成史莱姆这档事
- 下一篇: 王者荣耀