#include <stdio.h> #include <stdlib.h> int main() { int freq[26] = {0}; int pos[26] = {0}; int cnt = 0,min = 9999,firstIndex = -1; char ch; while((ch = getchar()) != '\n'){ int off = ch - 'a'; if(pos[off] == 0){ pos[off] = cnt ++; } freq[off] ++; } for(int i = 0; i < 26; i ++){ if(freq[i] == 1){ if(min > pos[i]){ min = pos[i]; firstIndex = i; } } } if(firstIndex != -1) printf("%c\n",'a'+ firstIndex); else printf("no\n"); return 0; }
时间: 2023-12-29 07:06:18 浏览: 32
这段代码的作用是统计用户输入的字符串中第一个不重复的字符,并输出该字符。下面是代码的详细解释:
1. 声明了两个数组 freq 和 pos,分别用于记录每个字符出现的次数和字符第一次出现的位置。
2. 初始化 cnt 为 0,min 为一个较大的数(9999),firstIndex 为 -1,表示还没有找到第一个不重复的字符。
3. 通过 getchar() 逐个读取用户输入的字符,如果该字符尚未出现过,则在 pos 数组中记录该字符第一次出现的位置,并将 cnt 自增 1;同时在 freq 数组中记录该字符出现的次数。
4. 遍历 freq 数组,找到第一个出现次数为 1 的字符。如果该字符在 pos 数组中的位置比 min 小,则更新 min 和 firstIndex。
5. 如果找到了第一个不重复的字符,则输出该字符;否则输出 "no"。
6. 返回 0,程序结束。
注意,该代码只能处理小写字母,如果用户输入了大写字母或其他字符,可能会导致程序出错。
相关问题
#include <stdio.h> #include <stdlib.h> int main() { int freq[26] = { 0 }; int pos[26] = { 0 }; int cnt = 0, min = 9999, firstIndex = -1; char ch; while ((ch = getchar()) != '\n') { int off = ch - 'a'; if (pos[off] == 0) { pos[off] = cnt++; } freq[off] ++; } for (int i = 0; i < 26; i++) { if (freq[i] == 1) { if (min > pos[i]) { min = pos[i]; firstIndex = i; } } } if (firstIndex != -1) printf("%c\n", 'a' + firstIndex); else printf("no\n"); return 0; }详细解释
这是一个 C 语言程序,用于输入一个字符串并找出字符串中第一个出现且仅出现一次的小写字母。以下是程序的逐行解释:
```c
#include <stdio.h>
#include <stdlib.h>
```
这两行代码包含了程序所需的头文件。
```c
int main() {
```
程序从这里开始执行,`main()` 函数是 C 语言程序的入口函数。
```c
int freq[26] = { 0 };
int pos[26] = { 0 };
int cnt = 0, min = 9999, firstIndex = -1;
char ch;
```
这几行代码定义了程序需要用到的变量。`freq[26]` 数组用于记录每个小写字母出现的次数,`pos[26]` 数组用于记录每个小写字母第一次出现的位置。`cnt` 变量用于记录已经扫描的字符数,`min` 变量用于记录第一个仅出现一次的字符的位置,`firstIndex` 变量用于记录第一个仅出现一次的字符的下标。`ch` 变量用于暂存输入的字符。
```c
while ((ch = getchar()) != '\n')
{
int off = ch - 'a';
if (pos[off] == 0)
{
pos[off] = cnt++;
}
freq[off] ++;
}
```
这个 `while` 循环用于输入字符串,并统计每个小写字母出现的次数和第一次出现的位置。`getchar()` 函数每次读入一个字符,如果读到了换行符 `\n`,则结束循环。`off` 变量用于记录当前字符相对于小写字母 `a` 的偏移量,如果之前没有出现过这个字符,就记录下当前位置 `cnt`,并将 `pos[off]` 的值设为 `cnt++`。然后将 `freq[off]` 的值加一,表示这个字符出现了一次。
```c
for (int i = 0; i < 26; i++)
{
if (freq[i] == 1)
{
if (min > pos[i])
{
min = pos[i];
firstIndex = i;
}
}
}
```
这个 `for` 循环遍历每个小写字母,找出第一个仅出现一次的字符,并记录该字符的下标。如果当前字符出现了一次,就比较它的位置 `pos[i]` 是否比当前最小位置 `min` 还小,如果是,就更新 `min` 和 `firstIndex` 的值。
```c
if (firstIndex != -1)
printf("%c\n", 'a' + firstIndex);
else
printf("no\n");
```
根据找到的第一个仅出现一次的字符的下标,输出该字符或者输出 "no"。如果 `firstIndex` 的值不是 -1,就打印出该字符,打印时将其转换为 ASCII 码对应的字符。如果 `firstIndex` 的值是 -1,就打印 "no"。最后,返回 0,表示程序正常退出。
#include <stdio.h>#include <stdlib.h>#define MAX_SIZE 1000int main() { int nums[MAX_SIZE], freq[MAX_SIZE], n, maxFreq = 0; printf("请输入数值个数:"); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &nums[i]); freq[i] = 0; } for (int i = 0; i < n; i++) { int count = 1; for (int j = i+1; j < n; j++) { if (nums[j] == nums[i]) { count++; } } freq[i] = count; if (count > maxFreq) { maxFreq = count; } } printf("出现次数最多的数为:"); for (int i = 0; i < n; i++) { if (freq[i] == maxFreq) { printf("%d ", nums[i]); freq[i] = -1; // 标记为已输出 } } printf("\n按从大到小顺序输出:"); for (int i = n-1; i >= 0; i--) { if (freq[i] != -1) { // 没有被输出过 for (int j = i-1; j >= 0; j--) { if (freq[j] != -1 && nums[j] > nums[i]) { int temp = nums[j]; nums[j] = nums[i]; nums[i] = temp; } } printf("%d ", nums[i]); } } return 0;}的总体设计
这段代码的总体设计是实现一个统计一个数组中出现次数最多的数,并按从大到小的顺序输出所有未输出的数字。具体实现如下:
1. 定义一个数组nums,用于存储输入的数值。
2. 定义一个数组freq,用于统计每个数值出现的次数,初始值都为0。
3. 读入数值个数n,并循环读入n个数值到nums数组中。
4. 遍历nums数组,统计每个数值出现的次数,并将其存储到freq数组中。
5. 找出出现次数最多的数值,并将其出现次数存储到maxFreq变量中。
6. 遍历freq数组,将出现次数最多的数值输出,并标记为已输出。
7. 按从大到小的顺序遍历nums数组,输出未被标记为已输出的数值。
总之,这段代码实现了一个简单的统计和排序功能,可以用于处理一些简单的数据分析和处理任务。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.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)