-
Notifications
You must be signed in to change notification settings - Fork 0
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
7-wnsmir #29
Conversation
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.
μ νμμ 그리 μ΄λ ΅μ§ μκ² κ΅¬νμΌλ.. cλ cppμμλ μ²λ¦¬ν μ μλ ν° μλ€ λλ¬Έμ stringμΌλ‘ μλ₯Ό λνλ ν¨μλ₯Ό λ§λ€μμ΅λλ€. λλΆμ μ½λ© μ€ν¬μ΄ λμλ€μ :)
cpp code
#include <iostream>
using namespace std;
string add(string large, string small) {
if (large.length() < small.length()) {
string tmp = small;
small = large;
large = tmp;
}
string sum = "";
int carry = 0;
// largeμ smallμ΄ μ€μ μ°μ°λλ λ‘μ§
for (int i = small.length() - 1; i >= 0; i--) {
int c = large[i] - '0' + small[i] - '0' + carry;
carry = c / 10;
sum = (char)(c % 10 + '0') + sum;
}
// largeμ smallμ κΈΈμ΄κ° λ€λ₯Ό λ largeμ μλΆλΆμ sumμ μΆκ°
for (int i = large.length() - small.length() - 1; i >= 0; i--) {
int c = large[i] - '0' + carry;
carry = c / 10;
sum = (char)(c % 10 + '0') + sum;
}
if (carry > 0) {
sum = (char)(carry + '0') + sum;
}
return sum;
}
string countTile(int n) {
string dp[251] = { "0", };
dp[0] = "1";
dp[1] = "1";
dp[2] = "3";
for (int i = 3; i <= n; i++)
//dp[i] += dp[i - 1] + dp[i - 2] * 2; μλμ κ°μ μλ―Έμ
λλ€ :)
dp[i] = add(dp[i], add(dp[i-1], add(dp[i-2], dp[i-2])));
return dp[n];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
while (cin >> n) {
cout << countTile(n) << '\n';
}
return 0;
}
for i in range(2, max_n + 1): | ||
dp[i] = dp[i-1] + dp[i-2] |
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.
νμ΄μ¬μ μ§μ§ μ μΈκ°.. μ΄λ κ² ν°μλ₯Ό μ΄λ»κ² λνλμ§ μ λ§ λλ¨νλ€μ!
- κ·Όλ° μ¬κΈ°μ
dp[i-2] * 2
λ₯Ό νμ§ μμλ λλμ?
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.
γ γ γ γ μ§μ§ νμ΄μ¬μ κ·Έλ₯ λν΄λ²λ¦¬λ€μ...
κ·Έλ¦¬κ³ μ λ dp[i-2] *2 λ₯Ό νμ§ μλ μ΄μ κ° κΆκΈν©λλ€!
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.
μ νμμ μκ°λ³΄λ€ λΉ λ₯΄κ² μκ°λ¬μμ΅λλ€.
κ·Έλμ λΉ λ₯΄κ² ꡬνν΄λ³΄λ €κ³ νλλ°...
μμ μΆλ ₯μ 보λκΉ κ±°μ 40μ리μ μ΄μμ΄ λμ€λκ±Έ 보κ³
μ μ΄κ±΄ λ¬Έμμ΄λ‘ λ°μΌλ©΄μ κ³μ°ν΄μΌκ² ꡬλ... λΌλ μκ°μ νμ΅λλ€.
λ¬Έμμ΄μ κ³μ°νλ λΆλΆμ μμ‘°λ‘κ² μμ±νλ€κ³ μκ°νκ³ μ€νμ μμΌλ΄€μ΅λλ€.
κ·Όλ° .... 342 + 242 κ° μκΎΈ 485λ‘ λμ€λ κ²λλ€.
κ·Έλ¦¬κ³ λ§μ§λ§κ³μ°μμ μ¬λ¦Όμ΄ λ κ²½μ°λ₯Ό νμΈνμ§ λͺ»νλ λ¬Έμ λ μμμ΅λλ€.
κ·Έλ°λ° 584κ°.... 485κ° λμ¨λ€? μ κ±°κΎΈλ‘ λμ€μ§...?
μ΄λ μκ° λ¬μ΅λλ€.
λ°°μ΄μμ 0λΆν° μ κ·Όνλ©΄ μ μΌ μλΆλΆ λΆν° λ€λ£¨κ² λλλ°
μ λ λ무 λΉμ°νκ²λ 1μμ리λΆν° λ€λ£¬λ€κ³ μκ°νμλ κ²μ
λλ€.
κ·Έλμ μ λ λ°°μ΄μ reverseνλ μ½λλ₯Ό μΆκ°νκ³ ,,, μ λ΅μ΄μμ΅λλ€.
μ€κ°μ λ¬Έμ λ₯Ό νλ©΄μ dp[i-2]μ κ³±νκΈ° 2λ₯Ό νλ λ‘μ§λ νμνλ€λ κ²μ μμμ΅λλ€.
κ·Έλ¦¬κ³ κΈ°λ₯μ μμ±λμλλ°
stringμ λνλ ν¨μλ₯Ό μ»΄ν©νΈνκ² μμ νκ³ ,
μ
λ ₯λ°λ λΆλΆμ μκ°νλ€κ³ 1μκ° λ° κ±Έλ Έμ΅λλ€ γ
γ
γ
γ
μ€μ μμ μ΄λ° λ¬Έμ λ₯Ό λ§λλ©΄ νμ΄μ¬μΌλ‘ νμ΄λ³΄λ κ²λ λμμ§ μμ κ² κ°λ€μ.
CPP CODE
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
string sumString(string a, string b) {
string result = "";
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int ten = 0;
for (int i = 0; i < a.length(); i++) {
int temp = (a[i] - '0') + (b[i] - '0') + ten;
if (temp >= 10) {
ten = 1;
temp %= 10;
}
else ten = 0;
result = to_string(temp) + result;
}
if (ten == 1) result = to_string(ten) + result;
return result;
}
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
vector<string> dp(251);
dp[0] = "1";
dp[1] = "1";
dp[2] = "3";
for (int i = 3; i <= 250; i++) {
string mul = sumString(dp[i - 2], dp[i - 2]);
dp[i] = sumString(dp[i - 1], mul);
}
int N;
while(cin >> N){
cout << dp[N] << '\n';
}
return 0;
}
for i in range(2, max_n + 1): | ||
dp[i] = dp[i-1] + dp[i-2] |
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.
γ γ γ γ μ§μ§ νμ΄μ¬μ κ·Έλ₯ λν΄λ²λ¦¬λ€μ...
κ·Έλ¦¬κ³ μ λ dp[i-2] *2 λ₯Ό νμ§ μλ μ΄μ κ° κΆκΈν©λλ€!
μ λ μ νμλ νΌμ μκ°νκΈ° κ½€ μ΄λ €μ λλ°μ ν ν ; |
π λ¬Έμ λ§ν¬
https://www.acmicpc.net/problem/1793
βοΈ μμλ μκ°
40min
β¨ μλ μ½λ
DPλ κ²°κ΅ μ νμμ μ°Ύλ μΈμμ λλ€.
μ£Όλ‘ λ§μ§λ§μ μ΄ν΄λ³΄λκ²μ΄ DPλ¬Έμ λ€μ μ°λ¬μ νμ΄λ³΄λ©° λλ μ μ λλ€.
n=0μΌλ κ°λ§ν μλκ±° νκ°μ§λ₯Ό ν μ μμ΅λλ€.
μ¦ dp[0] = 1
n=1μΌλλ 2*1μΈ νμΌ νλλ‘ μ±μΈ μ μμ΅λλ€
λ§μ°¬κ°μ§λ‘ dp[1]=1
n=2μΌλλ
μΈλ‘λ‘ λκ°λ₯Ό λ£κ±°λ, κ°λ‘λ‘ λκ°λ₯Ό λ£κ±°λ 2*2νλλ₯Ό λ£λ λ°©λ² 3κ°μ§κ° μμ΅λλ€.
dp[2]=3
n=3μ΄λΌλ©΄?
λ§μ§λ§μ΄ μΈλ‘μΈ 2*1μΌλ, λ¨μ건 n=2μΌλμ λ§μ°¬κ°μ§
μ¦ dp[3] = dp[2] + dp[1} + dp[1] = dp[2] + 2dp[1]
κ°λ‘λ‘ λκ°λ₯Ό λ°°μΉνλκ²κ³Ό 2*2λ₯Ό νλ λ°°μΉνλκ²μ λ€λ₯Έ κ²½μ°μ μμ§λ§ dp[1]μ λΆλ¬μ€λκ²μ κ°κΈ° λλ¬Έμ 2λ°°λ₯Ό ν΄μ£Όλκ²μ΄ ν₯μ λλ€.
μ΅λ νμΌμ ν¬κΈ°κ° 2*2λΌ κ·Έ μ΄μμ κ³ λ €νμ§ μμλ λ©λλ€.
μ νμμ λ°νμΌλ‘ μ½λꡬνμ μ¬μ΄νΈμ λλ€.