第五题那个我是用VC6.0写的,对于正整数都可以,没有考虑其他的!
仿佛是第六题吧!帮忙看一下,如果有冗余的地方还望多提点一下
希望看起不会太痛苦! --VC6.0
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
cout <<"Enter a number : ";
int number;
cin >>number;
int total = number * number;
int i = 0;
int j = 0;
//(1):
for (i = 0 ; i < number ; ++i)
{
for (j = 0 ; j < number ; ++j)
{
cout <<setw(5)<<total;
total -= 1;
}
cout <<endl;
}
cout <<endl;
//(2)
i = 0;
j = 0;
int num = 1;
vector< vector<int> > a;
a.resize(number);
for (int x = 0; x < number; ++x)
a[x].resize(number);
while(num <= number * number)
{
if(i == 0 || j == number)
{
if(j == number){
i+=1;
j--;
}
for(; j >= 0 && i != number; --j){
a[i][j] = num;
num++;
i++;
}
++j;
}
else if (j == 0 || i == number)
{
if (i == number){
j+=1;
i--;
}
for(; i >= 0 && j != number; --i){
a[i][j] = num;
num++;
j++;
}
++i;
}
}
for ( i = 0 ; i < number ; ++i)
{
for (int j = 0 ; j < number ; ++j)
cout <<setw(5)<<a[i][j];
cout <<endl;
}
cout <<endl;
//(3)
i = 0;
j = 0;
num = 0;
vector< vector<int> > b;
b.resize(number);
for (int y = 0; y < number; ++y)
b[y].resize(number);
int temp1 = 0;
int temp2 = number - 1;
for (int t = 0; t <= number/2; t++, temp1++,temp2--,++i,++j)
{
for(i = temp1; i < temp2; i++)
b[i][j] = ++num;
for(j = temp1; j < temp2; j++)
b[i][j] = ++num;
for(i = temp2; i > temp1; i--)
b[i][j] = ++num;
for(j = temp2; j > temp1; j--)
b[i][j] = ++num;
if(temp1 == temp2)
b[temp1][temp2] = ++num;
}
for ( i = 0 ; i < number ; ++i)
{
for (int j = 0 ; j < number ; ++j)
cout <<setw(5)<<b[i][j];
cout <<endl;
}
cout <<endl;
return 0;
}
3次就够了吧
第一次 a,b,c 对 d,e,f 若平衡……(简单)
若不平衡(如果左边重)(如果右边重同样道理,因为题目是对称的)
第二次 b,c,d对e,f,g
若平衡 则a偏重,问题解决
若不平衡(如果仍然是左边重)则是b,或c偏重,再称一次就解决了
(如果是左边轻)则是d偏轻,问题解决