L1-095 分寝室

暴力枚举。

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int res1,res2;
int main(){
	int a,b,c;//女生 男生 寝室数
	cin>>a>>b>>c;
	int minsub = inf;
	for(int i=2;i<a;i++){//i表示女生几人间 
		if(a % i == 0 && c-a/i > 0 && b % (c-a/i)==0){
		    int female = a/i;//女生需要几间 
		    int male = c-female;//男生需要几间 
		    int ns = i;//女生几人间
			int nans = b / (c-a/i);//男生几人间 
		    if(abs(ns - nans)<minsub){//人数差 
		    	minsub = abs(ns - nans);
		    	res1=female;
		    	res2=male;
			} 
		}
    }
    if(minsub==inf){
    	cout << "No Solution" << '\n';
	}else{
		cout << res1 << " " << res2;
	}
	return 0;
}
posted @ 2024-04-02 10:02  YuKiCheng  阅读(75)  评论(0编辑  收藏  举报