یک جدول n × m n×m داریم که در خانهی سطر i iام، ستون j jام آن عدد حقیقی x i , j x i,j …
- User
یک جدول n × m n×m داریم که در خانهی سطر i iام، ستون j jام آن عدد حقیقی x i , j x i,j نوشته شده است. باید هر خانهی این جدول مثل x i , j x i,j را به ⌊ x i , j ⌋ ⌊x i,j ⌋ یا ⌈ x i , j ⌉ ⌈x i,j ⌉ تبدیل کنیم. باید این تغییرات را طوری انجام دهیم به طوری که جمع سطرها و ستونها تغییر نکند.
از شما میخواهیم برنامهای بنویسید که تشخیص دهد انجام این کار شدنی است یا نه و در صورتی که این کار شدنی است، یک روش انجام این کار ارائه دهید.
ورودی در سطر اول ورودی، عدد صحیح و مثبت t t آمده که تعداد تستها را نشان میدهد. 1 ≤ t ≤ 100 1≤t≤100
در سطر اول هر تست، دو عدد صحیح n n و m m داده میشود که به ترتیب تعداد سطرها و ستونها جدول را نشان میدهد. 1 ≤ n , m ≤ 100 1≤n,m≤100
در n n سطر بعدی، در هر کدام m m عدد حقیقی که با فاصله از هم جدا شدهاند داده میشود. عدد j jام در سطر i iام همان x i , j x i,j است. هر عدد با دقت دقیقاً ۳ رقم بعد از اعشار داده میشود. − 1000 < x i , j < 1000 −1000<x i,j <1000
خروجی برای هر تست، در سطر اول خروجی، در صورتی که انجام عملیاتها ممکن است، YESو در غیر این صورت NO را چاپ کنید. برای حالتهایی که انجام عملیاتها ممکن است، یک جدول n × m n×m چاپ کنید که عدد سطر i iام ستون j jام آن برابر 0 یا 1 است عدد سطر i iام ستون j jام به ترتیب نشان دهندهی انتخاب ⌊ x i , j ⌋ ⌊x i,j ⌋ یا ⌈ x i , j ⌉ ⌈x i,j ⌉ است.
اگر چند حالت برای رسیدن به جواب وجود دارد، یکی را به دلخواه انتخاب کنید.
مثالها ورودی نمونه ۱ 3 2 3 3.000 4.000 1.000 5.000 3.000 1.000 3 2 0.500 0.500 0.500 0.500 0.500 1.000 3 3 5.000 3.333 1.667 2.667 0.000 3.333 3.333 2.667 3.000 Plain text خروجی نمونه ۱ YES 0 0 0 0 0 0 NO YES 0 0 1 1 1 0 0 1 1 write in c++
- Assistant
To solve this problem in C++, you can follow these steps to determine if it is possible and provide a solution:
-
Understand the Problem:
- You have a
n x mtable filled with real numbersx(i, j). - You need to round each number to either its floor or ceiling value such that the sum of numbers in each row and column remains unchanged.
- You have a
-
Approach:
- For each real number
x(i, j), let's define:floor(x(i, j))=a(i, j)ceiling(x(i, j))=b(i, j)
- Suppose you choose either 0 or 1 for each
x(i, j), indicating whether you chose floor or ceiling. - You are required that the sum of the floor or ceiling values chosen for each row and column must match exactly the integer part of the original sums of that row/column.
- For each real number
-
Implementation Steps:
Let's write the code for this logic using C++:
#include <iostream> #include <vector> #include <cmath> using namespace std; void solveCase() { int n, m; cin >> n >> m; vector<vector<double>> x(n, vector<double>(m)); vector<vector<int>> result(n, vector<int>(m)); vector<int> rowSum(n, 0), colSum(m, 0); double rowRemainderSum = 0.0, colRemainderSum = 0.0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> x[i][j]; rowSum[i] += floor(x[i][j]); colSum[j] += floor(x[i][j]); rowRemainderSum += x[i][j] - floor(x[i][j]); colRemainderSum += x[i][j] - floor(x[i][j]); } } // Check if the total remainder sums match an integer if (round(rowRemainderSum) != round(colRemainderSum)) { cout << "NO" << endl; return; } int totalRemainder = round(rowRemainderSum); vector<int> rowAdjustment(n, 0), colAdjustment(m, 0); for (int i = 0; i < n; ++i) { rowAdjustment[i] = rowSum[i] + floor(rowRemainderSum / n) - floor(rowRemainderSum); } for (int j = 0; j < m; ++j) { colAdjustment[j] = colSum[j] + floor(colRemainderSum / m) - floor(colRemainderSum); } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { double diff = x[i][j] - floor(x[i][j]); if (diff > 0.5 || (diff == 0.5 && totalRemainder > 0)) { result[i][j] = 1; --totalRemainder; } else { result[i][j] = 0; } } } if (totalRemainder != 0) { cout << "NO" << endl; return; } cout << "YES" << endl; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cout << result[i][j] << " "; } cout << endl; } } int main() { int t; cin >> t; while (t--) { solveCase(); } return 0; }Explanation:
- For each test case, read the matrix dimensions and the matrix values.
- Calculate the sum of floor values and the remainder sum for all rows and columns.
- Adjust floor values with the decision of choosing ceiling if it helps in balancing the required sums.
- If the cumulative remainders can be adjusted correctly across the matrix rows and columns, print "YES" and the resultant floor/ceiling decisions.
- Otherwise, print "NO".
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.