我用C ++创建程序,检查输入的数字是否为:偶数和正数,偶数和负数,奇数和正数或奇数和负数! 有没有更好的解决方案来检查这个?
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
system("color f0");
int n = 0, A[1337], B[1337];
cout << "Enter number of array members: " << endl;
cin >> n;
cout << "Enter numbers : \n";
for (int i = 0; i < n; i++)
cin >> A[i];
for (int i = 0; i < n; i++)
B[i] = A[i];
for (int i = 0; i < n; i++)
{
if (B[i] > 0 && B[i] % 2 == 0)
cout << B[i] << " pp" << endl;
if (B[i] > 0 && B[i] % 2 != 0)
cout << B[i] << " pn" << endl;
if (B[i] < 0 && B[i] % 2 != 0)
cout << B[i] << " np" << endl;
if (B[i] < 0 && B[i] % 2 == 0)
cout << B[i] << " nn" << endl;
}
cout << endl;
system("pause");
return 0;
}
答案 0 :(得分:0)
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
system("color f0");
int n = 0, A[1337];
cout << "Enter number of array members: " << endl;
cin >> n;
//Make sure n is not bigger than your arrays
if( n > 1337 ) n=1337;
cout << "Enter numbers : \n";
for (int i = 0; i < n; i++)
cin >> A[i];
for (int i = 0; i < n; i++)
{
if( A[i] == 0 )
{
cout << A[i] << " is 0" << endl;
}
else
{
cout << A[i] << " " << (A[i] < 0? "n": "p") << (A[i]%2? "p":"n");
}
}
cout << endl;
system("pause");
return 0;
}