跳到正文

个人记录

CSP-J 2024第二轮T2题解

duzedesenpai约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年10月4日
收录
2026年10月4日
更新
2026年11月1日
篇幅
约 3 分钟

版本

  1. v2当前
  2. v1

这是一篇我在复盘的时候做出的题解。

今天早上有一次集训,第一天还是这一题,我先DFS做这题,然后再推测出枚举第一位,然后先尽量输出0,再尽量输出8。以下是代码:

C++
#include <cstdio>
#include <string>

using namespace std; //C with STL 
const int ccf = 0;

int feibafenqianganfeima[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int min(int a, int b){
	return a>b ? b : a;
}
bool xiaoyu(string a, string b){
	if(a.length() == b.length()) return a < b;
	return a.length() < b.length(); //a 是否小于 b ? 
}

void gandefeimaxiaohaha(int n, FILE *out){
	if(n == 1){
		fprintf(out, "-1");
		return;
	}
	if(n <= 7){
		switch(n){
			case 1:
				fprintf(out, "-1");
				break;
			case 2:
				fprintf(out, "1");
				break;
			case 3:
				fprintf(out, "7");
				break;
			case 4:
				fprintf(out, "4");
				break;
			case 5:
				fprintf(out, "2");
				break;
			case 6:
				fprintf(out, "6");
				break;
			case 7:
				fprintf(out, "8");
				break;
		}
		return;
	}
	if(n == 10){
		fprintf(out, "22");
		return;
	}
	int head; //首位 
	int shengyu;
	string ans="", t;
	for(int i = 1; i <= 9; ++i){
		head = i;
		t = "";
		t += char(head+'0');
		shengyu = n-feibafenqianganfeima[head];
		while(shengyu > 0){
			if(shengyu % 7){
				shengyu -= 6;
				t += '0';
			}
			else{
				shengyu -= 7;
				t += '8';
			}
		}
		if(ans == ""){
			ans = t;
		}
		else{
			if(xiaoyu(t, ans)){
				ans = t;
			}
		}
	}
	fprintf(out, "%s", ans.c_str());
}

int main(){
	FILE * in = stdin;
	FILE * out = stdout;
	int t;
	fscanf(in, "%d", &t);
	int x;
	--t;
	while(t--){
		fscanf(in, "%d", &x);
		gandefeimaxiaohaha(x, out);
		fprintf(out, "\n"); 
	} 
	fscanf(in, "%d", &x);
	gandefeimaxiaohaha(x, out);
	fclose(in);
	fclose(out);
}

EOF

原文