题目描述
You are given an integer N. Build an undirected graph with N vertices with indices 1 to N that satisfies the following two conditions:
·The graph is simple and connected.
·There exists an integer S such that, for every vertex, the sum of the indices of the vertices adjacent to that vertex is S.
It can be proved that at least one such graph exists under the constraints of this problem.
Constraints
·All values in input are integers.
·3≤N≤100
输入
Input is given from Standard Input in the following format:
N
输出
In the first line, print the number of edges, M, in the graph you made. In the i-th of the following M lines, print two integers ai and bi, representing the endpoints of the i-th edge.
The output will be judged correct if the graph satisfies the conditions.
样例输入
3
样例输出
2
1 3
2 3
提示
For every vertex, the sum of the indices of the vertices adjacent to that vertex is 3.
思路
先建立一个1到n的完全图,消边即可得到规律,当n为偶数时,总边数为n*(n-2)/2,当i+j == n+1时消边,当n为奇数时,总边数为(n-1)*(n-1)/2,当i+j ==n时消边。
代码实现
#include<bits/stdc++.h>
using namespace std;
const int N=105;
const int mod=1000000007;
typedef long long ll;
int n;
int main()
{
scanf("%d",&n);
if(n&1)
{
printf("%d\n",(n-1)*(n-1)/2);
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
if((i+j)!=n) printf("%d %d\n",i,j);
}
}
}
else
{
printf("%d\n",n*(n-2)/2);
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
if((i+j)!=(n+1)) printf("%d %d\n",i,j);
}
}
}
return 0;
}