第五章:循环

//练习题
//1:抽奖时要求在1-10之间10个不同的随机数,编写一个程序,每次运行时生成5个抽奖选项
////////////////////////////////////////////////////////////////////////////////////////////////
/*
#include<stdlib.h>//包含rand()随机生成函数;
#include<time.h> //包含time()函数,产生随机时间种子;
#include<iomanip>
#include<iostream>
using namespace std ;
int main ()
{
    cout<<"5个奖项:"<<endl;
    int arr[10] = {-1} ;
    int num = 0 ,count = 0;     //num为每次生成随机数,count作为一个标记统计是否有重复,当 count > N时无重复,赋值!
    srand((unsigned)time (0));

    for (int i = 1 ; i <= 6 ; i++)
    {
        for (int j = 0 , N = 0 ; j < 10; )          //j记录每次的要接受无重复值的数组下标 ,N 记录已经完成赋值的数值个数
        {
            num = rand () % 10+ 1;
            for (int k = 0 ; k <= N ; k++)
            {
                if (arr[k] == num)          //有重复结束循环,此时count一定小于等于N;
                    break;
                else
                    count++;                //无重复标记加1;
            }       
            if (count > N )
            {
                arr[j] = num;
                N++;
                j++;
                count = 0;          //赋值完毕,标记count要重置为0,从arr[0]开始重新循环判断!
            }
            else
                count = 0;          //出现重复即count <= N ;那么同样需要把count置为零,以便下次重新统计!
        }   

        cout<<"第"<<i<<"组合为:";
        for (int i  = 0; i < 10 ; i++ )
        {
            cout<<setw(3)<<arr[i];
        }
        cout<<endl;
    }

    system ("pause");
    return 0;
}

*/
//2:在1-10之间选择4个数(这4个数有用户输入) 然后自动输出这4个数的所有排序
////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include<iomanip>//包含格式控制字符
#include <conio.h>//包含getch()函数,按任意键继续!
using namespace std;
const int N = 4;
static int count = 1; 
bool pan (int arr[] , int begin ,int end)           //只有不相同才能交换
{
     for (int i = begin; i < end; i++) 
    {
        if (arr[i] == arr[end])  
             return false;  
     }
       return true; 
}
void perm (int arr[] , int begin ,int end)      //递归求排列,
{
    if (begin == end)               
    {
        cout<<"第"<<setw(3)<<count<<"组排列为:";

        for (int i = 0 ;i <= end ; i++)
        {
            cout<<arr[i]<<" ";
        }
        cout<<endl;
        count++;
    }
    else
    {
        for (int i = begin ; i <= end ; i++)        //当 i++增加到 i = end -1 时进入后,退出本次循环返回上一层的 i + 1;
        {
            if ( pan (arr , begin , i) )
            {
                swap (arr[begin] , arr[i]);
                perm (arr , begin + 1 , end);   //当 begin = end -1 时,进入此处,输出一次的排列,推出本次循环返回上一次i + 1;
                swap (arr[begin] , arr[i]);
            }
        }
    }
}
void swap (int &a , int &b)
{
    int temp = a ;
    a = b ;
    b = temp ;
}

int main ()
{
    cout<<"输入要排列的"<<N<<"个数字:";
    int arr[N] = {0};
    for (int i = 0 ;i < N; i++)
    {
        cin>>arr[i];
    }
    cout<<endl;
    for (int i = 0 ;i < N; i++)
    {
        cout<<arr[i];
    }
    cout<<"的全排列为:\n";
    perm (arr , 0 , N - 1);

    system ("pause");
}
//3:创建一个程序,输出8个随机大小写字母或数字组成的密码
/*
#include<stdlib.h>//包含rand()随机生成函数;
#include<time.h> //包含time()函数,产生随机时间种子;
#include<iostream>
using namespace std;
int main ()
{
    cout<<"产生一个n位的随机数(由数字和大小写字母构成) n = " ;
    int n = 0;
    cin>>n ;

    int y = 0 , x = 0 , temp = 0 ;
    srand (time (0)) ;
    cout<<endl<<n<<"位随机数为:" ;
    for (int i = 1 ; i <= n ; i++)
    {
        while (1)
        {
            y = rand () ;
            temp = y;
            if ( (y >= 48 && y <= 57) || (y >= 65 && y <= 90) || (y >=97 && y <= 122) )
            {
                y = temp ;
                break;
            }
        }

        if (y >= 48 && y <= 57)
        {
            x = y % (57 - 48 + 1) + 48 ;
        }
        else if (y >= 65 && y <= 90)
        {
            x = y % (90 - 65 + 1) + 65 ;
        }
        else
        {
            x = y % (122 - 97 + 1) + 97 ;
        }

        cout<<char(x) ;
    }

    cout<<endl<<endl;
    system ("pause") ;
}

*/
//4:编写一个程序,输出1到用户输入的数字之间的所有奇数的平方
////////////////////////////////////////////////////////////////////////////////////

/*
#include<iostream>
using namespace std ;
int main ()
{
    cout<<"输入一个数计算1~X之间的所有奇数的平方:" ;
    double x = 0 ;
    cin>>x ;
    for (int i = 1 ; i <= x ; i++)
    {
        if ( i % 2  == 1)
            cout<<i<<"的平方为:"<<i * i<<endl ;
    }

    system ("pause") ;
}

*/

//5:使用while循环累加随机个数的整数和,输出所有数字的总和,和浮点数类型的平均值
//////////////////////////////////////////////////////////////////////////////////////////
/*
#include<iostream>
using namespace std ;
int main ()
{
    double x = 0 , fsum = 0 ,sum = 0 ;
    int y = 0 , z = 0 ;
    cout<<"随机输入n个数按0结束:"<<endl ;
    while (1)
    {
        cin>>x ;
        if (x == 0){break ;}
        sum += x ;
        y = int(x) ;                        //此处强制类型转换后不改变原来的数据类型!例如X=3.85转换以后X还是原来的值3.85,但是y = 3;
        if (x - y != 0)     //判断是否为浮点数
        {
            z++ ;
            fsum += x ;
        }
    }
    cout<<"sum = "<<sum<<endl ;
    cout<<"浮点数的平均数为:"<<fsum / z <<endl ;

    system ("pause");

}

总结:
    强制转换不改变原来的数据类型,也不进行四舍五入,处理;
    */

//6:创建一个程序,它使用do--while循环计算用户在一行上输入的非空白字符的个数,
//在第一次遇到输入中的#字符时,停止记数
//////////////////////////////////////////////////////////////////////////////////////////
/*
#include<iostream>
using namespace std ;
int main ()
{
    char x = 0 ;
    int i = 0 ;
    do
    {
        cin>>x ;
        if (x == '#')
        {   break ; }
        i++ ;
    }while (1);

    cout<<"字符的个数为:"<<i<<endl;

    system ("pause");
}
*/



//7:创建一个程序,循环25次,打印出1到10的数字,和20到25的数字
////////////////////////////////////////////////////////////////////////////////////////////////
/*

#include<iostream>
using namespace std ;
int main ()
{
    cout<<"1~10数字为:" ;
    for (int i = 1 ; i <= 10 ; i++)
    {
        cout<<i<<" " ;
    }
    cout<<endl ;
    cout<<"20~25数字为:" ;
    for (int i = 20 ; i <= 25 ;i++)
    {
        cout<<i<<" " ;
    }
    cout<<endl ;
    system ("pause");
}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值