1. 题目链接
链接: 题目链接
2. 解答
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>bool issquare(int n)
{if (n == 1 || n == 4) return true;if (n == 2 || n == 3) return false;for (int i = 3; i < n/2; i++ ) {if (n == i*i) return true;}return false;}int solve(int num)
{int *num_square = malloc(sizeof(int) * num);int square_len = 0;int count = 0;for (int i = 0; i < num; i++) {//计算所有完全平方数,完全平方数从小到大排序if (issquare(i) == true) {num_square[square_len] = i;square_len++;printf("squre[%d]=%d, ", square_len, num_square[square_len]);}}printf("\n");//循环从大向小循环,因为找最少的和for (int i = square_len - 1; i >= 0;) {//如果输入与完全平方数相同,那么结束循环,并记录此数据if (num == num_square[i]) {count++;printf("%d ", num_square[i]);break;} else if (num > num_square[i]) { //如果输入大于完全平方数,记录完全平方数,继续循环num = num - num_square[i];count++;printf("%d ", num_square[i]);} else {//如果输入小于完全平方数,那么找更小的完全平方数i--;}}free(num_square);printf("\n");return count;
}int main()
{int num = 0;scanf("%d", &num);printf("input is:%d\n", num);int result = solve(num);printf("result:%d\n", result);return 0;}