3212 KNice(水题1)

This is a super simple problem. The description is simple, the solution is simple. If you believe so, just read it on. Or if you don't, just pretend that you can't see this one.

We say an element is inside a matrix if it has four neighboring elements in the matrix (Those at the corner have two and on the edge have three). An element inside a matrix is called "nice" when its value equals the sum of its four neighbors. A matrix is called "k-nice" if and only if k of the elements inside the matrix are "nice".

Now given the size of the matrix and the value of k, you are to output any one of the "k-nice" matrix of the given size. It is guaranteed that there is always a solution to every test case.

Input

The first line of the input contains an integer T (1 <= T <= 8500) followed by T test cases. Each case contains three integers n, m, k (2 <= n, m <= 15, 0 <= k <= (n - 2) * (m - 2)) indicating the matrix size n * m and it the "nice"-degree k.

Output

For each test case, output a matrix with n lines each containing m elements separated by a space (no extra space at the end of the line). The absolute value of the elements in the matrix should not be greater than 10000.

Sample Input

2
4 5 3
5 5 3

Sample Output

2 1 3 1 1
4 8 2 6 1
1 1 9 2 9
2 2 4 4 3
0 1 2 3 0
0 4 5 6 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0




附带正确代码和超时代码,求解为何会超时呢?

正确代码:

#include <cstdio>
using namespace std;
int main(){
    int T, n, m, k, i, j;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d%d", &n, &m, &k); 
        k = (n-2)*(m-2) - k;
        for(i = 1; i <= n; i++) {
            printf("0");
            for(j=1;j<=m-2;j++)
                if(k>0)printf(" %d",k--);else printf(" 0");
            printf(" 0\n");
        }
    }
    return 0;
}






超时代码:
#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{
 
int T, n, m, k, i, j; 

 cin>>T;
 
while(T--)
 
{     

    cin>>n>>m>>k;
 
k = (n-2)*(m-2) - k;  

  for(i = 1; i <= n; i++)
 
 {        
 
   cout<<"0 ";
  
 for(j=1;j<=m-2;j++)   
   
       if(k>0)
  
  cout<<k--<<" ";
  
  else   {cout<<"0 "; }
 
   cout<<"0"<<endl;   
     
   } 

  
 return 0;

}
//为什么会不对呢?
原文地址:https://www.cnblogs.com/beibeibao/p/2981389.html