C 字符串排序 SDUT

Time Limit: 1000 ms Memory Limit: 65536 KiB


Problem Description

输入3个字符串,按字典序从小到大进行排序。


Input

输入数据有一行,分别为3个字符串,用空格分隔,每个字符串长度不超过100。


Output

输出排序后的三个字符串,用空格分隔。


Sample Input

abcd cdef bcde


Sample Output

abcd bcde cdef


Hint

Source


本题可以考虑三个整数排序的方法,只是在交换时用strcpy实现

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
   int main()
   {
       char a[101],b[101],c[101],s[101];

       scanf("%s%s%s",a,b,c);
       if(strcmp(a,b)>0)
       {
           strcpy(s,a);
           strcpy(a,b);
           strcpy(b,s);
       }
       if(strcmp(a,c)>0)
       {
           strcpy(s,a);
           strcpy(a,c);
           strcpy(c,s);
       }
       if(strcmp(b,c)>0)
       {
           strcpy(s,b);
           strcpy(b,c);
           strcpy(c,s);
       }
       printf("%s %s %s",a,b,c);


       return 0;

   }


### SDUT Python 字符串处理方法与示例教程 #### 1. 输入与分割操作 在Python中,`input()` 函数用于接收用户的输入数据[^1]。如果需要对字符串进行分割,则可以使用 `split()` 方法。例如: ```python data = input("请输入一段文字:").strip() words = data.split() # 默认按空格分割 print(words) ``` 上述代码会将用户输入的内容按照空格分隔成多个部分并存储到列表中。 --- #### 2. 回文判断 回文是指正读和反读都相同的字符串。可以通过切片或者 `reversed()` 函数实现回文检测[^2]。以下是两种常见的实现方式: ##### 方法一:切片反转 ```python def is_palindrome_slice(s): return s == s[::-1] result = is_palindrome_slice("abccba") print(result) # 输出 True ``` ##### 方法二:`reversed()` 函数 ```python def is_palindrome_reversed(s): return "".join(reversed(s)) == s result = is_palindrome_reversed("abccba") print(result) # 输出 True ``` 对于非回文字符串 `"hdjh877"`,以上两者的输出均为 `False`。 --- #### 3. 字符统计与排序 通过 `collections.Counter` 类可以高效地统计字符串中的字符频率,并进一步对其进行排序[^3]。以下是一个完整的例子: ```python from collections import Counter string = "hello world" counter = Counter(string) # 获取最常出现的字符及其频次 most_common = counter.most_common() max_freq = most_common[0][1] chars_with_max_freq = [char for char, freq in most_common if freq == max_freq] if len(chars_with_max_freq) > 1: result_char = sorted(chars_with_max_freq)[0] else: result_char = chars_with_max_freq[0] print(f"出现最多的字符是 '{result_char}',共出现了 {max_freq} 次") ``` 此代码片段能够找出给定字符串中最频繁出现的字符。 --- #### 4. 扩展符解析 针对扩展符 `'-'` 的字符串转换问题,可以根据特定规则将其还原为原始形式[^4]。下面提供了一个解决方案: ```python def expand_string(s): parts = [] current_part = "" i = 0 while i < len(s): if s[i].isalnum(): start = s[i] if i + 1 < len(s) and s[i + 1] == '-': end_index = i + 2 if end_index < len(s) and s[end_index].isalnum(): end = s[end_index] if (start.isdigit() and end.isdigit()) or \ (start.isalpha() and end.isalpha()): step = 1 if ord(start) > ord(end): # 如果起始大于结束则倒序排列 step = -1 expanded = ''.join([chr(c) for c in range(ord(start), ord(end) + step, step)]) parts.append(expanded) i += 2 # 跳过 '-' 和下一个字符 continue parts.append(start) else: parts.append(s[i]) i += 1 return ''.join(parts) expanded_result = expand_string("a-cD-E1-3xyz") print(expanded_result) # 输出 abcDEFGHIJKLMNOPQRSTUVWXYZ123xyz ``` 该函数实现了基于 `[a-z]`, `[A-Z]`, 或 `[0-9]` 范围内的扩展功能。 --- #### 5. 计算字符串长度 利用内置函数 `len()` 可以轻松获取任何序列类型的元素数量,包括但不限于列表、元组、字典、集合以及字符串本身[^5]。例如: ```python text = "Hello World!" length = len(text) print(length) # 输出 12 ``` 此处计算的是整个字符串的实际长度,包含所有空白字符和其他特殊符号。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碧羽o(* ̄▽ ̄*)ブ回雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值