C# Electronics Shop [HackerRank]
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static int getMoneySpent(int[] keyboards, int[] drives, int b) {
//최고 효율을 내는 비용 변수 -1로 선언
int maxCost=-1;
for(int i=0; i<keyboards.Length; i++)
{
for(int j=0; j<drives.Length; j++)
{
//보든 경우의 수를 반복하여 buget보다 작으면서 변수 maxCost보다 클 때 maxCost 초기화
if(keyboards[i]+drives[j]<=b && keyboards[i]+drives[j] > maxCost)
{
maxCost=keyboards[i]+drives[j];
}
}
}
return maxCost;
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] bnm = Console.ReadLine().Split(' ');
int b = Convert.ToInt32(bnm[0]);
int n = Convert.ToInt32(bnm[1]);
int m = Convert.ToInt32(bnm[2]);
int[] keyboards = Array.ConvertAll(Console.ReadLine().Split(' '), keyboardsTemp => Convert.ToInt32(keyboardsTemp))
;
int[] drives = Array.ConvertAll(Console.ReadLine().Split(' '), drivesTemp => Convert.ToInt32(drivesTemp))
;
/*
* The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items
*/
int moneySpent = getMoneySpent(keyboards, drives, b);
textWriter.WriteLine(moneySpent);
textWriter.Flush();
textWriter.Close();
}
}
댓글