C++第五弹-练习题
Demo测试实例
求商及余数
使用 C++ 获取用户的输入两个数字,并将两个数字相除,然后将商和余数输出到屏幕。
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void){
//除数
int divisor;
//被除数
int dividend;
//余数
int remainder;
//商
int merchant;
cout << "请输入除数:" << endl;
cin >> divisor;
if(divisor != 0){
cout << "请输入被除数" << endl;
cin >> dividend;
merchant = divisor / dividend;
remainder = divisor % dividend;
cout << divisor << "/" << dividend << "=" << merchant << ",余数=" << remainder << endl;
}else{
cout << "除数不能为0" << endl;
}
return 0;
}
交换变量
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
void swap1(int &numOne,int &numTwo);
void swap2(int &numOne,int &numTwo);
void swap3(int &numOne,int &numTwo);
int main(void){
//第一个数字
int numOne;
//第二个数字
int numTwo;
cout << "请输入第一个数字" << endl;
cin >> numOne;
cout << "请输入第二个数字" << endl;
cin >> numTwo;
// cout << "swap1交换前:" << numOne << "," << numTwo << endl;
// swap1(numOne,numTwo);
// cout << "swap2交换后:" << numOne << "," << numTwo << endl;
//
// cout << "swap2交换前:" << numOne << "," << numTwo << endl;
// int *ptrOne = &numOne;
// int *ptrTwo = &numTwo;
// swap2(*ptrOne,*ptrTwo);
// cout << "swap2交换后:" << numOne << "," << numTwo << endl;
cout << "swap3交换前:" << numOne << "," << numTwo << endl;
int &rOne = numOne;
int &rTwo = numTwo;
swap3(rOne,rTwo);
cout << "swap3交换后:" << numOne << "," << numTwo << endl;
return 0;
}
//中间变量交换
void swap1(int &numOne ,int &numTwo){
int temp;
temp = numOne;
numOne = numTwo;
numTwo = temp;
}
//数字交换
void swap2(int &numOne,int &numTwo){
numOne = numOne + numTwo;
numTwo = numOne - numTwo;
numOne = numOne - numTwo;
}
//异或交换
void swap3(int &numOne,int &numTwo){
// int temp;
// temp = numOne ^ numTwo;
// numOne = temp ^ numOne;
// numTwo = temp ^ numTwo;
// numOne = numOne ^ numTwo;
// numTwo = numTwo ^ numOne;
// numOne = numOne ^ numTwo;
numOne^=numTwo^=numOne^=numTwo;
}
判断一个数是奇数还是偶数
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void){
int num;
cout << "输入一个数字:";
cin >> num;
if(num % 2 == 0){
cout << num << "是偶数" << endl;
}else{
cout << num << "是奇数" << endl;
}
return 0;
}
判断元音/辅音
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
char character;
cout << "请输入字符" << endl;
cin.get(character);
char yuan[] = {'a', 'e', 'i', 'o', 'u'};
for (int i = 0; i < sizeof(yuan) / sizeof(yuan[0]); i++) {
cout << "yuan[i]-32" << yuan[i]-32 << endl;
if (character == yuan[i] || character == (yuan[i] - 32)) {
cout << character << "是元音字母" << endl;
break;
}else{
cout << character << "不是元音字母" << endl;
}
}
return 0;
}
判断三个数中的最大数
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
int nums[3];
for (int i = 0; i < sizeof(nums) / sizeof(nums[i]); i++) {
cout << "请输入第" << i+1 << "个数:" << endl;
cin >> nums[i];
}
int numMax;
for (int i = 0; i < sizeof(nums) / sizeof(nums[i]); i++) {
numMax = nums[i] > numMax ? nums[i] : numMax;
}
cout << "最大值为:" << numMax << endl;
return 0;
}
计算自然数之和
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
int num, sum = 0;
cout << "请输入一个正整数:" << endl;
cin >> num;
for (int i = 1; i <= num; i++) {
sum += i;
cout << "sum=" <<sum << endl;
}
cout << "1到" << num << "的累加和为:" << sum << endl;
return 0;
}
判断闰年
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
int inputYear;
cout << "请输入年份:" << endl;
cin >> inputYear;
if (inputYear % 4 == 0 || inputYear % 400 == 0 && inputYear % 100 != 0) {
cout << inputYear << "是闰年" << endl;
}else{
cout << inputYear << "不是闰年" << endl;
}
return 0;
}
求一个数的阶乘
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int jieCheng(int num);
int main(void){
int num;
cout << "输入要计算的阶乘数" << endl;
cin >> num;
if(num !=0){
cout << num << "的阶乘结果为:" << jieCheng(num) << endl;
}else{
cout << "输入的数不能为0" << endl;
}
return 0;
}
int jieCheng(int num){
if(num == 1){
return 1;
}else{
return num * jieCheng(num -1);
}
}
创建各类三角形图案
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
int rows;
cout << "请输入行数:" << endl;
cin >> rows;
for (int i = 0; i < rows + 1; i++) {
for (int j = 0; j < i; j++) {
cout << "* ";
}
cout << endl;
}
cout << "------------------------------" << endl;
for (int i = 0; i < rows + 1; i++) {
for (int j = 0; j < i; j++) {
cout << j + 1 << " ";
}
cout << endl;
}
cout << "------------------------------" << endl;
char character = 'A';
for (int i = 0; i < rows + 1; i++) {
for (int j = 0; j < i + 1; j++) {
cout << character << " ";
}
character++;
cout << endl;
}
cout << "------------------------------" << endl;
for (int i = 0; i < rows + 1; i++) {
for (int j = 0; j < rows - i; j++) {
cout << "* ";
}
cout << endl;
}
cout << "------------------------------" << endl;
for (int i = 0; i < rows + 1; i++) {
for (int j = 0; j < rows - i; j++) {
cout << j + 1 << " ";
}
cout << endl;
}
cout << "------------------------------" << endl;
for (int i = 0; i < rows; i++) {
//打印空格
for(int k =0;k<rows-i;k++){
cout<<" ";
}
for (int j = 0; j < i + 1; j++) {
cout << " *";
}
cout << endl;
}
cout << "------------------------------" << endl;
for(int i=0;i<rows;i++){
for(int j = 0;j<rows-i;j++){
cout << " ";
}
for(int k=0;k<i+1;k++){
cout <<" " <<k+1;
}
cout << endl;
}
cout << "------------------------------" << endl;
for(int i=0;i<rows+1;i++){
for(int j =0;j<rows-i;j++){
cout << " ";
}
for(int k=0;k<i;k++){
cout << " " <<i;
}
cout << endl;
}
cout << "------------------------------" << endl;
for(int i=0;i<rows+1;i++){
for(int k=0;k<i;k++){
cout << " ";
}
for(int j =0;j<rows-i;j++){
cout << "* ";
}
cout << endl;
}
cout << "------------------------------" << endl;
for(int i=0;i<rows+1;i++){
for (int j = 0; j < rows-i; ++j) {
cout << " ";
}
int temp;
for(int k = 0; k < i+1; ++k){
if(k ==0 || i ==0){
temp =1;
}else{
temp = temp * (i-k+1) / k;
}
cout << " " << temp;
}
cout << endl;
}
return 0;
}
求两数的最大公约数
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
int numOne;
int numTwo;
cout << "请输入第一个数" << endl;
cin >> numOne;
cout << "请输入第二个数" << endl;
cin >> numTwo;
while (numOne != numTwo) {
if (numOne > numTwo) {
numOne = numOne - numTwo;
} else {
numTwo = numTwo - numOne;
}
}
cout << "最大公约数是" << numTwo << endl;
return 0;
}
求两数最小公倍数
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
int numOne, numTwo;
cout << "请输入第一个数" << endl;
cin >> numOne;
cout << "请输入第二个数" << endl;
cin >> numTwo;
//先确定最大的值
int max = numOne > numTwo ? numOne : numTwo;
do {
//如果最大的值对两个数都能进行整除,说明就是最小公倍数了
if (max % numOne == 0 && max % numTwo == 0) {
cout << "最小公倍数是:" << max << endl;
break;
} else {
//否则就让最大值自增,直到能整除
max++;
}
} while (true);
return 0;
}
实现一个简单的计算器
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void){
int numOne,numTwo;
cout << "请输入第一个数:" << endl;
cin >> numOne ;
cout << "请输入第二个数:" << endl;
cin >> numTwo ;
char fuhao;
cout << "请输入运算符:" ;
cin.ignore();
cin.get(fuhao);
switch(fuhao){
case '+':
cout << numOne << " + " << numTwo << " = " << numOne + numTwo << endl;
break;
case '-':
cout << numOne << " - " << numTwo << " = " << numOne - numTwo << endl;
break;
case '*':
cout << numOne << " * " << numTwo << " = " << numOne * numTwo << endl;
break;
case '/':
if(numOne != 0){
cout << numOne << " / " << numTwo << " = " << numOne / numTwo << endl;
}else{
cout << "除数不能为0" << endl;
}
break;
default:
cout << "输入的运算符有误" << endl;
}
return 0;
}
猴子吃桃问题
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
int day;
cout << "输入天数" << endl;
cin >> day;
int tao = 1;
int temp = day;
//因为是每次吃一半加1,从最后天数往前看,就是桃子总数*2+1,第十天是1个桃子,第九天就是(2+1)*2 = 4个桃子
for (int i = 1; i < day; i++) {
cout << "第" << temp-- << "天的桃子为:" << tao << endl;
tao = (tao + 1) * 2;
}
cout << "第" << temp << "天的桃子为:" << tao << endl;
return 0;
}
三只小猪称体重
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
int weight[3];
int sizes = sizeof(weight) / sizeof(weight[0]);
for (int i = 0; i < sizes; i++) {
cout << "请输入第" << i + 1 << "只小猪的体重" << endl;
cin >> weight[i];
}
int max = 0;
for (int j = 0; j < sizes; j++) {
if (weight[j] > weight[max]) {
max = j;
}
}
cout << "最重的是第" << max + 1 << "只小猪,它的体重是:" << weight[max] << endl;
return 0;
}
猜数字(0-100)
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
#include <cmath>
#include <random>
using namespace std;
int main(void) {
//1.设置种子
srand((unsigned) time(NULL));
//2.随机生成数字
int num = rand() % (1 - 1 + 100) + 1;
cout << "随机数已生成,请尝试猜测" << endl;
int userInput;
while (true) {
cout << "请输入你猜测的结果:" << endl;
cin >> userInput;
if (userInput > num) {
cout << "猜大了" << endl;
} else if (userInput < num) {
cout << "猜小了" << endl;
} else {
if (userInput == num) {
cout << "恭喜你,猜对了!" << endl;
break;
}
}
}
return 0;
}
求水仙花数
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
int bai, shi, ge;
for (int i = 2; i < 999; i++) {
bai = i / 100;
shi = i % 10;
ge = i % 100 / 10;
if (bai * bai * bai + shi * shi * shi + ge * ge * ge == i) {
cout << i << "是水仙花数" << endl;
}
}
return 0;
}
敲桌子(0-100)
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
for (int i = 0; i < 100; i++) {
if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) {
cout << "敲桌子" << endl;
} else {
cout << i << endl;
}
}
return 0;
}
乘法口诀表
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
for (int i = 1; i < 10; i++) {
for (int j = 1; j < i+1; j++) {
cout << j << " * " << i << " = " << (i * j) << " ";
}
cout << endl;
}
return 0;
}
数组元素逆置
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void){
int arr1[5]={1,2,3,4,5};
int sizes= sizeof(arr1)/sizeof(arr1[0]);
cout << "交换前:";
for(int i=0;i<sizes;i++){
cout<< arr1[i] << " ";
}
for(int i=0;i<sizes/2;i++){
int temp =0;
temp = arr1[i];
arr1[i] = arr1[sizes-1-i];
arr1[sizes-1-i] = temp;
}
cout << "交换后:";
for(int i=0;i<sizes;i++){
cout<< arr1[i] << " ";
}
return 0;
}
冒泡排序
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void) {
int arr[] = {3, 22, 1, 4, 23, 49, 87, 61};
int sizes = sizeof(arr) / sizeof(arr[0]);
cout << "排序前:";
for (int i = 0; i < sizes; i++) {
cout << arr[i] << " ";
}
for (int i = 0; i < sizes - 1; i++) {
for (int j = 0; j < sizes - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "排序后:";
for (int i = 0; i < sizes; i++) {
cout << arr[i] << " ";
}
return 0;
}
统计学生成绩
//
// Created by TodaySaturday on 2024/4/24.
//
#include <iostream>
using namespace std;
int main(void){
int score[][3]={
{88,22,76},
{99,33,77},
{66,55,44}
};
int sum;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
sum += score[i][j];
}
}
cout << "sum = " << sum << endl;
return 0;
}