1. 计算两点之间的距离

#include <iostream>
#include <cmath>
using namespace std;
int main() {
int x1, x2, y1, y2;
cout << "请输入x1, x2, y1, y2的值"; cin >> x1 >> x2 >> y1 >> y2;
cout << "两点之间的距离为: " << sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
return 0;
}

2. 成绩评定

#include <iostream>
using namespace std;
int main() {
float score;
char grade;
cout << "输入分数"; cin >> score;
while (score > 100 || score < 0) {
cout << "数据错误" << endl;
cin >> score;
}
switch (int(score / 10)) {
case 10:
case 9: grade = 'A'; break;
case 8: grade = 'B'; break;
case 7: grade = 'C'; break;
case 6: grade = 'D'; break;
default: grade = 'E'; break;
}
cout << "分数等级: " << grade;
return 0;
}

3. 完数

#include <iostream>
using namespace std;
int main() {
int m, i, s;
for (m = 2; m < 100; m++) {
s = 0;
for (i = 1; i < m; i++)
if (m % i == 0)
s += i;
if (s == m) cout << m << "是完数" << endl;
}
return 0;
}

4. 最大、最小、平均值

#include <iostream>
using namespace std;
int main() {
int a, max, min, s;
cin >> a;
max = min = s = a;
for (int i = 1; i < 20; i++) {
cin >> a;
if (max < a) max = a;
if (min > a) min = a;
s += a;
}
cout << "最大值: " << max << ", 最小值: " << min << ", 平均值: " << s / 20 << endl;
return 0;
}

5. 正数、负数、零的个数

#include<iostream>
using namespace std;
int main() {
int n = 0, m = 0, k = 0;
for (int i = 0; i < 20; i++) {
int a;
cin >> a;
if (a > 0) n++;
else if (a < 0) m++;
else k++;
}
cout << "正数个数: " << n << " 负数个数: " << m << " 零的个数: " << k << endl;
return 0;
}

6. 奇数和、偶数和

#include<iostream>
using namespace std;
int main() {
int n = 0, m = 0;
for (int i = 1; i < 51; i++)
if (i % 2) n += i;
else m += i;
cout << "奇数和: " << n << " 偶数和: " << m;
return 0;
}

7. 素数

#include <iostream>
using namespace std;
bool isprime(int num) {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
int main() {
int count = 0;
for (int i = 2; i <= 1000; i++)
if (isprime(i)) {
cout << i << '\t';
count++;
if (count % 5 == 0) cout << endl;
}
cout << endl;
return 0;
}

8. 找最大值

#include <iostream>
using namespace std;
int max(int a, int b, int c = 0) {
if (a < b) a = b;
if (a < c) a = c;
return a;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
cout << "max(a, b, c): " << max(a, b, c) << endl;
cout << "max(a, b): " << max(a, b) << endl;
return 0;
}

9. 类与对象 - 计算体积

#include <iostream>
using namespace std;
class Box {
int length, width, height;
public: void get_value() {
cout << "输入长度 宽度 高度: " << endl;
cin >> length >> width >> height;
}
float display() {
cout << length * width * height << endl;
}
};
int main() {
Box box1, box2, box3;
box1.get_value();
cout << "box1的体积为 ";
box1.display();
box2.get_value();
cout << "box2的体积为 ";
box2.display();
box3.get_value();
cout << "box3的体积为 ";
box3.display();
return 0;
}

10. 类与对象 - 时间显示

#include <iostream>
using namespace std;
class Time {
int hour, minute, second;
public:
Time(int h, int m, int s) {
hour = h; minute = m; second = s;
}
void show_time() {
cout << hour << ":" << minute << ":" << second << endl;
}
};
int main() {
Time t(9, 10, 0);
t.show_time();
return 0;
}

11. 类与对象 - 长方体比较

#include <iostream>
using namespace std;
class Cuboid {
int length, width, height;
public:
Cuboid(int l = 3, int w = 4, int h = 5) {
length = l; width = w; height = h;
}
int getL() { return length; }
int getW() { return width; }
int getH() { return height; }
int getArea() {
return length * width * height;
}
bool isEqual(Cuboid &b) {
return length == b.getL() && width == b.getW() && height == b.getH();
}
};

12. 类与对象 - 矩形面积和周长

#include <iostream>
using namespace std;
class Rectangle {
float length, width;
public:
Rectangle(float l = 20, float w = 30) { length = l; width = w; }
float Area() {
return length * width;
}
float Perimeter() {
return (length + width) * 2;
}
};

13. 类与对象 - 日期

#include <iostream>
using namespace std;
class CDate {
int Year, Month, Day;
public:
CDate() { Year = 2024; Month = 1; Day = 5; }
CDate(int y = 2024, int m = 1, int d = 1) { Year = y; Month = m; Day = d; }
void PrintDate() { cout << Year << "-" << Month << "-" << Day; }
void SetDate(int sy, int sm, int sd) { Year = sy; Month = sm; Day = sd; }
};

14. 类与对象 - 日期

#include <iostream>
using namespace std;
class Date {
int Month, Day, Year;
public:
Date(int y = 2019, int m = 1, int d = 1) { Year = y; Month = m; Day = d; }
void Display() { cout << Month << '/' << Day << '/' << Year; }
};

15. 类与对象 - 复数相加

#include <iostream>
using namespace std;
class Complex {
double real, image;
public:
Complex(double r, double i) { real = r; image = i; }
void Show() { cout << real << ',' << image << endl; }
Complex operator+(Complex &c2) {
return Complex(real + c2.real, image + c2.image);
}
};

16. 类与对象 - 继承

#include <iostream>
using namespace std;
class rectangle {
double length, width;
public:
rectangle(double l, double w) { length = l; width = w; }
float Area() { return length * width; }
};
class rectangular : public rectangle {
double height;
public:
rectangular(double l, double w, double h) : rectangle(l, w) { height = h; }
double volume() { return Area() * height; }
};
int main() {
rectangle obj1(2, 8);
rectangular obj2(3, 4, 5);
cout << "长方形的面积为: " << obj1.Area() << endl;
cout << "长方体的体积为: " << obj2.volume() << endl;
return 0;
}

c++ 期末编程题的更多相关文章

  1. 中国MOOC_面向对象程序设计——Java语言_期末考试编程题_1细胞自动机

    期末考试编程题 返回   这是期末考试的编程题 温馨提示: 1.本次考试属于Online Judge题目,提交后由系统即时判分. 2.学生可以在考试截止时间之前提交答案,系统将取其中的最高分作为最终成 ...

  2. 中国MOOC_零基础学Java语言_期末考试的编程题_1二进制的前导的零

    期末考试的编程题 返回   这是期末考试的编程题,在60分钟内,你可以多次提交,直到正确为止. 温馨提示: 1.本次考试属于Online Judge题目,提交后由系统即时判分. 2.学生可以在考试截止 ...

  3. 算法是什么我记不住,But i do it my way. 解一道滴滴出行秋招编程题。

    只因在今日头条刷到一篇文章,我就这样伤害我自己,手贱. 刷头条看到一篇文章写的滴滴出行2017秋招编程题,后来发现原文在这里http://www.cnblogs.com/SHERO-Vae/p/588 ...

  4. C算法编程题系列

    我的编程开始(C) C算法编程题(一)扑克牌发牌 C算法编程题(二)正螺旋 C算法编程题(三)画表格 C算法编程题(四)上三角 C算法编程题(五)“E”的变换 C算法编程题(六)串的处理 C算法编程题 ...

  5. C算法编程题(七)购物

    前言 上一篇<C算法编程题(六)串的处理> 有些朋友看过我写的这个算法编程题系列,都说你写的不是什么算法,也不是什么C++,大家也给我提出用一些C++特性去实现问题更方便些,在这里谢谢大家 ...

  6. C算法编程题(六)串的处理

    前言 上一篇<C算法编程题(五)“E”的变换> 连续写了几篇有关图形输出的编程题,今天说下有关字符串的处理. 程序描述 在实际的开发工作中,对字符串的处理是最常见的编程任务.本题目即是要求 ...

  7. C算法编程题(五)“E”的变换

    前言 上一篇<C算法编程题(四)上三角> 插几句话,说说最近自己的状态,人家都说程序员经常失眠什么的,但是这几个月来,我从没有失眠过,当然是过了分手那段时期.每天的工作很忙,一个任务接一个 ...

  8. C算法编程题(四)上三角

    前言 上一篇<C算法编程题(三)画表格> 上几篇说的都是根据要求输出一些字符.图案等,今天就再说一个“上三角”,有点类似于第二篇说的正螺旋,输出的字符少了,但是逻辑稍微复杂了点. 程序描述 ...

  9. C算法编程题(三)画表格

    前言 上一篇<C算法编程题(二)正螺旋> 写东西前还是喜欢吐槽点东西,要不然写的真还没意思,一直的想法是在博客园把自己上学和工作时候整理的东西写出来和大家分享,就像前面写的<T-Sq ...

  10. C算法编程题(二)正螺旋

    前言 上一篇<C算法编程题(一)扑克牌发牌> 写东西前总是喜欢吐槽一些东西,还是多啰嗦几句吧,早上看了一篇博文<谈谈外企涨工资那些事>,里面楼主讲到外企公司包含的五类人,其实不 ...

随机推荐

  1. Linux系列教程——Linux基本权限、Linux特殊权限、LinuxACL控制、Linux输入输出

    @ 目录 1 Linux基本权限 1.权限基本概述 1.什么是权限? 2.为什么要有权限? 3.权限与用户之间的关系? 4.权限中的rwx分别代表什么含义? 2.权限设置示例 1.为什么要设定权限,我 ...

  2. C++的模板类在HotSpot VM中的应用

    模板是c++的一种特性,允许函数或者类通过泛型(generic types)的形式表现或者运行.模板可以使得函数或类在对应不同的类型(types)的时候正常工作,而无需为每一种类型分别写一份代码. 在 ...

  3. Merge-Lrc 合并歌词的小工具

    Merge-Lrc 背景 音乐区有群友希望各种乱七八糟的歌词(lrc 格式居多,里面甚至还有翻译)可以整理成单一的文件,或者一个仅翻译的歌词可以和原文的歌词合并.于是就开发了这款工具.地址:https ...

  4. unity利用Rigibody实现第一人称移动

    1. CameraRotation脚本,将它给MainCamera,实现上下视角旋转 using System.Collections; using System.Collections.Generi ...

  5. tomcat nio2源码分析

    一. 前言 ​ 最近在看tomcat connector组件的相关源码,对Nio2的异步回调过程颇有兴趣,平时读源码不读,自己读的时候很多流程都没搞明白,去查网上相关解析讲的给我感觉也不是特别清晰,于 ...

  6. 浅谈斜率优化DP

    前言 考试 T2 出题人放了个树上斜率优化 DP,直接被同校 OIER 吊起来锤. 离 NOIP 还有不到一周,赶紧学一点. 引入 斜率 斜率,数学.几何学名词,是表示一条直线(或曲线的切线)关于(横 ...

  7. Spring5学习随笔-Spring5的第一个程序(环境搭建、日志框架整合)

    第二章.第一个Spring程序 1.软件版本 1.JDK1.8+ 2.Maven3.5+ 3.IDEA2018+ 4.SpringFramework 5.1.4 官网:www.spring.io 2. ...

  8. 1. 手动移植FreeRTOS V9.00到 Stm32F103C8T6

    记录移植过程,以便以后查看: 附上FreeRTOS源码和  测试文件: 链接:https://pan.baidu.com/s/1v6nvDOk4-2NILYqN3njGjQ 提取码:1234 1.使用 ...

  9. 矩阵重叠 (3.18 leetcode每日打卡)

    度简单66收藏分享切换为英文关注反馈矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形 ...

  10. 高效开发与设计:提效Spring应用的运行效率和生产力

    引言 现状和背景 Spring框架是广泛使用的Java开发框架之一,它提供了强大的功能和灵活性,但在大型应用中,由于Spring框架的复杂性和依赖关系,应用的启动时间和性能可能会受到影响.这可能导致开 ...