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

P11229 [CSP-J 2024] 小木棍

题意

现在小 S 希望拼出一个正整数,满足如下条件:
· 拼出这个数恰好使用 n 根小木棍;
· 拼出的数没有前导 0;
· 在满足以上两个条件的前提下,这个数尽可能小。

思路

一步一步想

测试点1,2暴力

由于大家都能想到就不细细道来

特殊性质

这道题的特殊性这非常有用,可以帮我们想正解(60pts也可以拿一等了)

image
先来看特殊性质A,用一年级的小棒摆一摆,会发现位数尽量小的有益度大于全部写1.
那么我们可以用8来填充每个位置。
再来看特殊性质B,会发现一个神奇的规律,我们可以先保证8最多,然后剩下的手动切为最小
这可是一到找规律的好题,注意余数为3时有一个特判

小心卡常60pts(虽然我也不知道怎么TLE)

#include<bits/stdc++.h>
using namespace std;
int d[]={-1,-1,1,7,4,3,6,8,10};
void f(long long n){
	if(n<=8) {
		cout<<d[n]<<'\n';
		return ;
	}
	else if(n%7==0){
		while(n){
			cout<<8;
			n-=7;
		}
		cout<<'\n';
		return ;
	} 
	else if(n%7==1){
		cout<<10;
		n-=8;
		while(n){
			cout<<8;
			n-=7;
		}
		cout<<'\n';
		return ;
	} 
	else if(n%7==2){
		cout<<18;
		n-=9;
		while(n){
			cout<<8;
			n-=7;
		}
		cout<<'\n';
		return ;
	}
	else if(n%7==3){
		cout<<22;
		n-=9;
		while(n){
			cout<<8;
			n-=7;
		}
		cout<<'\n';
		return ;
	} 
}
int main(){
	int t;
	cin>>t;
	while(t--){
		long long n;cin>>n;
		f(n);
	}
}

代码100pts

#include<bits/stdc++.h>
using namespace std;
const int f[10]= {0,-1,1,7,4,2,6,8,10};
int x,n,d,b;
int main() {
	cin>>x;
	for(int i=x; i; i--) {
		b++;
		if(b>x) return 0;
		cin>>n;
		if(n<=8)cout<<f[n];
		else {
			d=n%7;
			if(d==0) {
				for(int j=1; j<=n/7; j++)cout<<8;
			}
			if(d==1) {
				cout<<10;
				for(int j=1; j<n/7; j++)cout<<8;
			}
			if(d==2) {
				cout<<1;
				for(int j=1; j<=n/7; j++)cout<<8;
			}
			if(d==3) {
				if(n==10)cout<<22;
				else {
					cout<<200;
					for(int j=1; j<=n/7-2; j++)cout<<8;
				}
			}
			if(d==4) {
				cout<<20;
				for(int j=1; j<n/7; j++)cout<<8;
			}
			if(d==5) {
				cout<<2;
				for(int j=1; j<=n/7; j++)cout<<8;
			}
			if(d==6) {
				cout<<6;
				for(int j=1; j<=n/7; j++)cout<<8;
			}
		}
		cout<<'\n';
	}
}

感谢大家阅读

300分拿捏

posted @ 2025-10-11 21:48  爱做牢笼的老龙  阅读(35)  评论(0)    收藏  举报