枚举
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<queue> 7 #include<vector> 8 using namespace std; 9 int w[110][110],o[110]; 10 int main() 11 { 12 int i,j,k,n,m; 13 scanf("%d%d%d",&n,&k,&m); 14 int t = 1; 15 for(i = 1 ; i <= n ; i++) 16 { 17 if(t>k) 18 t = 1; 19 o[i] = t; 20 printf("%d ",t); 21 t++; 22 } 23 int tt=0,a=1; 24 while(1) 25 { 26 if(a<=n) 27 { 28 if(a<n) 29 { 30 printf("%d %d ",a,a+1); 31 w[a][a+1] = 1; 32 w[a+1][a] = 1; 33 a+=2; 34 } 35 else 36 { 37 printf("%d %d ",n-1,n); 38 w[n-1][n] =1; 39 w[n][n-1] = 1; 40 a++; 41 } 42 tt++; 43 } 44 else 45 { 46 for(i = 1; i <= n ;i++) 47 { 48 for(j = 1; j <= n ; j++) 49 if(i!=j&&!w[i][j]&&o[i]!=o[j]) 50 { 51 printf("%d %d ",i,j); 52 w[i][j] = 1; 53 w[j][i] = 1; 54 tt++; 55 if(tt==m) 56 break; 57 } 58 if(tt==m) 59 break; 60 } 61 } 62 if(tt==m) 63 break; 64 } 65 return 0; 66 }