问题 B: Balanced Neighbors(思维)

时间限制: 1 Sec 内存限制: 128 MB
提交: 72 解决: 37
[提交] [状态] [命题人:admin]
题目描述
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.

先画出完全图,发现删边规律


#include<iostream>
using namespace std;
int dp[109][109];
int a[1009];
 
int main(){
	int n;
	scanf("%d",&n);
	if(n%2==0){
		cout<<n*(n-1)/2-n/2<<endl;
		for(int i=1;i<=n;i++){
			dp[i][n-i+1]=1;
		}
	}
	else{
		cout<<n*(n-1)/2-(n-1)/2<<endl;
		for(int i=1;i<=n-1;i++){
			dp[i][n-i]=1;
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			if(dp[i][j]==0) printf("%d %d\n",i,j);
		}
	}
} 
全部评论

相关推荐

11-28 17:58
门头沟学院 Java
美团 JAVA开发 n×15.5
牛客786276759号:百度现在晋升很难的 而且云这块的业务没美团好 你看百度股价都跌成啥样了
点赞 评论 收藏
分享
整顿职场的柯基很威猛:这种不可怕,最可怕的是夹在一帮名校里的二本选手,人家才是最稳的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务