求一个程序:比如说有d1,d2,d3,d4,判断d1+d2+d3+d4是不是偶数,如果是再判断(d2-1)+(d3-1)+(d4-1)是不
2个回答
展开全部
#include <iostream>
using namespace std;
void main()
{
int n, i, j, sum, min, pos;
cout << "n: ";
cin >> n;
int* d = new int[n];
for (i = 0; i != n; ++i)
{
cout << "the " << i + 1 << "number:";
cin >> d[i];
}
min = d[n - 1];
pos = n - 1;
for (i = 0; i != n - 1; ++i)
{
sum = 0;
for (j = i + 1; j != n; ++j)
{
if (j != n - 1)
{
d[j]--;
if (d[j] == 0)
{
cout << "complete!" << endl;
delete []d;
return;
}
sum += d[j];
}
else
{
sum += d[j];
}
}
for (j = i + 1; j != n; ++j)
{
if (min > d[j])
{
min = d[j];
pos = j;
}
}
if (d[pos] != d[n - 1])
{
d[pos] = d[n - 1];
d[n - 1] = min;
}
for (j = i + 1; j != n; ++j) //用来显示是否换对的
{
cout << d[j] << " ";
}
cout << endl;
if (sum % 2)
{
cout << "error!" << endl;
delete []d;
return;
}
}
cout << "error!" << endl;
delete []d;
}
using namespace std;
void main()
{
int n, i, j, sum, min, pos;
cout << "n: ";
cin >> n;
int* d = new int[n];
for (i = 0; i != n; ++i)
{
cout << "the " << i + 1 << "number:";
cin >> d[i];
}
min = d[n - 1];
pos = n - 1;
for (i = 0; i != n - 1; ++i)
{
sum = 0;
for (j = i + 1; j != n; ++j)
{
if (j != n - 1)
{
d[j]--;
if (d[j] == 0)
{
cout << "complete!" << endl;
delete []d;
return;
}
sum += d[j];
}
else
{
sum += d[j];
}
}
for (j = i + 1; j != n; ++j)
{
if (min > d[j])
{
min = d[j];
pos = j;
}
}
if (d[pos] != d[n - 1])
{
d[pos] = d[n - 1];
d[n - 1] = min;
}
for (j = i + 1; j != n; ++j) //用来显示是否换对的
{
cout << d[j] << " ";
}
cout << endl;
if (sum % 2)
{
cout << "error!" << endl;
delete []d;
return;
}
}
cout << "error!" << endl;
delete []d;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询