백준 2309번

상인·2023년 1월 30일
0

문제-https://www.acmicpc.net/problem/2309

내 코드

#include <bits/stdc++.h>
using namespace std;

int n = 9,k=2,temp=0,check=0;
int main() {
	vector<int> a;
	int b;
	for(int i=0;i<n;i++){
		cin>>b;
		temp+=b;
		a.push_back(b);
	}
	for(int i=0;i<n;i++){
		for(int j=i+1;j<n;j++){
			if((temp-a[i]-a[j])==100){
				a.erase(a.begin()+i);
				a.erase(a.begin()+j-1);
				sort(a.begin(),a.end());
				for(int i : a) cout<<i<<'\n';
				return 0;
			}
		}
	}
	return 0;
}

좀 더 깔끔한 코드

#include <bits/stdc++.h>
using namespace std;
pair <int,int> ret;
int a[9],temp=0;
vector<int> v;
int solve(){
	for(int i=0;i<9;i++){
		for(int j=i+1;j<9;j++){
			if(temp-a[i]-a[j]==100){
				ret = {i,j};
				return 0;
			}
		}
	}
}
int main() {
	int b;
	for(int i=0;i<9;i++){
		cin>>b;
		temp+=b;
		a[i]=b;
	}
	solve();
	for(int i=0;i<9;i++){
		if(ret.first==i||ret.second==i){
			continue;
		}
		v.push_back(a[i]);
	}
	sort(v.begin(),v.end());
	for(int i : v) cout<<i<<"\n";
	
	
	return 0;
}
profile
상상그이상인

0개의 댓글