c++ - 快乐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;
}
解决方案
推荐阅读
- mongodb - 从 Scala 中的 mongoDB 读取
- python - 字符串 '\' 替换为 '/'
- python - 使用 Python 类在属性更新时触发程序终止
- javascript - 第一次安装组件后 componentDidUpdate 未触发(React)
- ios - 技术上是否可以截取 iOS 音频以添加 DSP 声音过滤?
- python - ValueError:logits 和标签必须具有相同的形状 ((None, 14) vs (None, 1))
- mysql - 计算 mysql group 上未重复的元素
- spring - 弹簧和不同的方式来避免使用新的
- google-cloud-platform - 如何在 GCP memorystore redis 实例中安装 redisearch 模块
- kotlin - 当工作本身包含另一个工作时,如何等待工作加入?- 科特林