题目传送门
Description
DQS watches JoJo’s Bizarre Adventure: Golden Wind a lot, and her favorite character is Guido Mista.
One day, Mista had a dream. In his dream, he has a revolver (a kind of pistol) with n rounds of bullets. He wants to shoot all his bullets at Diablo, the ultimate boss with the stand King Crimson.
Initially, bullets are without gunpowder, so he needs to fill gunpowder into the bullets. For every bullet, the gunpowder filled in is at least 1 and at most 9. Also, the sum of used gunpowder must be a perfect square number, so that Mista will feel happy. (For specific definition, see the hint.)
To make the plan smooth and himself happy, he needs to shoot the bullets in the smallest lexicographical order. Please output the required shooting plan.
Input
Multiple test cases. Read to the end of the file.
For every line, there’s an integer .
Output
For every test case, output the plan in a line. There’s no space between two digits. See sample output for detail.
Sample Input
1
2
3
Sample Output
1
13
112
Hint
A perfect square number x is a certain integer y’s square. That’s to say, x=y^2.
Source
"科林明伦杯"哈尔滨理工大学第九届程序设计团队赛
题意
给一个数 n ,代表n次操作。每次操作为1~9,求n次操作后,使得得到的数的各个位相加,是一个平方数,求出这个最小的满足条件的数。
题解
- 每次最小1,所以默认n位1
- 从后往前贪心求解即可
AC-Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int main() {
int n;while (cin >> n) {
string str(n, '1');int sum = ceil(sqrt(n)) * ceil(sqrt(n));int dt = sum - n;int p = n - 1;while (dt > 0) {
str[p] = dt >= 8 ? 8 + '1' : dt + '1';dt -= dt >= 8 ? 8 : dt;p--;}cout << str << endl;}
}