IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++代码练习及答案 -> 正文阅读

[C++知识库]C++代码练习及答案

简单C++程序

#include<iostream>//预处理命令
using namespace std;//命名空间
int main() {
	
	cout << "Hello!" << endl;//输出
	cout << "Welcome to C++!" << endl;

	return 0;
}

输入一个年份,判断是否是闰年

#include<iostream>
using namespace std;
int main() {
	
	int year;
	bool isLeapYear;

	cout << "输入一个年份:" << endl;
	cin >> year;
	isLeapYear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));

	if (isLeapYear)
		cout << year << " is a leap year" << endl;
	else
	{
		cout << year << " is not a leap year" << endl;
	}

	return 0;
}

比较两个数的大小

#include<iostream>
using namespace std;

int main() {
	
	int x, y;
	cout << "Enter x and y:";
	cin >> x >> y;

	if (x != y)
		if (x > y)
			cout << "x>y" << endl;
		else
		{
			cout << "x<y" << endl;
		}
	else
	{
		cout << "x=y" << endl;
	}

	return 0;
}

输入一个0~6的整数,转换成星期输出

#include<iostream>
using namespace std;

int main() {
	
	int day;

	cin >> day;
	switch (day)
	{
	case 0:
		cout << "Sunday" << endl;
		break;
	case 1:
		cout << "Monday" << endl;
		break;
	case 2:
		cout << "Tuesday" << endl;
		break;
	case 3:
		cout << "Wednesday" << endl;
		break;
	case 4:
		cout << "Thursday" << endl;
		break;
	case 5:
		cout << "Friday" << endl;
		break;
	case 6:
		cout << "Saturday" << endl;
		break;
	default:
		cout << "Day out of range Sunday...Saturday" << endl;
		break;
	}

	return 0;
}

求1~10的之和

#include<iostream>
using namespace std;

int main() {
	
	int i = 1,sum = 0;
	while (i <= 10) {
		sum += i;
		i++;
	}
	cout << "sum=" << sum << endl;

	return 0;
}

输入一个整数,将各位数字反转后输出

#include<iostream>
using namespace std;

int main() {
	
	int n, right_digit, newnum = 0;
	cout << "Enter the number:";
	cin >> n;

	cout << "The number in reverse order is ";
	do {
		right_digit = n % 10;
		cout << right_digit;
		n /= 10;
	} while (n != 0);
	cout << endl;

	return 0;
}

用do…while语句编程,求自然数1~10之和

#include<iostream>
using namespace std;

int main() {
	
	int i = 1, sum = 0;
	do {
		sum += i;
		i++;
	} while (i < 10);
	cout << "sum=" << sum << endl;

	return 0;
}

输入一个整数,求出它的所有因子

#include<iostream>
using namespace std;

int main() {
	
	int n;

	cout << "Enter a postive integer:";
	cin >> n;
	cout << "Number " << n << " Factors ";

	for (int k = 1; k <= n; k++) {
		if (n % k == 0) {
			cout << k << " ";
		}
	}
	cout << endl;

	return 0;
}

编写输出图案

在这里插入图片描述

#include<iostream>
using namespace std;

int main() {
	
	const int N = 4;
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= 30; j++) {
			cout << ' ' ;
		}
		for (int j = 1; j <= 8 - 2 * i; j++) {
			cout << ' ' ;
		}
		for (int j = 1; j <= 2 * i - 1; j++) {
			cout << '*';
		}
		cout << endl;
	}
	for (int i = 1; i <= N - 1; i++) {
		for (int j = 1; j <= 30; j++) {
			cout << ' ';
		}
		for (int j = 1; j <= 7 - 2 * i; j++) {
			cout << '*';
		}
		cout << endl;
	}

	return 0;
}

求100~200之间不能被3整除的数

#include<iostream>
using namespace std;

int main() {
	
	for (int n = 100; n <= 200; n++) {
		if (n % 3 != 0) {
			cout << n << endl;
		}
	}

	return 0;
}

读入一系列整数,统计出正整数个数i和负整数个数j,读入0则结束

#include<iostream>
using namespace std;

int main() {
	
	int i = 0, j = 0, n;
	cout << "Enter some integers please (enter 0 to quit):" << endl;
	cin >> n;
	while (n != 0) {
		if (n > 0) {
			i += 1;
		}
		if (n < 0) {
			j += 1;
		}
		cin >> n;
	}
	cout << "Count of positive integers:" << i << endl;
	cout << "Count of negative integers:" << j << endl;

	return 0;
}

设某次体育比赛只有4种可能,所以可以声明一个枚举类型,用一个枚举类型的变量来存放比赛比赛结果

#include<iostream>
using namespace std;

enum GameResult{WIN,LOSE,WIE,CANCEL};

int main() {
	
	GameResult result;//声明变量时,可以不写关键字enum
	enum GameResult omit = CANCEL;//也可以在类型名前写enum

	for (int count = WIN; count <= CANCEL; count++) {//隐式类型转换
		result = GameResult(count);//显式类型转换
		if (result == omit) {
			cout << "The game was cancelled" << endl;
		}
		else {
			cout << "The game was played";
			if (result == WIN) {
				cout << "and we won!";
			}
			if (result == LOSE) {
				cout << "and we lost.";
			}
			cout << endl;
		}
	}

	return 0;
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-23 10:32:07  更:2021-07-23 10:34:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/28 14:51:26-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码