Skip to content
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

[그리디] 10월 23일 #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions 10월 4일 - 그리디/10610.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

bool isAvailable(string &s)
{
int sum = 0;
bool isZero = false; //0이 있는지 여부

for (char c : s)
{
if (c == '0')
{
isZero = true;
}
sum += (c - '0');
}

return sum % 3 == 0 && isZero; //각 자리수의 합이 3의 배수이고 0이 존재한다면 30의 배수로 만들 수 있음
}

int main()
{
string s;
cin >> s;

if (!isAvailable(s))
{
cout << -1;
}
else
{
sort(s.begin(), s.end(), greater<char>()); //가장 큰 30의 배수로 만들기 위해 역순으로 정렬
cout << s;
}
}
31 changes: 31 additions & 0 deletions 10월 4일 - 그리디/11399.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
int n;
cin >> n;

vector<int> times(n, 0);

for (int i = 0; i < n; i++)
{
cin >> times[i];
}

//돈을 인출하는 데 시간이 적게 걸리는 순으로
sort(times.begin(), times.end());

int sum = 0, tmp = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍👍

for (auto i : times)
{
tmp += i;
sum += tmp;
}

cout << sum;
}
75 changes: 75 additions & 0 deletions 10월 4일 - 그리디/2503.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
#include <vector>
#include <string>

using namespace std;

bool check(string s1, string s2, int strike, int ball)
{
int s = 0, b = 0;

for (int i = 0; i < s1.size(); i++)
{
//자리와 숫자가 모두 같다면 strike
if (s1[i] == s2[i])
{
s++;
}

//숫자만 같은 경우에는 ball
else if (s2.find(s1[i]) != string::npos)
{
b++;
}
}

return (s == strike && b == ball);
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

int n, target, s, b;
vector<int> nums;

//가능한 모든 경우의 수 저장
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
if (i == j)
{
continue;
}
for (int k = 1; k <= 9; k++)
{
if (i == k || j == k)
{
continue;
}
nums.push_back(i * 100 + j * 10 + k);
}
}
}

cin >> n;

while (n--)
{
cin >> target >> s >> b;

for (int i = nums.size() - 1; i >= 0; i--)
{
//영수의 대답과 일치하지 않는 숫자를 삭제
if (!check(to_string(nums[i]), to_string(target), s, b))
{
nums.erase(nums.begin() + i);
}
}
}

cout << nums.size();
}
77 changes: 77 additions & 0 deletions 10월 4일 - 그리디/3613.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void changeType(string input)
{
string s = "";
int type = 0;
vector<string> str;

//첫 글자가 소문자가 아닌 경우
if (input[0] < 'a' || input[0] > 'z')
{
cout << "Error!";
return;
}

for (int i = 0; i < input.size(); i++)
{
if (input[i] == '_')
{
str.push_back(s);
s = "";

//변수명 타입이 혼용되거나 c++, java 변수명 타입에 해당되지 않는 경우
if (input[i + 1] < 'a' || input[i + 1] > 'z' || type == -1)
{
cout << "Error!";
return;
}

s += toupper(input[++i]);
type = 1; //c++ 변수명 타입이란 것을 저장
}

else if (input[i] >= 'A' && input[i] <= 'Z')
{
str.push_back(s + '_');
s = tolower(input[i]);

//변수명 타입이 혼용되거나 c++, java 변수명 타입에 해당되지 않는 경우
if (i > input.size() - 1 &&
(input[i + 1] < 'a' || input[i + 1] > 'z' || type == 1))
{
cout << "Error!";
return;
}

type = -1;
}

else
{
s += input[i];
}
}

str.push_back(s);

for (string s : str)
{
cout << s;
}
}

int main()
{
string input, s = "";
bool error_flag = false;
int type = 0;
vector<string> str;
Comment on lines +71 to +73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p3. 굳이 필요하지 않은 변수 선언인 것 같습니다.


cin >> input;
changeType(input);
}