P11229 [CSP-J 2024] 小木棍 题解

· · 题解

萌新第一次发题解

因在考场上未注释freopen痛失100分

我们可以列打个表

数字 0 1 2 3 4 5 6 7 8 9
小木棍的支数 6 2 5 5 4 5 6 3 7 6

这段代码是用来打表的

#include <bits/stdc++.h>
using namespace std;
int a[10]={6,2,5,5,4,5,6,3,7,6};
int main()
{
    int f,sum;
    bool flag;
    for(int i=1;i<50;i++)
    {
        for(long long j=1;j<=10000000000;j++)
        {
            f=j;
            sum=0;
            while(f)
            {
                sum+=a[f%10];
                f/=10;
            }
            if(sum==i)
            {
                flag=1;
                cout<<j<<",";
                break;
            }
        }
        if(!flag)cout<<-1<<",";
    }
    return 0;
}

输出

-1,1,7,4,2,

6,8,10,18,22,20,28,

68,88,108,188,200,208,288,

688,888,1088,1888,2008,2088,2888,

6888,8888,10888,18888,20088,20888,28888,

68888,88888,108888,188888,200888,208888,288888,

688888,888888,1088888,1888888,2008888,2088888,2888888,

6888888,8888888,

我们可以发现除了1~5, 6~12其他都是有迹可循的。

1~5时只用打个表啊a[5]={-1,1,7,4,2};

6~13 b[7]={6,8,10,18,22,20,28};

剩下的也是一个表:

设输入为x;
c[7]={68,88,108,188,200,208,288};
cout<<c[(x-13)%7];
for(int i=1;i<=(x-13);i++)
{
  cout<<8;
}
cout<<endl;

代码演示

#include <bits/stdc++.h>
using namespace std;
int a[5] = { -1, 1, 7, 4, 2};
int b[7] = {6, 8, 10, 18, 22, 20, 28};
int c[7] = {68, 88, 108, 188, 200, 208, 288};
int main() {
    int n, x;
    cin >> n;
    while (n--) {
        cin >> x;
        if(x==1)
        {
            cout<<-1<<endl;
        }
        if (x>1&&x < 6) {
            cout << a[x - 1] << endl;
        }
        if (x > 5 && x < 13) {
            cout << b[x - 6] << endl;
        }
        if (x > 12) {
            cout << c[(x - 13) % 7];
            for (int i = 1; i <= (x - 13)/7; i++) {
                cout << 8;
            }
            cout << endl;
        }
    }
    return 0;
}