ZZULIOJ1139 (C language implementation)

insert image description hereProblem-solving idea: My idea is to store the string in a two-dimensional array first, and then traverse the length of the string after storing it. If there is a string shorter than the previous one, record the subscript position. Finally, it is output in the form of a string, and it is clear to see the source code.
Source code:
int main()
{ int n = 0; scanf_s(“%d”, &n);//Number of input strings char arr[1001][1001] = { 0 };


int i = 0;
int min = 99999;
int k = 0;
getchar();//这里要把输入n之后的空格给吸收了,不然gets会将它看做一次输入
for (i = 0; i < n; i++)//i来控制输入几行字符串
{
	gets_s(arr[i]);//输入字符串
	int j = 0;//用arr[i][j]来遍历输入的字符串的长度
	int count = 0;//count记录字符串的长度
	
	while (arr[i][j])
	{
		
		count++;
			j++;

		}
	if (count < min)//如果有比count小的字符串,将count赋值给min,并且记录下标i
	{
		min = count;
		k = i;
	}
	

}

printf("%s", arr[k]);//此时k中存放的就是最短的字符串


return 0;
}

Guess you like

Origin blog.csdn.net/Kirihara_Yukiho/article/details/124141138