首页 > 解决方案 > 快乐Nr(i); 没有按照我的理解运行

问题描述

编写一个带有函数 main () 并选择函数菜单的程序:

为彩票/最多 100 个/六位数字生成一个编程随机数生成器数据,并将它们存储在一个数组中

-Overwrite 生成一个新数组,并按升序对该数组进行排序并显示输出

- 计数并显示所有“快乐”六位数彩票的输出和数量/前3位数字之和等于后三位的数字/

- 保存在数组中并显示下载的“幸运”彩票的输出序号

最后两个步骤有问题。你能解释一下如何正确地做吗?

#include <cstdlib>
#include <ctime>
#include <cstring>
#include <iostream>

using namespace std;

int numbers;
int array[100];

void generator()
{
   srand((unsigned)time(0));
   for(int i = 0; i < 100; i++)
   {
       numbers  = (rand() % 900000) + 100000;
       array[i] = numbers;
       cout << numbers << endl;
   }
}

void sorted_list()
{
      int i = 100, a, b, c;
      for (a = 0; a < i - 1; a++)
      {
          for(b = 1; b < i; b++)
          {
              if (array[b] < array[b - 1])
              {
                  c = array[b];
                  array[b]= array[b - 1];
                  array[b - 1]=c;
              }
           }
      }

      for (a = 0; a < i; a++)
      {
          cout << a << ": " << array[a] << "\n";
      }
}


bool happyNr(int a){
    if (a<100000 || a>999999) return false;
    int half1=0, half2=0;
    for (int i=0;i<6;i++){
        if (i<3) {half1+=a%10; a=a/10;}
        else {half2+=a%10; a=a/10;}
    }
    return half1==half2;
}

int menu()
{
    int choice;
    cout<<"\n_______________MENU_______________";
    cout<<"\n 1. Generate random numbers";
    cout<<"\n 2. Sorted array";
    cout<<"\n 3. Happy numbers";
    cout<<"\n 4. Exit";
     do
      {
         cout<<"\n Choice: ";
         cin>>choice;
      }while(choice<1||choice>4);

    return(choice);
}
int main()
{
int i;
do
    {
       i=menu();
       switch(i)
          {
            case 1: generator();break;
            case 2: sorted_list();break;
            case 3: happyNr(i);break;
          }
   }
while(i!=4);
return 0;
}

标签: c++

解决方案


推荐阅读