C++代码笔记

猜数字游戏

系统随机生成一个1到100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果猜对恭喜玩家胜利,并且退出游戏。

错解

#include<iostream>
#include<cstdlib>
using namespace std;
int main() 
{
	int num = rand() %100+1;//生成0-100的随机数
	int a=0;
	cout << "请输入猜测的数字: "<<endl;
	cin >> a;
	if(a>num)
	{
		cout<<"数字大了"<<endl; 
	}
	else if(a<num)
	{
		cout<<"数字小了"<<endl; 
	}
	else(a=num)
	{
		cout<<"恭喜你,猜对了"<<endl; 
	}
	system("pause");
	return 0;

	}

分析:1;while循环

2:随机数种子

3:循环跳出的条件

if嵌套的循环语句中,最后一个else没有(),切记

正解

#include<iostream>
#include<cstdlib>
#include<ctime> //随机数种子的头文件 
using namespace std;
int main() 
{
	srand(time(NULL));//防止每次生成的随机数都一样 
	int num = rand() %100+1;//生成随机数种子 
	int a=0;
//	cout<< num<<endl;
	cout << "请输入猜测的数字: "<<endl;
		while(1)
	{
		cin >> a;//必须在循环以内才行 
		if(a>num)
	   {
		cout<<"数字大了"<<endl; 
	    }
	else if(a<num)
    	{
		cout<<"数字小了"<<endl; 
	    }
	else
    	{
		cout<<"恭喜你,猜对了"<<endl; 
		break;//break好像在else循环外和循环内好像都没有什么影响 
	    }
	 	}
	system("pause");
	return 0;
	}
水仙花

水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身,比如:1^3 + 5^3+ 3^3 = 153

错解

#include<iostream>
using namespace std;
int main()
{
	int num =100;
	do
	{
	 int a=0;
   	 int b=0;
	 int c=0;
	 a=num%100;
	 b=num/10%10;
	 c=num/100;
	 if(a*a*a+b*b*b+c*c*c==num)
	 {
		cout<<num<<endl;
   	 }
	 else
	 {
		num++;
	 }	
	}
	while(num<1000);
	system("pause");
	return 0;
 } 

正解

#include<iostream>
using namespace std;  //指调用命名空间std内定义的所有标识符
#include<ctime>  //头文件,用于C++字符串

int main()
{
	int num = 100;
	do
	{
		int a = 0;
		int b = 0;
		int c = 0;
		int d = 0;
		a = num % 10;
		b = num / 10 % 10;
		c = num / 100;
		d = a*a*a + b*b*b + c*c*c;
		if (d == num)
		{
			cout << num << endl;
		}
		num++;
	} while (num < 1000);
	system("pause");  //让程序暂停一下,然后按任意键继续
	return 0;
}
敲桌子

案例描述:从1开始数到数字100, 如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出

正解

// An highlighted block
#include<iostream>
using namespace std;  //指调用命名空间std内定义的所有标识符
#include<ctime>  //头文件,用于C++字符串

int main()
{
	//
	int i = 0;

	for (i = 1; i < 100; i++)
	{
		if (i % 7 == 0)
		{
			cout << "qiaozhuozi" << endl;
		}
		else if (i % 10 == 7)
		{
			cout << "qiaozhuozi" << endl;
		}
		else if (i / 10 % 10 == 7)
		{
			cout << "qiaozhuozi" << endl;
		}
		else
		{
			cout << i << endl;
		}
		
	}
	
	system("pause");  //让程序暂停一下,然后按任意键继续
	return 0;//表明程序正常退出,返回到主程序继续往下执行
}
乘法口诀
#include<iostream>
using namespace std;  //指调用命名空间std内定义的所有标识符
#include<ctime>  //头文件,用于C++字符串
int main()
{
	int i = 0;
	int j = 0;
	for (i = 1; i < 10; i++)
	{
		for (j = 1; j <= i; j++)
		{
			cout << j << "*" << i << "=" << j * i << "  ";
           // cout << "i*j=" << i*j<<" "<<endl;错误的写法
		}
		cout << endl;
	}
	
	system("pause");  //让程序暂停一下,然后按任意键继续
	return 0;//表明程序正常退出,返回到主程序继续往下执行
}

在循环中的i*j中需要特别注意

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值