Codeforces Round 427 B The name on the board 水题

  题目链接: http://codeforces.com/contest/835/problem/B

  题目描述: 一个数原来的和至少K,再给出一个数问最少改变几位可以得到原来那个数  

  解题思路:  水题

  代码: 

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <stack>
#include <deque>
#include <map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;

const int maxn = 1e6 + 100;
char a[maxn];
int s[maxn];

int main() {
    memset(s, 0, sizeof(s));
    int k;
    scanf( "%d%s", &k, a );
    int len = (int)strlen(a);
    for( int i = 0; i < len; i++ ) {
        s[i] = (int)a[i]-48;
    }
    //    for( int i = 0; i <len; i++ ) {
    //        cout << s[i] << endl;
    //    }
    int sum = 0;
    for( int i = 0; i < len; i++ ) {
        sum += s[i];
//        cout << s[i] << endl;
    }
    //    cout << sum << endl;
    if( sum >= k ) {
        printf( "0
" );
        return 0;
    }
    else {
        sort(s, s+len);
        int ans = 0;
        while( sum < k ) {
//            cout << sum << endl;
            sum += (9-s[ans]);
            ans++;
        }
        cout << ans << endl;

    }
    return 0;
}
View Code

  思考: 一道水题, 不应该写到博客上的......算了都写这么多了, 以后不写这种题了

原文地址:https://www.cnblogs.com/FriskyPuppy/p/7356818.html