构造矩阵【看似高大上,实际水题一道】

版权声明:https://blog.csdn.net/qq_41730082 https://blog.csdn.net/qq_41730082/article/details/84973886

对于一个N × M的整数矩阵A,小Hi知道每一行的整数之和依次是P1, P2, ... PN,每一列的整数整数之和依次是Q1, Q2, ... QM。  

你能构造出一个矩阵A,满足每个元素Aij都是非负的,并且满足上述行列之和吗?

Input

第一行包含两个整数N和M。  

第二行包含N个整数,P1, P2, ... PN。  

第三行包含M个整数,Q1, Q2, ... QM。  

输入保证有解。  

1 ≤ N, M ≤ 100  

1 ≤ Pi, Qi ≤ 100000

Output

输入一个N × M的矩阵,满足题目中的要求。如果有多解,你可以输出任意一个。

Sample Input

3 3  
15 15 15  
15 15 15

Sample Output

8 1 6  
3 5 7  
4 9 2

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 105;
int N, M, P[maxN], Q[maxN], ans[maxN][maxN], now_P[maxN], now_Q[maxN];
void init()
{
    memset(ans, 0, sizeof(ans));
    memset(now_P, 0, sizeof(now_P));
    memset(now_Q, 0, sizeof(now_Q));
}
int main()
{
    while(scanf("%d%d", &N, &M)!=EOF)
    {
        for(int i=1; i<=N; i++) scanf("%d", &P[i]);
        for(int i=1; i<=M; i++) scanf("%d", &Q[i]);
        for(int i=1; i<=N; i++)
        {
            for(int j=1; j<=M; j++)
            {
                ans[i][j] = min(P[i], Q[j]);
                P[i] -= ans[i][j];
                Q[j] -= ans[i][j];
            }
        }
        for(int i=1; i<=N; i++)
        {
            for(int j=1; j<=M; j++)
            {
                printf("%d%c", ans[i][j], j==M?'\n':' ');
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/84973886