C++入门教程

例子

小猪体重的比较(if else语句)

int main21()
{
	int a;
	int b;
	int c;
	cout << "请输入小猪a的体重\n"<<endl;
	cin >> a;
	cout << "请输入小猪b的体重\n"<< endl;
	cin >> b;
	cout << "请输入小猪c的体重\n"<< endl;
	cin >> c;

	cout << "小猪a的体重为" << a << endl;
	cout << "小猪b的体重为" << b << endl;
	cout << "小猪c的体重为" << c << endl;

	if (a > b) 
	{
		if (a > c)
		{
			cout << "最重的小猪为a" << endl;
		}
		else cout << "最重的小猪为c" << endl;
	}
	else
	{
		if (b > c)
		{
			cout << "最重的小猪为b" << endl;

		}
		else cout << "最重的小猪为c" << endl;
	}


	system("pause");
	return 0;
}

水仙花数

do while 循环案例

案例描述:水仙花数是指一个三位数,他的每个位上的数字的三次幂之和等于他本身
利用do while 循环求出所有水仙花数
例如13 +53 +33 =153。
获取个位189%=9;
获取十位189/10=15(C++中自动舍去小数);15%10=5。

int main() 
{
	int a=100;
	do 
	{
	  int b = a%10;
	  int c = a / 10 % 10;
	  int d = a / 100;
	  if (b*b*b+c*c*c+d*d*d==a)
	  {
		  cout << a << endl;
	  }	
		a++;
	} while (a < 1000);
 
	system("pause");
	return 0;
}

敲桌子(逢7过)

案例描述;从1开始数到数字100,如果数字各位含有7,或者是7的倍数,打印敲桌子,其余数字直接打印输出。

int main()

{
	for (int a=1; a<100 ; a++)
	{
		if (a % 7 == 0)
		{
			cout << "敲桌子" << endl;
		}
		else if (a % 10 == 7)
		{
			cout << "敲桌子" << endl;
		}
		else cout << a << endl;
		//更简单的写法
		/*
		for (int a=1; a<100 ; a++)
	   {
		 if (a % 7 == 0||a % 10 == 7)
		 {
		 	cout << "敲桌子" << endl;
		  }
		else cout << a << endl;
		*/
	}
	system("pause");
	return 0;
}

乘法口诀表

int main() 
{
	//i为行数,j为列数,当列数小于等于行数时输出
	for (int i = 1; i < 10;i++)
	{
		for (int j = 1; j <= i; j++)
		{
			cout << i << "*" << j << "=" << i * j<<"  ";

		}cout << endl;
	}
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值