在编程的世界里,难题无处不在,它们像是一座座小山,等待我们去攀登。而趣味题目则是这座山中充满惊喜的宝藏,它们不仅能挑战我们的智力,更能激发我们的创造力。本文将为你精选一系列编程难题,让你在挑战中收获快乐,在快乐中提升技能。
一、逻辑思维题
1. 猜数字游戏
问题描述:编写一个猜数字游戏,游戏生成一个1到100之间的随机数,玩家需要猜测这个数字是多少。每次猜测后,系统会提示“太大了”或“太小了”,玩家根据提示不断缩小猜测范围。
编程语言:Python
import random
def guess_number_game():
target_number = random.randint(1, 100)
guess = 0
while guess != target_number:
guess = int(input("猜猜看这个数字是多少(1-100):"))
if guess < target_number:
print("太小了!")
elif guess > target_number:
print("太大了!")
else:
print("恭喜你,猜对了!")
guess_number_game()
2. 数组去重
问题描述:给定一个整数数组,请编写一个函数,返回数组中所有不同元素组成的数组。
编程语言:Java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class UniqueArray {
public static List<Integer> removeDuplicates(int[] array) {
Set<Integer> set = new HashSet<>();
for (int num : array) {
set.add(num);
}
return new ArrayList<>(set);
}
public static void main(String[] args) {
int[] array = {1, 2, 3, 2, 1, 4, 5, 4};
List<Integer> uniqueList = removeDuplicates(array);
System.out.println(uniqueList);
}
}
二、算法挑战题
1. 排序算法
问题描述:实现一个高效的排序算法,例如快速排序、归并排序或堆排序,并对其进行测试。
编程语言:C++
#include <iostream>
#include <vector>
#include <algorithm>
void quickSort(std::vector<int>& array, int left, int right) {
if (left < right) {
int pivot = array[left];
int i = left, j = right;
while (i < j) {
while (i < j && array[j] >= pivot) j--;
if (i < j) array[i++] = array[j];
while (i < j && array[i] <= pivot) i++;
if (i < j) array[j--] = array[i];
}
array[i] = pivot;
quickSort(array, left, i - 1);
quickSort(array, i + 1, right);
}
}
int main() {
std::vector<int> array = {9, 7, 5, 11, 12, 2, 14, 3, 10, 6};
quickSort(array, 0, array.size() - 1);
for (int num : array) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
2. 寻找最长子串
问题描述:给定一个字符串,找出其中最长的不重复子串的长度。
编程语言:JavaScript
function findLongestSubstring(str) {
let charMap = {};
let start = 0;
let maxLength = 0;
for (let end = 0; end < str.length; end++) {
let char = str[end];
if (charMap[char] > start) {
start = charMap[char];
}
charMap[char] = end;
maxLength = Math.max(maxLength, end - start + 1);
}
return maxLength;
}
console.log(findLongestSubstring("abcabcbb")); // 输出:3
三、实战案例
1. 制作待办事项列表
问题描述:创建一个简单的待办事项列表应用,允许用户添加、删除和查看待办事项。
编程语言:HTML + CSS + JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>待办事项列表</title>
<style>
/* CSS样式 */
</style>
</head>
<body>
<h1>待办事项列表</h1>
<input type="text" id="todoInput" placeholder="添加待办事项...">
<button onclick="addTodo()">添加</button>
<ul id="todoList"></ul>
<script>
let todos = [];
function addTodo() {
const input = document.getElementById("todoInput");
const text = input.value.trim();
if (text !== "") {
todos.push(text);
renderTodos();
input.value = "";
}
}
function renderTodos() {
const list = document.getElementById("todoList");
list.innerHTML = "";
todos.forEach((todo, index) => {
const item = document.createElement("li");
item.textContent = todo;
const deleteButton = document.createElement("button");
deleteButton.textContent = "删除";
deleteButton.onclick = () => deleteTodo(index);
item.appendChild(deleteButton);
list.appendChild(item);
});
}
function deleteTodo(index) {
todos.splice(index, 1);
renderTodos();
}
</script>
</body>
</html>
通过这些编程难题,你不仅能锻炼自己的逻辑思维能力,还能在解决实际问题的过程中提升编程技能。编程之路充满了挑战,但正是这些挑战让我们不断成长。愿你在这个编程的世界里,勇攀高峰,收获满满!
