1주차 (07/01) - Class 2
2798 - 블랙잭
#include <iostream>
#include <vector>
using namespace std;
int main() {
int cardNum, limitScore, maxScore = 0;
vector<int> cardList;
cin >> cardNum >> limitScore;
for (int i=0; i<cardNum; i++) {
int card;
cin >> card;
cardList.push_back(card);
}
for (int i=0; i<cardNum-2; i++) {
for (int j=i+1; j<cardNum-1; j++) {
for (int k=j+1; k<cardNum; k++) {
int cardSum = cardList[i] + cardList[j] + cardList[k];
if (cardSum <= limitScore && cardSum > maxScore)
maxScore = cardSum;
}
}
}
cout << maxScore << "\n";
return 0;
}
15829 - Hashing
#include <iostream>
#include <string>
using namespace std;
int main() {
int strLength;
string inputStr;
long long result = 0;
long long primeNum = 1;
cin >> strLength >> inputStr;
for (int i = 0; i < strLength; i++) {
int alphabetNum = inputStr[i] - 'a' + 1;
result += alphabetNum * primeNum;
result %= 1234567891;
primeNum *= 31;
primeNum %= 1234567891;
}
cout << result << "\n";
return 0;
}
1259 - 팰린드롬수
#include <iostream>
#include <string>
using namespace std;
int main() {
while (true) {
int num;
string numToStr;
bool isPalindrome = true;
cin >> num;
if (num == 0)
break;
numToStr = to_string(num);
for (int i=0; i<(numToStr.length() >> 1); i++) {
int idxFront = i;
int idxBack = numToStr.length() - i - 1;
if (numToStr[idxFront] != numToStr[idxBack]) {
isPalindrome = false;
break;
}
}
cout << ((isPalindrome) ? "yes" : "no") << "\n";
}
return 0;
}
2609 - 최대공약수와 최소공배수
#include <iostream>
#include <numeric>
using namespace std;
int main() {
int num1, num2;
cin >> num1 >> num2;
cout << gcd(num1, num2) << "\n" << lcm(num1, num2);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num1, num2;
int gcd, lcm;
cin >> num1 >> num2;
if (num1 < num2)
swap(num1, num2);
for (int i = num1; i >= 1; i--) {
if (num1 % i == 0 && num2 % i == 0) {
gcd = i;
break;
}
}
lcm = num1 * num2 / gcd;
cout << gcd << "\n" << lcm;
return 0;
}
2775 - 부녀회장이 될테야
#include <iostream>
using namespace std;
int main() {
int testCase;
cin >> testCase;
for (int i=0; i<testCase; i++) {
int floor, roomNumber;
long long result = 1;
cin >> floor >> roomNumber;
for (int i = floor + roomNumber; i >= 2; i--) {
result *= i;
if (i <= roomNumber - 1)
result /= i;
if (i <= floor + 1)
result /= i;
}
cout << result << "\n";
}
return 0;
}