-
Notifications
You must be signed in to change notification settings - Fork 1
33-kokeunho #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
33-kokeunho #132
Conversation
kangrae-jo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ½ 2μκ°λμμ gptμ claudeλ₯Ό κ³ λ¬Έν λμ μ λ΅μ μ»μ΄λμ΅λλ€.
μ²μμ ν 30-40λΆμ λ¬Έμ λ₯Ό μ λͺ» μ΄ν΄ν΄μ κ³ μμ μ’ νλ€μ.
μ κΈ° λμμλ μκ° 10μ§μμΈμ€ μμμ΅λλ€.
κ·Έλ¬λ μ
λ ₯μΌλ‘ μ£Όμ΄μ§λ μλ€μ λͺ¨λ nμ§μ λλΌκ΅¬μ.
14 + 3 = 17μ λ³΄κ³ νμ°Έ λ©λλ Έλ€μ.
λ¬Έμ λ₯Ό νΈλ λ°©μμ μλμ κ°μ΅λλ€.
- μ tokenize νλ©΄μ Xκ° ν¬ν¨λ μ λΆλ¦¬
- 2μ§λ² λΆν° 9μ§λ² κΉμ§ μ μ ν μ μ©μμΌλ³΄κΈ°( expsλ₯Ό νλμ© κΊΌλ΄κ°λ©° μμ΄ λ§λμ§νμΈ)
- ν보μ μλ μ§λ²λ€ μ μ©μ κ°μ΄ λ¬λΌμ§λ λ μμ ?λ‘ λ£κ³ , μλ¬λΌμ§λ λ μμ κ·Έ κ°μ λ£μ΅λλ€.
μ΄λ μ λ΅ λ°°μ΄μ λ£μλλ μ§λ²μ μ μ§ν΄μΌν΄μ.
λ°λΌμ μ¦κ° μ°μ°μ
Nμ§λ²μμ 10μ§λ²μΌλ‘ λ°κΎΈκ³ ,
κ³μ°νκ³ ,
10μ§λ²μμ Nμ§λ²μΌλ‘ λ°κΏμΌ ν©λλ€.
μ½λμ μ μλμ΄μλ + - λ 10μ§λ² κΈ°λ° κ³μ°μ΄κΈ° λλ¬Έμ΄μ£ .
κ·Έλ¦¬κ³ λ κ±°μ 1μκ°μ μ΄ λΆλΆμ΄ μλλ°, μ΄κ±° νλ κ³ μ³μ£ΌλκΉ μ λ΅μ΄λλΌκ΅¬μ.
그건 λ°λ‘ λͺ¨λ μλ¦Ώμλ₯Ό νμΈνλ©΄μ μ΅μ μ§λ²? μ μ°Ύλ κ±°μμ.
μ§λ²μ΄ μ μ©μ΄ λλ μλλλ‘ νλ¨ν μ μμ κ±°λΌ μκ°νλλ°, μλμλλ΄μ...
μ€λλ§μ λ§€μ°λ§€μ° μ΄λ ΅κ³ νλ€μλ€μ.
κ·ΌνΈλ μ½λλ λ§μ΄ λμμ΄ λμμ΅λλ€. κ°μ¬ν©λλ€.
CPP CODE
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
int nToTen(const string& s, int base, bool& ok) {
int val = 0;
ok = true;
for (char ch : s) {
if (ch < '0' || ch > '9') {
ok = false;
return 0;
}
int d = ch - '0';
if (d >= base) {
ok = false;
return 0;
}
val = val * base + d;
}
return val;
}
string tenToN(int val, int base) {
if (val == 0) return "0";
string result = "";
while (val > 0) {
result = char('0' + val % base) + result;
val /= base;
}
return result;
}
struct Exp {
string a, s, b, c;
Exp(string a_, string s_, string b_, string c_) : a(a_), s(s_), b(b_), c(c_) {}
bool isCollectAry(int ary) {
bool okA, okB, okC;
int A = nToTen(a, ary, okA);
int B = nToTen(b, ary, okB);
int C = nToTen(c, ary, okC);
if (!okA || !okB || !okC) return false;
if (s == "+") return (A + B) == C;
else return (A - B) == C;
}
string getResult(int ary) {
bool okA, okB;
int A = nToTen(a, ary, okA);
int B = nToTen(b, ary, okB);
if (!okA || !okB) return "";
int result;
if (s == "+") result = A + B;
else result = A - B;
return tenToN(result, ary);
}
string toString(string result) {
return a + " " + s + " " + b + " = " + result;
}
// μμ ν¬ν¨λ λͺ¨λ μ«μμμ κ°μ₯ ν° μλ¦Ώμ μ°ΎκΈ°
int getMaxDigit() {
int maxDigit = 0;
for (char ch : a) {
if (ch >= '0' && ch <= '9') maxDigit = max(maxDigit, ch - '0');
}
for (char ch : b) {
if (ch >= '0' && ch <= '9') maxDigit = max(maxDigit, ch - '0');
}
if (c == "X") return maxDigit;
for (char ch : c) {
if (ch >= '0' && ch <= '9') maxDigit = max(maxDigit, ch - '0');
}
return maxDigit;
}
};
vector<string> solution(vector<string> expressions) {
vector<Exp> xs, exps;
for (int i = 0; i < expressions.size(); i++) {
stringstream ss(expressions[i]);
string a, sign, b, equal, c;
ss >> a >> sign >> b >> equal >> c;
if (c == "X") xs.push_back(Exp(a, sign, b, c));
else exps.push_back(Exp(a, sign, b, c));
}
// λͺ¨λ μμμ μ΅λ μλ¦Ώμ μ°ΎκΈ°
int maxDigit = 0;
for (Exp exp : exps) {
maxDigit = max(maxDigit, exp.getMaxDigit());
}
for (Exp exp : xs) {
maxDigit = max(maxDigit, exp.getMaxDigit());
}
// μ΅λ μλ¦Ώμ + 1μ§λ²λΆν° μμ
vector<int> arys;
for (int ary = maxDigit + 1; ary <= 9; ary++) {
bool flag = true;
for (Exp exp : exps) {
if (!exp.isCollectAry(ary)) {
flag = false;
break;
}
}
if (flag) arys.push_back(ary);
}
vector<string> answer;
for (Exp exp : xs) {
vector<string> results;
for (int ary : arys) {
string result = exp.getResult(ary);
if (!result.empty()) {
results.push_back(result);
}
}
bool allSame = true;
if (results.size() > 1) {
for (int i = 1; i < results.size(); i++) {
if (results[i] != results[0]) {
allSame = false;
break;
}
}
}
if (allSame && !results.empty()) answer.push_back(exp.toString(results[0]));
else answer.push_back(exp.toString("?"));
}
return answer;
}
π λ¬Έμ λ§ν¬
[PCCP κΈ°μΆλ¬Έμ ] 4λ² μμ 볡μ
https://school.programmers.co.kr/learn/courses/30/lessons/340210
βοΈ μμλ μκ°
3h? (νμ΄λ μ¬μ΄λ° ꡬν μλμ΄ μ’ λΆμ‘±ν΄μ...)
β¨ μλ μ½λ
ν΄λΉ λ¬Έμ νμ΄λ κ°λ¨ν©λλ€.
μ λ ₯ λ°μ μμμ κ°λ₯ν μ§λ² λ²μλ₯Ό μ°ΎμλΈ ν
μμ μ±λ¦½ μν€λ μ§λ²λ€λ§ μΆλ €λ λλ€.
κ·Έλ¦¬κ³ ν΄λΉ μ§λ²λ€λ‘ Xλ₯Ό ꡬνκ³ μΆλ ₯μ ꡬνλ©΄ λ©λλ€.
ꡬν μλμ΄ λΆμ‘±ν΄μ ν€λ§¨ λ¬Έμ μμ΅λλ€.
λ€λ₯Έ λΆλ€μ μ΄λ ΅μ§ μκ² ν μ μμ κ²μ΄λΌκ³ μκ°ν©λλ€.
λ μμΈν νμ΄λ λΈλ‘κ·Έ ν¬μ€ν μΌλ‘ μμ±ν΄λ΄€μ΅λλ€.
https://velog.io/@kokangsik65/PCCP-κΈ°μΆ-4λ²-ꡬνμ-νμ§-λͺ»νλ-λμκ²
μ½λ 리뷰λ ν¬μ€νΈ 리뷰λ λ¬κ² λ°κ² μ΅λλ€!
π μλ‘κ² μκ²λ λ΄μ©