C++练习案例(1) --- 三只小猪称体重、猜数字、水仙花数、敲桌子、乘法口诀表、五只小猪称体重、数组元素逆置、考试成绩统计

C++基础入门 — 练习案例

1.三只小猪称体重

说明:有三只小猪分别为A、B、C,分别输入三只小猪的体重,并判断哪种小猪的体重最重。

#include <iostream>
using namespace std;

int main()
{
    int A_Weight = 0;
    int B_Weight = 0;
    int C_Weight = 0;

    cout << "请输入小猪A的体重:" << endl;
    cin >> A_Weight;
    cout << "请输入小猪B的体重:" << endl;
    cin >> B_Weight;
    cout << "请输入小猪C的体重:" << endl;
    cin >> C_Weight;

    char flag = 0;
    int ret = (A_Weight > B_Weight ? A_Weight : B_Weight) > C_Weight ? (A_Weight > B_Weight ? A_Weight : B_Weight) : C_Weight;

    if (ret == A_Weight)
        flag = 'A';
    else if (ret == B_Weight)
        flag = 'B';
    else
        flag = 'C';
    cout << "体重最重的是小猪" << flag << " 体重为:" << ret << endl;
    system("pause");
    return 0;
}
2.猜数字

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

#include <iostream>
#include <ctime>
using namespace std;

void menu()
{
    cout << "***************************" << endl;
    cout << "******* 1.Play 0.Exit *****" << endl;
    cout << "***************************" << endl;
}

void game()
{
    srand((unsigned int)time(NULL));
    int num = rand() % 100 + 1;
    while (1)
    {
        int Guess = 0;
        cout << "请输入猜的数字:>" << endl;
        cin >> Guess;
        if (Guess > num)
            cout << "猜大了" << endl;
        else if (Guess < num)
            cout << "猜小了" << endl;
        else {
            cout << "恭喜胜利" << endl;
            break;
        }
    }
}

int main()
{
    int input = 0;
    do
    {
        menu();
        cout << "请选择游戏状态:>" << endl;
        cin >> input;
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            cout << "退出游戏" << endl;
            break;
        default:
            break;
        }
    }while (input);
    system("pause");
    return 0;
}

3.水仙花数

说明:水仙花数指一个三位数,它的每个位上的数字的三次幂之和等于它本身。

例:1^3 + 5^3 + 3^3 = 153

#include <iostream>
using namespace std;

int main()
{
    int num = 100;
    do
    {
        int one = num % 10;
        int ten = num / 10 % 10;
        int hundred = num / 100;
        if (one * one * one + ten * ten * ten + hundred * hundred * hundred == num)
            cout << num << endl;
        num++;
    } while (num < 1000);
    system("pause");
    return 0;
}
4.敲桌子

说明:从1开始到数字100,数字中个位或十位含7,或者是7的倍数,打印敲桌子,其余打印数字。

#include <iostream>
using namespace std;
int main()
{
    int i = 0;
    for (i = 1; i <= 100; i++)
    {
        if (i % 10 == 7)
            cout << "敲桌子" << endl;
        else if (i / 10 == 7)
            cout << "敲桌子" << endl;
        else if (i % 7 == 0)
            cout << "敲桌子" << endl;
        else
            cout << i << endl;
    }
    system("pause");
    return 0;
}
5.乘法口诀表

说明:九九乘法表。

#include <iostream>
using namespace std;

int main()
{
    int i = 0;
    int j = 0;
    for (i = 1; i < 10; i++)
    {
        for (j = 1; j <= i; j++)
        {
            cout << j << "*" << i << "=" << i * j << "\t";
        }
        cout << endl;
    }
    system("pause");
    return 0;
}
6.五只小猪称体重

说明:在一个数组中记录了五只小猪的体重,如:int arr[5] = {300,200,420,360,280},找出并打印最重的小猪体重。

#include <iostream>
using namespace std;

int main()
{
	int arr[5] = { 300,200,420,360,280 };
	int max = arr[0];
	for (int i = 0; i < 5; i++)
	{
		if (arr[i] > max)
		{
			max = arr[i];
		}
	}
	cout << max << endl;
	system("pause");
	return 0;
}
7.数组元素逆置

说明:声明一个5个元素的数组,将数组元素逆置,如:数组元素为:1,2,3,4,5;逆置后为:5,4,3,2,1。

#include <iostream>
using namespace std;

int main()
{
	int arr[] = { 1,2,3,4,5 };
	int len = sizeof(arr) / sizeof(arr[0]);
	int left = 0;
	int right = len - 1;
	while (left <= right)
	{
		int tmp = arr[left];
		arr[left] = arr[right];
		arr[right] = tmp;

		left++;
		right--;
	}

	for (int i = 0; i < len; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}
8.考试成绩统计

说明:由三名同学(A,B,C),在一个考试中的成绩如下表,分别输出三名同学的总成绩。

语文数学英语
A1009876
B907588
C928473
#include <iostream>
using namespace std;

int main()
{
	int score[3][3] = { {100,98,76}, {90,75,88}, {92,84,73} };
	for (int i = 0; i < 3; i++)
	{
		int sum = 0;
		for (int j = 0; j < 3; j++)
		{
			sum += score[i][j];
		}
		cout << sum << endl;
	}
	system("pause");
	return 0;
}
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值