【贪玩巴斯】入门c++,你只需要敲出这些程序 !(三){一位数组、九九乘法表、猜数字、水仙花数、逛三园逢6过等等...// 2021-03-29

本文详细介绍了C++中的循环控制语句,包括while、do...while和for循环,以及它们在猜数字游戏和打印九九乘法表等场景中的应用。此外,还讲解了一维数组的定义、输出和sizeof运算符的使用,展示了如何通过数组存储和处理数据。
摘要由CSDN通过智能技术生成

//

//  main.cpp

//  _03day

//

//  Created by AchesonD 贪玩巴斯 on 2021/3/21.

//

 

 

 

 

 

/*

 

一、while语句的运用

 

 

//while语句的运用

 

#include<iostream>

using namespace std;

 

int main(){

    int a = 1;

    while (a <= 20) {

        cout << a << endl;

        a++;

    }

    

    system("pause");

    return 0;

}

 

*/

 

 

 

 

 

/*

 

二、while语句的例题——猜数字游戏

 

 

//while循环语句的一个游戏 猜数字

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

 

 

#include<iostream>

using namespace std;

 

//time时间函数包括的头文件

#include<ctime>

 

int main(){

    

    //这句语句是为了添加随机数种子,作用利用当前系统时间生成随机数,防止每次系统的伪随机数都一样(都为42);

    srand((unsigned int)time(NULL));

    

    //int num = rand() % 200; 意思是生成 0 - 199 的一百个随机数。 如果要生成 1 - 200 只需要在生成后面加一个 1

    //这样生成的就是 0 + 1 到 199 + 1 的随机数

    int num = rand() % 200 + 1;

    

    //遍历展示一遍 随机生成的数

    //cout << num << endl;

    

    int s = 0;

 

    while(1)

    {

        

        cout << "请输入玩家您想猜测是数字为(1 - 200): " << endl;

        cin >> s;

        

        if(s == num)

        {

            cout << "恭喜您,猜对啦!" << endl;

            cout << "您的数字为 " << s << " \n系统的数字为 " << num << endl;

            break;

        }

        else if(s < num)

        {

            cout << "温馨提示:您猜测的数字比正确答案小!" << endl;

        }

        else

        {

            cout << "温馨提示:您猜测的数字比正确答案大!" <<endl;

        }

        cout << endl;

            

    }

    system("pause");

    return 0;

}

 

*/

 

 

 

 

/*

 

三、do...while 循环语句的基础语法!

 

//do...while 循环语句

//区别在于会先执行一次循环 再进行判断

 

#include<iostream>

using namespace std;

 

int main(){

    int num = 20;

    

    do

    {

        cout << num << endl;

        num--;

    }while(num>=0);

    

    system("pause");

    return 0;

}

 

*/

 

 

 

/*

 

 

四、do...while语句解决经典问题——水仙花数

 

 

// 水仙花数题目,我们求1000以内的所有水仙花数

//(Narcissistic number)也被称为超完全数字不变数

//(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),

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

 

//1^3 + 5^3+ 3^3 = 153

 

//do...while 语句来求

 

#include<iostream>

using namespace std;

 

int main(){

    

    //三位数 100-999

    int a = 100;

    

    do

    {

        int b,c,d = 0;

        //求个位数 ——  a % 10

        b = a % 10;

        

        //求十位数 —— a / 10 % 10

        c = a / 10 % 10;

        

        //求个位数 —— a / 100

        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;

}

 

 

//正确结果为:153 370 371 407

 

*/

 

 

 

 

/*

 

五、for循环的基础语句

 

 

// for 循环

 

#include<iostream>

using namespace std;

 

int main(){

    

    for (int a=1; a <= 50; a++ )

    {

        cout << a << endl;

    }

    

    system("pause");

    return 0;

}

 

 

*/

 

 

/*

 

六、运用for循环解决酒桌游戏逛三园中的逢6过!

 

 

// 酒桌游戏 逛三园 逢6过

 

//从1开始数到数字200,如果数字个位含有6,或者数字十位含有6,或者该数字是6的倍数,我们打印逢6过,其余数字直接打印输出。

 

#include<iostream>

using namespace std;

 

int main(){

    int a = 0;

 

    for( a=0; a<=200; a++)

    {

        if( a / 10 % 10  == 6 || a % 6 == 0 || a % 10 == 6)

        {

            cout << "逢6过" << endl;

            continue;

        }

        cout << a << endl;

    }

    

    system("pause");

    return 0;

}

 

*/

 

 

 

/*

 

七、学会嵌套循环 打印九九乘法表!

 

 

//嵌套循环

//练习 打印出九九乘法表

 

 

#include "iostream"

using namespace std;

 

int main(){

    

    int a,b = 0;

    

    for ( a=1; a<=9; a++){

        for(b=1; b<=a; b++)

        {

            cout << b << "x" << a << "=" << a * b << "\t";

        }

        cout << endl;

    }

    

    system("pause");

    return 0;

}

*/

 

/*

 

八、break continue goto 语句,三个跳出循环语句的基础语法!

 

 

//三个跳出循环的语句

break

 

// break

//break使用的时机:

//出现在switch条件语句中,作用是终止case并跳出switch

//出现在循环语句中,作用是跳出当前的循环语句

//出现在嵌套循环中,跳出最近的内层循环语句

 

//continue

//跳过本次循环中余下尚未执行的语句,继续执行下一次循环

 

//练习打印输出0-100的所有偶数(不打印奇数)

#include<iostream>

using namespace std;

 

int main() {

    

    for(int i = 0; i <= 100; i++)

    {

        if( (i+1) % 2 == 0)

        {

            cout << i << " 它是奇数!" << endl;

            continue;

        }

        cout << i << endl;

    }

    

    system("pause");

    return 0;

}

 

*/

 

/*

 

//goto语句,一般不用

 

//如果标记的名称存在,执行到goto语句时,会跳转到标记的位置

 

#include<iostream>

using namespace std;

 

int main() {

 

    cout << "你" << endl;

 

    goto zhe;

 

    cout << "是" << endl;

    cout << "他" << endl;

    cout << "的" << endl;

    cout << "妈" << endl;

    

zhe:

    cout << "妈" << endl;

    

    cout << "不加goto 语句的话 原文为: 你是他的妈妈 加了之后变成: 你妈"<<endl;

    

    

    system("pause");

 

    return 0;

}

 

*/

 

 

/*

 

九、一位数组的定义方法

 

//一维数组

//一维数组的定义有三种方式

 

#include<iostream>

using namespace std;

 

int main(){

    

    //方法一: 数据类型 数组名[元素个数];

    int arr[10];

    //再赋值,下标从 0 到 9;

    arr[0] = 1;

    arr[1] = 2;

    arr[2] = 3;

    //后面没有赋值的皆为0;

    //输出方式一:利用下标输出

    cout << arr[0] << endl;

    cout << arr[1] << endl;

    cout << arr[2] << endl;

    

    cout << "\n\n" << endl;

    

    

    

    

    //定义方式二: 数据类型 数组名[元素个数] = {值1,2,3,4,……};

 

    int arr1[10] = {5,4,3,2,1};

    

    //输出方式二:利用循环遍历输出

 

    //sizeof(arr1)/sizeof(arr1[1]) - 1

    //这个语句是利用sizeof算出全部arr的内存大小除以一个arr[0]数组所占内存的大小得到总的arr的个数

    //再减去1 为最后一个数组的下标!

    for(int i = 0; i <= sizeof(arr1)/sizeof(arr1[1]) - 1; i++)

    {

        cout << arr1[i] << endl;

    }

    cout << "\n\n" << endl;

    

    

    

    

    //定义方式三: 数据类型 数组名[] = {值1,2,3,4,……};

 

    int arr2[] = {9,8,7,6,5,4,3};

    cout << "数组长度为:" << sizeof(arr2)/sizeof(arr2[0]) << endl;

    

    for(int i = 0; i <= sizeof(arr2)/sizeof(arr2[0]) - 1; i++)

    {

        cout << arr2[i] << endl;

    }

    

    

    

    system("pause");

    return 0;

}

 

 

*/

 

十、一位数组的用途和sizeof运用

 

 

//一维数组的用途

 

//1.可以统计整个数组在内存中的长度 2.可以获取内存中的首地址

 

#include<iostream>

using namespace std;

 

int main(){

    

    int arr[5] = {100,200,300,400};

    //遍历输出数组

    for(int i = 0; i <= sizeof(arr)/sizeof(arr[0]) - 1 ; i++)

    {

        cout << arr[i] << endl;

    }

    

    cout << "整个数组所占内存为:" << sizeof(arr) << endl;

    cout << "每个元素所占内存为:" << sizeof(arr[0]) << endl;

    cout << "数组元素的个数长度为:" << sizeof(arr)/sizeof(arr[0]) << endl;

    

    //数组不可以赋值!数组名是常量 因此不可以赋值!

    //数组是一段连续的内存空间

    

    

    system("pause");

    return 0;

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贪玩巴斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值