6-2 统计专业人数 分数 15 全屏浏览题目 切换布局 作者 张泳 单位 浙大城市学院 本题要求实现一个函数,统计学生学号链表中专业为计算机的学生人数。链表结点定义如下: struct ListNode { char code[8]; struct ListNode *next; }; 这里学生的学号共7位数字,其中第2、3位是专业编号。计算机专业的编号为02。 函数接口定义: int countcs( struct ListNode *head ); 其中head是用户传入的学生学号链表的头指针;函数countcs统计并返回head链表中专业为计算机的学生人数。 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> #include <string.h> struct ListNode { char code[8]; struct ListNode *next; }; struct ListNode *createlist(); /*裁判实现,细节不表*/ int countcs( struct ListNode *head ); int main() { struct ListNode *head; head = createlist(); printf("%d\n", countcs(head)); return 0; } /* 你的代码将被嵌在这里 */ 输入样例: 1021202 2022310 8102134 1030912 3110203 4021205 # 输出样例: 3 代码长度限制 16 KB 时间限制 400 ms 内存限制
时间: 2024-03-14 11:45:30 浏览: 1763
下面是我给出的代码实现:
```c
int countcs(struct ListNode *head) {
int count = 0;
while (head != NULL) {
if (head->code[1] == '0' && head->code[2] == '2') {
count++;
}
head = head->next;
}
return count;
}
```
这个函数的实现很简单,只需要遍历整个链表,对于每个结点,检查它的学号中第2和第3位是否为 "02",如果是的话,累加计数器即可。最后返回计数器的值即可。
相关问题
6-1 计算两个复数之积 分数 15 全屏浏览题目 切换布局 作者 张泳 单位 浙大城市学院 本题要求实现一个计算复数之积的简单函数。 函数接口定义: struct complex multiply(struct complex x, struct complex y);
以下是一个实现计算复数之积的函数的示例代码:
```c
#include <stdio.h>
struct complex {
double real; // 复数的实部
double imag; // 复数的虚部
};
// 计算两个复数之积
struct complex multiply(struct complex x, struct complex y) {
struct complex z;
z.real = x.real * y.real - x.imag * y.imag;
z.imag = x.real * y.imag + x.imag * y.real;
return z;
}
int main() {
struct complex x = {1.0, 2.0};
struct complex y = {3.0, 4.0};
struct complex z = multiply(x, y);
printf("(%g + %gi) * (%g + %gi) = %g + %gi\n", x.real, x.imag, y.real, y.imag, z.real, z.imag);
return 0;
}
```
在这个示例代码中,我们定义了一个 `struct complex` 结构体表示复数,包含实部和虚部两个成员变量。我们还定义了一个 `multiply` 函数,用于计算两个复数之积。在 `multiply` 函数中,我们使用了结构体变量的成员运算符 `.` 来访问结构体的成员变量。最后在 `main` 函数中,我们定义了两个复数 x 和 y,并调用 `multiply` 函数计算它们的积,然后输出结果。
运行上述代码,输出结果为:
```
(1 + 2i) * (3 + 4i) = -5 + 10i
```
这表示计算复数 `(1 + 2i)` 和 `(3 + 4i)` 的积,结果为复数 `(-5 + 10i)`。
7-13 找最长的字符串 分数 15 全屏浏览 切换布局 作者 张泳 单位 浙大城市学院 本题要求编写程序,针对输入的N个字符串,输出其中最长的字符串。
在编程挑战题目中,给定一个包含N个字符串的列表,目标是找到并输出其中长度最长的那个字符串。这通常是一个简单的字符串操作任务,可以使用多种编程语言来解决,以下是基本的步骤:
1. **初始化变量**:首先创建一个变量,比如`max_length`用于存储当前已知的最长字符串长度,`longest_str`存储对应的最长字符串。
2. **遍历字符串列表**:通过一个循环遍历每个输入的字符串。对于每个字符串,获取其长度并与`max_length`比较。
3. **更新记录**:如果当前字符串的长度大于`max_length`,则将`max_length`设为该长度,并将该字符串赋值给`longest_str`。
4. **结束遍历后返回结果**:当所有字符串都检查过之后,`longest_str`应该包含了最长的字符串。
下面是一个简化的Python示例代码:
```python
def find_longest_string(strings):
max_length = 0
longest_str = ""
for string in strings:
if len(string) > max_length:
max_length = len(string)
longest_str = string
return longest_str
# 示例:
strings = ["hello", "world", "this is a long string"]
result = find_longest_string(strings)
print(result) # 输出 "this is a long string"
```
阅读全文
相关推荐
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)