题解:P13729 【MGVOI R1-A】超级奇数(odd)
P13729 超级奇数题解
题目描述
原题链接\
简单来说就是给定一个数
(赛时)解题思路
由于这是 T1 ,所以我以为这是签到题。但是不然,一看数据范围
#include <bits/stdc++.h>
using namespace std;
long long T, a;
bool check(int k) {
while(k) {
if(k % 10 % 2 == 0) return false;
k /= 10;
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> T;
while(T--) {
cin >> a;
for(long long i = a; ; i++) {
if(check(i)) {
cout << i - a << endl;
break;
}
}
}
return 0;
}
跑去做 T2 ,但是发现还是想不出正解, 所以又跑回来做 T1 。
注意到数字很大,但是数字的位数很小。于是有下面的猜想:我们要求
#include <bits/stdc++.h>
#define inc(i, l, r) for(int i = l; i <= r; i++)
#define dec(i, l, r) for(int i = l; i >= r; i--)
using namespace std;
const int maxn = 20;
long long T;
string a;
bool vis[maxn];
long long tonum(string s) {
long long res = 0;
inc(i, 0, s.length() - 1) {
res *= 10;
res += s[i] - '0';
}
return res;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> T;
while(T--) {
memset(vis, 0, sizeof vis);
cin >> a;
string ans, ans1;//存储第一次操作的数和微调的数
inc(i, 0, a.length() - 1) {
if((a[i] - '0') % 2 == 0 ){ //这是个偶数,不符合要求!
ans += (char)a[i] + 1; //变成奇数吧!
vis[i] = true;//打上标记,方便找最后一个位置!
} else {
ans += a[i];//奇数照抄
}
}
int cnt = 0x3ffffff;//因为可能 a 本身就是超级奇数,所以设置极大值
inc(i, 0, a.length() - 1) if(vis[i] == true) {cnt = i; break;}//找最后一个偶数位
inc(i, cnt + 1, ans.length() - 1) ans[i] = '1';//剩下变1吧!因为1是最小的奇数
cout << tonum(ans) - tonum(a) << endl;//字符转数字
}
return 0;
}
代码细节(可以跳过)
详见注释!因为字符串操作会让思路变清晰(我老师告诉我的)所以采用它实现! 完结撒花!