AT_arc123_c [ARC123C] 1, 2, 3 - Decomposition
Shapy_UI_Tools · · 题解
题意
给你一个数 1 2 3 的数加起来得到
解法
思路
我们考虑一位一位地凑,设
以下"第
实现
这个时候我们就可以写出很优美的代码了。用五个函数分别判断五种情况,判断
代码
提交记录
#include <iostream>
using namespace std;
int T;
long long a;
bool one(long long x) {
while (x != 0) {
if (x % 10 < 1 || x % 10 > 3) return false;
x /= 10;
}
return true;
}
bool two(long long x) {
while (x != 0) {
if (one(x)) return true;
if (x % 10 < 2 || x % 10 > 6) return false;
x /= 10;
}
return true;
}
bool three(long long x) {
while (x != 0) {
if (two(x)) return true;
if (x % 10 < 3) return false;
x /= 10;
}
return true;
}
bool four(long long x) {
bool f = false;//是否从上一位进位过来
while (x != 0) {
if (three(x - f)) return true;
if (x % 10 <= 2) f = true;
else if (x % 10 == 3) {
if (f == 0) return false;
}
else if (x % 10 == 4 && f) return false;
else f = false;
x /= 10;
}
return true;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%lld", &a);
if (one(a)) printf("1\n");
else if (two(a)) printf("2\n");
else if (three(a)) printf("3\n");
else if (four(a)) printf("4\n");
else printf("5\n");
}
}