栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++ day.1 基础练习

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++ day.1 基础练习

1.c++简单猜数字游戏#day1#
进行猜 1~100 的数字,若猜数字不正确则提示猜数字是大了还是小了,直到猜中数字则结束本局。

#include
#include
#include
#include
using namespace std;
int main()
{
     //添加随机数种子,作用利用当前系统时间生成随机数,防止每次随机都是固定数字
	srand((unsigned int)time(NULL));//跟随系统时间发生变化达到随机效果
	//1.系统生成随机数
	//2.玩家进行猜测
	//3.判断玩家猜测数
	int  c = rand() % 100 +1;  
	//  rand()%100    生成0-99之间的随机数,在后面加就把范围右移到1-100
	cout << "生成的随机数为:" << c << endl;
	int val = 0;
	while (1)  
	{
		cin >> val;
		if (val > c)
		{
			cout << "dale" << endl; //大了
			
		}
		else if (val < c)
		{
			cout << "xiaole" << endl;///小了
			
		}
		else
		{
			cout << "yes" << endl;
			break;//此处break达成条件自动跳出while循环  游戏结束
		}
	}
	return 0;

}

2.c++经典100-1000内水仙花数
“水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身。例如:1^3 + 5^3+ 3^3 = 153。”

#include
using namespace std;
int main()
{
	int number = 100,a,b,c;//定义起始值最小三位数100
	do 
	{
		a = number % 10;  //个位
		b = number / 10 % 10;  //十位
		c = number / 100; //百位
		if (a*a*a+b*b*b+c*c*c == number)
			cout << number << endl;
		number++;
	} while (number < 1000);
	return 0;
}

3.c++ 练习案例敲桌子

#include
using namespace std;
int main()
{
	int i,a,b;//设立循环1-100
	for (i = 1; i <= 100; i++)
	{
		if (i % 10 == 7)
			cout << "qiaozhuozi" << endl;
		else if (i / 10 == 7)
			cout << "qiaozhuozi" << endl;
		else if (i % 7 == 0)
			cout << "qiaozhuozi" << endl;
		else
			cout << i << endl;
	}
	return 0;

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/854309.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号