检查数组的所有内容是否在一个数字范围内,没有重复
Check if all content of arrays are between a range of numbers, without duplicates
我需要一种方法来查看用户输入的所有数字是否都是从 1 到 15 的数字。没有重复。
到目前为止我有一个程序可以检查数字是否超过 15 或小于 1,但我不知道如何检查输入数组的每个数字,看看是否已经输入数组了
这是我目前所拥有的。
#include <iostream>
using namespace std;
int main() {
bool somethingBadHappend=false; //booleans like this make it easy for the program to be read
int numbers[15]{};
cout << "enter 15 numbers:";
for (int i = 0; i < 15; i++)
{
cin>>numbers[i]; //entering them into the array
}
if (numbers[i] > 15|| numbers[i]<=0)
{
somethingBadHappend = true;
}
}
if (somethingBadHappend) //see the perfect use of code here?
{
cout<<"NOT GOOD"; // How elegant!
}
else
cout<<"GOOD";
return 0;
}
如果我理解你的问题,你必须再次检查数组以查看重复项。
#include <iostream>
using namespace std;
int main() {
bool somethingBadHappend=false; //booleans like this make it easy for the program to be read
int numbers[15]{};
cout << "enter 15 numbers:";
for (int i = 0; i < 15; i++)
{
cin>>numbers[i]; //entering them into the array
}
//to find values >15 or <1
for (int i = 0; i < 15; i++)
{
if (numbers[i] > 15 || numbers[i]<=0)
{
somethingBadHappend = true;
}
}
//to find the duplicate into array
for (int i = 0; i < 15; i++){
for(int c=0; c<15; c++){
if(i!=c){ // check the different index
if(numbers[i]==numbers[c]){
somethingBadHappend = true; //found duplicate
break;
}
}
}
}
if (somethingBadHappend) //see the perfect use of code here?
{
cout<<"NOT GOOD"; // How elegant!
}
else
cout<<"GOOD";
return 0;
}
我需要一种方法来查看用户输入的所有数字是否都是从 1 到 15 的数字。没有重复。
到目前为止我有一个程序可以检查数字是否超过 15 或小于 1,但我不知道如何检查输入数组的每个数字,看看是否已经输入数组了
这是我目前所拥有的。
#include <iostream>
using namespace std;
int main() {
bool somethingBadHappend=false; //booleans like this make it easy for the program to be read
int numbers[15]{};
cout << "enter 15 numbers:";
for (int i = 0; i < 15; i++)
{
cin>>numbers[i]; //entering them into the array
}
if (numbers[i] > 15|| numbers[i]<=0)
{
somethingBadHappend = true;
}
}
if (somethingBadHappend) //see the perfect use of code here?
{
cout<<"NOT GOOD"; // How elegant!
}
else
cout<<"GOOD";
return 0;
}
如果我理解你的问题,你必须再次检查数组以查看重复项。
#include <iostream>
using namespace std;
int main() {
bool somethingBadHappend=false; //booleans like this make it easy for the program to be read
int numbers[15]{};
cout << "enter 15 numbers:";
for (int i = 0; i < 15; i++)
{
cin>>numbers[i]; //entering them into the array
}
//to find values >15 or <1
for (int i = 0; i < 15; i++)
{
if (numbers[i] > 15 || numbers[i]<=0)
{
somethingBadHappend = true;
}
}
//to find the duplicate into array
for (int i = 0; i < 15; i++){
for(int c=0; c<15; c++){
if(i!=c){ // check the different index
if(numbers[i]==numbers[c]){
somethingBadHappend = true; //found duplicate
break;
}
}
}
}
if (somethingBadHappend) //see the perfect use of code here?
{
cout<<"NOT GOOD"; // How elegant!
}
else
cout<<"GOOD";
return 0;
}