考研机试常见基本题型

1、求100以内的素数

sqrt()函数在cmath头文件中。

cpp 复制代码
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int count = 0;  // 用于统计素数的个数
    // 遍历 100 到 200 之间的每一个数
    for (int num = 100; num <= 200; num++) {
        bool isPrime = true;  // 先假设当前数是素数
        // 判断是否为素数,检查从 2 到根号 num 的数
        for (int i = 2; i <= sqrt(num); i++) {
            if (num % i == 0) {
                isPrime = false;  // 如果能整除,则不是素数
                break;
            }
        }
        if (isPrime) {
            count++;  // 如果是素数,计数器加 1
            cout << num << " ";  // 输出素数
        }
    }
    cout << endl << count << endl;  // 换行后输出素数的个数
    return 0;
}

2、判断某点是否在一个三角形内

1、面积法

abs()、sqrt()都是在cmath中

cpp 复制代码
#include <iostream>
#include <vector>
#include <cmath>

// 计算两点间距离
double distance(std::vector<double>& a, std::vector<double>& b) {
    return std::sqrt((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]));
}

// 计算三角形面积
double triangleArea(std::vector<double>& a, std::vector<double>& b, std::vector<double>& c) {
    double ab = distance(a, b);
    double bc = distance(b, c);
    double ca = distance(c, a);
    double s = (ab + bc + ca) / 2;
    return std::sqrt(s * (s - ab) * (s - bc) * (s - ca));
}

// 判断点是否在三角形内
bool isPointInTriangle(std::vector<double>& p, std::vector<double>& a, std::vector<double>& b, std::vector<double>& c) {
    double s = triangleArea(a, b, c);
    double s1 = triangleArea(p, a, b);
    double s2 = triangleArea(p, b, c);
    double s3 = triangleArea(p, c, a);
    // 考虑浮点数精度问题,判断差值是否在一个很小的范围内
    return std::abs(s - (s1 + s2 + s3)) < 1e-9;
}

int main() {
    // 测试代码
    std::vector<double> a = {0, 0};
    std::vector<double> b = {1, 0};
    std::vector<double> c = {0, 1};
    std::vector<double> p = {0.2, 0.2};
    std::cout << (isPointInTriangle(p, a, b, c)? "在三角形内" : "不在三角形内") << std::endl;
    return 0;
}

3、鸡兔同笼

cpp 复制代码
#include <iostream>
using namespace std;

// 判断是否能构成有效的鸡兔同笼问题
bool isSameCage(int heads, int feet) {
    // 判断脚的数量减去两倍头的数量是否为偶数且大于等于0
    if ((feet - 2 * heads) % 2 == 0 && feet - 2 * heads >= 0) {
        int y = (feet - 2 * heads) / 2;  // 兔子的数量
        int x = heads - y;  // 鸡的数量
        // 判断鸡和兔的数量是否都为非负整数
        if (x >= 0 && y >= 0) {
            return true;
        }
    }
    return false;
}
int main() {
    // 测试用例
    cout << (isSameCage(35, 94)? "true" : "false") << endl;
    cout << (isSameCage(2, 4)? "true" : "false") << endl;
    cout << (isSameCage(4, 2)? "true" : "false") << endl;
    cout << (isSameCage(10, 30)? "true" : "false") << endl;
    return 0;
}

英文字母大小写转换

描述:把一个字符串中所有的大写字母转换为小写字母,小写字母转换为大写字母,其他字符保持不变。

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    cin >> s;
    for (char& c : s) {
        if (isupper(c)) {
            c = tolower(c);
        } else if (islower(c)) {
            c = toupper(c);
        }
    }
    cout << s << endl;
    return 0;
}

c语言版本

c 复制代码
#include <iostream>
#include <string>
using namespace std;

int main() {
	string s;
	cin >> s;
	for (char& c : s) {
	if ('A'<=c && c<='Z') {
		c +=32;
	}
	else if ('a' <= c && c <= 'z') {
		c -=32;
	}
	}
	cout << s << endl;
    return 0;
}
相关推荐
whoarethenext10 分钟前
使用 C++ 实现 MFCC 特征提取与说话人识别系统
开发语言·c++·语音识别·mfcc
R-G-B10 分钟前
【MFC】Combobox下拉框中4个选项,运行后点击下拉框选项不能全部展示出来,只能显示2个选项,需要垂直滚动条滚动显示其余选项
c++·mfc
写个博客40 分钟前
暑假算法日记第二天
算法
ChaITSimpleLove1 小时前
.NET9 实现排序算法(MergeSortTest 和 QuickSortTest)性能测试
算法·排序算法·.net·benchmarkdotnet·datadog.trace
CVer儿1 小时前
svd分解求旋转平移矩阵
线性代数·算法·矩阵
Owen_Q1 小时前
Denso Create Programming Contest 2025(AtCoder Beginner Contest 413)
开发语言·算法·职场和发展
视觉人机器视觉2 小时前
Visual Studio2022和C++opencv的配置保姆级教程
c++·opencv·visual studio
liulilittle2 小时前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
Wilber的技术分享2 小时前
【机器学习实战笔记 14】集成学习:XGBoost算法(一) 原理简介与快速应用
人工智能·笔记·算法·随机森林·机器学习·集成学习·xgboost
Tanecious.3 小时前
LeetCode 876. 链表的中间结点
算法·leetcode·链表