牛客小白月赛79

第一题数位dp

题目如下:

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

思路 :

我是在输入时变成了2个方向的一个是数字本身的,一个数字变成字符串,变成字符串好去看这个数字的长度,第一种情况 数子长度为1,且为偶数,所以不需要操作次数

第二种情况数字长度为一且为奇数,操作次数唯一

剩下就得挨个判断了、

代码如下:

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
    int n;
    cin>>n;
    string s=to_string(n);
    int i;
    int count=0;
    int c=0;
    if(s.length()==1 && n%2==0){
        cout<<0<<endl;
    }
    else if(s.length()==1 && n%2==1){
        cout<<1<<endl;
    }
    else {
        if(s[0]!='0' && n%2==0){
            cout<<0;
        }
       
        
        else{
            for(i=0;i<s.length();i++){
               if((s[i]-'0')%2==0 && s[i]!='0') count++;
           }
            if(count==0){
                cout<<s.length();
            }
            else{
             int arr[count];
             for(i=0;i<s.length();i++){
               if((s[i]-'0')%2==0 && s[i]!='0') {
                   arr[c]=s.length()-i-1;
                   c++;
               }
            }
            sort(arr,arr+count);
              cout<<arr[0];  
            }
            

            
        }
    }
}

 代码如下

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> Q;
int cmp(int a,int b)
{
    return a > b;
}
int main()
{
    int n;
    cin >> n;
    long long ans = 0;
    while(n -- )
    {
        int num;
        cin >> num;
        if(num & 1)Q.push_back(num);
        else ans += num;
    }
    sort(Q.begin(),Q.end(),cmp);
    int res = Q.size() - 1;
    if(Q.size() & 1)res -- ;
    for(int i = 0;i <= res;i ++ )ans += Q[i];
    cout << ans << endl;
    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值