#37454: 解題思路


21s011@ms.mingdao.edu.tw (鄭宇廷)

學校 : 不指定學校
編號 : 238963
來源 : [140.128.156.248]
最後登入時間 :
2024-03-13 16:05:31
c295. APCS-2016-1029-2最大和 -- 2016年10月APCS | From: [60.198.50.243] | 發表日期 : 2023-09-10 15:43

第一行是表達輸入幾行每行有幾個數字,

那你可以利用for迴圈創造出n行樣大小的陣列,

之後排列將最大值相加,

後除以那些最大值若可以除盡的話就將數字印出來。

#include<bits/stdc++.h>
using namespace std;
int a,b;
std::queue<int>q ;
std::queue<int>k ;
signed main(){
    cin>>a>>b;
    
    int arr[b];
    int ans[a]={};
    int c=0;
    for(int i=1;i<=a;i++){
        arr[b]={};
        for(int j=0;j<b;j++){
            cin>>arr[j];
        } sort(arr, arr+b);
        c=c+arr[b - 1];
        k.push(arr[b- 1]);
        
        
        
    }
    while(!k.empty()){
        if(c%k.front()==0){
            q.push(k.front());
            k.pop();
        }else{
            k.pop();
        }
    }
    
    if(q.size()>0){
        cout<<c<<"\n";
        while (!q.empty()) {
            if(q.size()==1){
                cout<<q.front();
                q.pop();
            }else{
              cout<<q.front()<<" ";
              q.pop();
            }
            
        }
    }else{
        cout<<c<<"\n"<<"-1";
    }
    
    
    
}
 
ZeroJudge Forum