第一题数位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;
}