题解 | 球格模型(简单版)
球格模型(简单版)
https://www.nowcoder.com/practice/fada102a84ec459ea93e3db4918c0f04
// 活动地址: 牛客春招刷题训练营 - 编程打卡活动 #include<iostream> #include<algorithm> #include<cmath> #define int long long using namespace std; // int a[3300][4220]; // 煮波有话要说 这题是的bt 改bug改了一万年 结果发现是数组开小了 // 其他的没什么问题 但其实好像可以 不用数组也可以 也可以用一维数组 void solve(){ int n,m,k; cin>>n>>m>>k; if(max(n,m)>k){ cout<<"-1"<<"\n"; return ; } // 算了还是写下吧 int d = k-max(n,m)+1; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(i==j){ if(i==0)cout<<d; else cout<<1; } else{ if(n>m&&i>=m&&j==m-1)cout<<1; else if(n<m&&j>=n&&i==n-1)cout<<1; else cout<<0; } cout<<" "; } cout<<endl; } // for(int i=1;i<=min(n,m);i++){ // a[i][i]=1; // } // if(n<m){ // for(int i=n+1;i<=m;i++){ // a[n][i]=1; // } // a[1][1]+=k-m; // } // else{ // for(int i=m+1;i<=n;i++){ // a[i][m]=1; // } // a[1][1]+=k-n; // } // for(int i=1;i<=n;i++){ // for(int j=1;j<=m;j++){ // cout<<a[i][j]; // if(j!=m)cout<<" "; // } // cout<<endl; // //cout<<endl; // } } signed main(){ int T=1; ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); // cin>>T; while(T--){ solve(); } return 0; } // 活动地址: 牛客春招刷题训练营 - 编程打卡活动 // #include<iostream> // #include<algorithm> // #include<cmath> // #define int long long // using namespace std; // int a[1010][1002]; // void solve(){ // int n, m, k; // cin >> n >> m >> k; // // Step 1: If there aren't enough balls to put at least one in each row and column // if (max(m,n) > k) { // cout << "-1" << endl; // return; // } // // Step 2: Initialize the grid with 0s // for (int i = 1; i <= n; i++) { // for (int j = 1; j <= m; j++) { // a[i][j] = 0; // } // } // // Step 3: Distribute one ball per row and column // for (int i = 1; i <= min(n, m); i++) { // a[i][i] = 1; // Put one ball in the diagonal cells // } // // Step 4: Fill remaining balls in the grid // k -= (n + m - 1); // Subtract the balls placed in the diagonal and first row/column // if (n < m) { // for (int i = n + 1; i <= m && k > 0; i++) { // a[n][i] = 1; // Place remaining balls in the last row // k--; // } // } else { // for (int i = m + 1; i <= n && k > 0; i++) { // a[i][m] = 1; // Place remaining balls in the last column // k--; // } // } // // Step 5: If there are still remaining balls, add them to the first cell // a[1][1] += k; // // Step 6: Output the grid // for (int i = 1; i <= n; i++) { // for (int j = 1; j <= m; j++) { // cout << a[i][j] << " "; // } // cout << endl; // } // } // signed main(){ // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); // solve(); // return 0; // }