题目## 题目## 题目
解题思路
- 读取输入的句子
- 将句子按空格分割成单词列表
- 反转单词列表
- 将反转后的单词列表重新组合成字符串
- 输出结果
代码
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
string line;
getline(cin, line);
istringstream iss(line);
vector<string> words;
string word;
// 分割单词
while (iss >> word) {
words.push_back(word);
}
// 反转单词列表
reverse(words.begin(), words.end());
// 输出结果
for (size_t i = 0; i < words.size(); ++i) {
if (i > 0) cout << " ";
cout << words[i];
}
cout << endl;
return 0;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
// 分割单词
String[] words = line.split(" ");
// 反转单词列表
Collections.reverse(Arrays.asList(words));
// 输出结果
System.out.println(String.join(" ", words));
}
}
# 读取输入
s = input()
# 分割单词并反转
words = s.split()
words.reverse()
# 输出结果
print(" ".join(words))
算法及复杂度
- 算法:字符串分割、列表反转、字符串拼接
- 时间复杂度: O ( n ) \mathcal{O}(n) O(n) - 其中 n n n为字符串长度
- 空间复杂度: O ( n ) \mathcal{O}(n) O(n) - 需要存储分割后的单词列表
解题思路
- 读取浮点数输入
- 获取小数部分:
- 可以通过减去整数部分得到
- 或使用语言内置函数
- 判断小数部分:
- 如果大于等于0.5,向上取整
- 如果小于0.5,向下取整
- 输出结果
代码
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num;
cin >> num;
cout << round(num) << endl;
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double num = sc.nextDouble();
System.out.println(Math.round(num));
}
}
num = float(input())
print(int(num+0.5))
算法及复杂度
- 算法:简单的条件判断或使用语言内置的四舍五入函数
- 时间复杂度: O ( 1 ) \mathcal{O}(1) O(1) - 只需要常数时间完成计算
- 空间复杂度: O ( 1 ) \mathcal{O}(1) O(1) - 只需要常数级额外空间
解题思路
-
题目要求:
- 解析类似xcopy命令的参数
- 参数之间用空格分隔
- 对于带引号的参数,需要作为一个完整参数处理
- 输出参数个数和每个参数值
-
解题方法:
- 使用状态机处理字符串
- 特别处理引号内的内容
- 按规则分割参数
代码
def parse_command(command):
params = []
current_param = []
in_quotes = False
for char in command:
if char == '"': # 处理引号
in_quotes = not in_quotes
elif char == ' ' and not in_quotes: # 空格且不在引号内
if current_param:
params.append(''.join(current_param))
current_param = []
else: # 普通字符
current_param.append(char)
# 处理最后一个参数
if current_param:
params.append(''.join(current_param))
# 输出结果
print(len(params))
for param in params:
print(param)
while True:
try:
command = input().strip()
parse_command(command)
except:
break
import java.util.*;
public class Main {
public static void parseCommand(String command) {
List<String> params = new ArrayList<>();
StringBuilder currentParam = new StringBuilder();
boolean inQuotes = false;
for (char c : command.toCharArray()) {
if (c == '"') { // 处理引号
inQuotes = !inQuotes;
} else if (c == ' ' && !inQuotes) { // 空格且不在引号内
if (currentParam.length() > 0) {
params.add(currentParam.toString());
currentParam = new StringBuilder();
}
} else { // 普通字符
currentParam.append(c);
}
}
// 处理最后一个参数
if (currentParam.length() > 0) {
params.add(currentParam.toString());
}
// 输出结果
System.out.println(params.size());
for (String param : params) {
System.out.println(param);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String command = sc.nextLine().trim();
parseCommand(command);
}
}
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void parseCommand(string command) {
vector<string> params;
string currentParam;
bool inQuotes = false;
for (char c : command) {
if (c == '"') { // 处理引号
inQuotes = !inQuotes;
} else if (c == ' ' && !inQuotes) { // 空格且不在引号内
if (!currentParam.empty()) {
params.push_back(currentParam);
currentParam.clear();
}
} else { // 普通字符
currentParam += c;
}
}
// 处理最后一个参数
if (!currentParam.empty()) {
params.push_back(currentParam);
}
// 输出结果
cout << params.size() << endl;
for (const string& param : params) {
cout << param << endl;
}
}
int main() {
string command;
while (getline(cin, command)) {
parseCommand(command);
}
return 0;
}
算法分析
- 算法:状态机
- 时间复杂度: O ( n ) \mathcal{O}(n) O(n),其中 n n n 为输入字符串长度
- 空间复杂度: O ( n ) \mathcal{O}(n) O(n),需要存储解析后的参数