CodeForces - 1291A Even But Not Even

题意

再给定的串中找到一个子串,符合:
1.对应的数字不能被二整除;
2.各位之和能被二整除;

思路

符合条件的数最后一位必然是奇数才能不被二整除;
除了最后一位必然还有一个奇数各位之和才能被二整除;
所以可以找到串中两个奇数按顺序输出就可以了;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    string s;
    int t;
    while(cin>>t)
    {
        while(t--)
        {
            int n;
            long long sum=0;
            int a=-1,b=-1;
            cin>>n;
            cin>>s;
            for(int i=0;i<n;i++)
            {
                if(a==-1)
                {
                    if((s[i]-'0')%2==1)
                    {
                        a=s[i]-'0';
                    }
                }
                else if(b==-1)
                {
                    if((s[i]-'0')%2==1)
                    {
                        b=s[i]-'0';
                        break;
                    }
                }
            }
            if(b!=-1)
            {
                cout<<a<<b<<endl;
            }
            else
            {
                cout<<-1<<endl;
            }
        }
    }
    return 0;
}

发布了19 篇原创文章 · 获赞 19 · 访问量 676

猜你喜欢

转载自blog.csdn.net/qq_44086097/article/details/104151140