本帖最后由 bin554385863 于 2019-6-28 16:58 编辑 #include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX 1024
int main()
{
int length, aver;
int i, j, flag, temp;
char str[MAX], str2[MAX];
printf("请输入字符串:\n");
scanf("%s", str);
printf("\n");
length = strlen(str);
for (i = 0; i < MAX; i++)
{
if (length == i * i)
{
flag = 1;//判断是否平方数
break;
}
else
{
flag = 0;
}
}
switch (flag)
{
case 1:
aver = sqrt(length);
for (i = 0; i < aver; i++)
{
for (j = 0; j < aver; j++)
{
printf("%c ", str[i * aver + j]);
}
printf("\n");
}
break;
case 0:
aver = sqrt(length) + 1;
temp = aver * aver;
for (i = 0; i < length; i++)
{
str2[i] = str[i];
}
for (j = length; j < temp; j++)
{
str2[j] = '?';//不够平方数用?填充
}
for (i = 0; i < aver; i++)
{
for (j = 0; j < aver; j++)
{
printf("%c ", str2[i * aver + j]);
}
printf("\n");
}
break;
}
return 0;
}
--------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-wngnwkil.r4o --stdout=Microsoft-MIEngine-Out-fx2ompbt.bl4 --stderr=Microsoft-MIEngine-Error-vk4hiwfc.cpb --pid=Microsoft-MIEngine-Pid-4pjt3kot.mzc --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
请输入字符串:
123asd456b
1 2 3 a
s d 4 5
6 b ? ?
? ? ? ?
E:\Administrator\Documents\My C> |