优化这段代码#include <stdio.h> #include <stdlib.h> int main() { char s[4][50]; int i; for(i=0;i<4;i++) { scanf("%s",&s[i]); } int (*p)[50]; p=&s; int a[4],n; n=0; for(i=0;i<4;i++) { while(*p!='\0') { n++; } a[i]=n; n=0; } int max; max=0; for(i=0;i<4;i++) { if(a[i]>max) { max=a[i]; } } printf("%d",max); return 0; }
时间: 2024-04-09 17:28:02 浏览: 21
这段代码存在一些问题,我将为你进行优化:
```C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char s[4][50];
int i;
for(i = 0; i < 4; i++)
{
scanf("%s", s[i]);
}
int a[4], n;
int max = 0;
for(i = 0; i < 4; i++)
{
n = strlen(s[i]);
a[i] = n;
if(n > max)
{
max = n;
}
}
printf("%d\n", max);
return 0;
}
```
优化说明:
1. 添加 `#include <string.h>` 头文件以使用 `strlen` 函数。
2. 移除 `int (*p)[50];` 和 `p=&s;` 的部分,因为可以直接使用 `strlen` 计算字符串的长度。
3. 将 `while(*p!='\0')` 改为 `n = strlen(s[i]);`,便于计算每个字符串的长度。
4. 将 `max = 0;` 的初始化提前到循环之前。
5. 删除不必要的变量和赋值操作。
这样,代码会更加简洁和可读,并且能够正确计算最长字符串的长度。
相关问题
include <stdio.h>#include <stdlib.h>
这是一个C语言的标准头文件的引用,其中<stdio.h>包含了C语言的输入输出函数,而<stdlib.h>包含了一些通用的工具函数和内存分配函数。这些头文件在C语言中非常常见,几乎每个C程序都会用到它们。下面是一个简单的例子,展示了如何使用这两个头文件。
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
char str[100];
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered %d.\n", num);
printf("Enter a string: ");
scanf("%s", str);
printf("You entered %s.\n", str);
return 0;
}
```
上面的程序使用了<stdio.h>中的printf和scanf函数,以及<stdlib.h>中的exit函数。输入一个整数和一个字符串,然后将它们输出到控制台上。
Hanoi问题#include <stdio.h> #include <stdlib.h>
Hanoi问题是一个经典的递归问题,其目标是将一堆盘子从一个柱子移动到另一个柱子,每次只能移动一个盘子,并且大盘子不能放在小盘子上面。下面是一个C语言的Hanoi问题的递归实现:
```c
#include <stdio.h>
#include <stdlib.h>
void hanoi(int n, char A, char B, char C) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", A, C);
return;
}
hanoi(n-1, A, C, B);
printf("Move disk %d from %c to %c\n", n, A, C);
hanoi(n-1, B, A, C);
}
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
return 0;
}
```
上述代码中,hanoi函数是递归函数,用于将n个盘子从A柱子移动到C柱子,B柱子作为辅助柱子。当n等于1时,直接将盘子从A柱子移动到C柱子。否则,先将n-1个盘子从A柱子移动到B柱子,再将第n个盘子从A柱子移动到C柱子,最后将n-1个盘子从B柱子移动到C柱子。在main函数中,首先输入盘子的数量n,然后调用hanoi函数。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![application/x-gzip](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)