1. 직사각형 별찍기
#include <iostream>
using namespace std;
int main(void) {
int a;
int b;
cin >> a >> b;
for(int i=0;i<b;i++){
for(int j=0;j<a;j++){
cout << "*";
}
cout <<endl;
}
return 0;
}
2. 행렬의 덧셈
#include <string>
#include <vector>
using namespace std;
vector<vector<int>> solution(vector<vector<int>> arr1, vector<vector<int>> arr2) {
vector<vector<int>> answer;
vector<int>vc;
for(int i=0;i<arr1.size();i++){
for(int j=0;j<arr1[i].size();j++){
vc.push_back(arr1[i][j]+arr2[i][j]);
}
answer.push_back(vc);
vc.clear();
}
return answer;
}
3. 짝수와 홀수
#include <string>
#include <vector>
using namespace std;
string solution(int num) {
string answer = "";
if(num%2==0){
answer= "Even";
}else{
answer = "Odd";
}
return answer;
}
4. 제일 작은 수 제거하기
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> arr) {
vector<int> answer;
int min=arr[0];
for(int i=0;i<arr.size();i++){
if(arr[i]<min){
min=arr[i];
}
}
if(arr.size()==1){
answer.clear();
answer.push_back(-1);
return answer;
}
for(int i=0;i<arr.size();i++){
if(min==arr[i]){
arr.erase(arr.begin()+i);
answer=arr;
}
}
return answer;
}
5. x만큼 간격이 있는 n개의 숫자
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<long long> solution(int x, int n) {
vector<long long> answer;
for(int i=1;i<=n;i++){
answer.push_back(x*i);
}
return answer;
}
6. 정수 내림차순으로 배치하기
#include <string>
#include <vector>
#include<algorithm>
using namespace std;
long long solution(long long n) {
long long answer = 0;
string str=to_string(n);
sort(str.begin(), str.end(), greater<char>());
return stoll(str);
}
7. 자릿수 더하기
#include <iostream>
#include <string>
using namespace std;
int solution(int n)
{
int ans = 0, tmp=0;
string str=to_string(n); // int -> string
char c;
for(int i=0;i<str.size();i++){
c=str[i];
tmp=(int)c-48; // string -> int 로 변경
ans+=tmp;
}
return ans;
}
다른풀이) 다 해놓고 헤더파일 string 안해서 위의 방법으로 제출함 ㅎ.. 확인 제대로 하자
#include <iostream>
#include <string>
using namespace std;
int solution(int n)
{
int ans = 0, tmp=0;
string str=to_string(n); // int -> string
for(int i=0;i<str.size();i++){
ans+=(str[i]-'0');
}
return ans;
}
8. 핸드폰 번호 가리기
처음 코드
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> vc;
string solution(string ph) {
string answer = "";
int i;
for(i=0;i<ph.size()-4;i++){
answer+="*";
}
for(i=ph.size()-4;i<ph.size();i++){
answer+=ph.at(i);
}
return answer;
}
replace 사용
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> vc;
string solution(string ph) {
string answer = ph;
for(int i=0;i<ph.size()-4;i++){
answer.replace(i,1,"*");
}
return answer;
}
9. 최대공약수와 최소공배수
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int gcd(int n, int m){
int rval;
while(m>0){
rval=n%m;
n=m;
m=rval;
}
return n;
}
int lcm(int n, int m){
return n*m/gcd(n,m);
}
vector<int> solution(int n, int m) {
vector<int> answer;
answer.push_back(gcd(n,m));
answer.push_back(lcm(n,m));
return answer;
}
10. 평균 구하기
#include <string>
#include <vector>
using namespace std;
double solution(vector<int> arr) {
double answer = 0;
for(int i=0;i<arr.size();i++){
answer+=arr[i];
}
return answer/arr.size();
}
11. 하샤드 수
#include <string>
#include <vector>
using namespace std;
bool solution(int x) {
bool answer = true;
int sum=0;
int n=x;
while(n!=0){
sum+=n%10;
n/=10;
}
if(x%sum!=0)
answer=false;
return answer;
}
12. Collatz conjecture
#include <string>
#include <vector>
using namespace std;
int solution(int a) {
int answer = 0;
long long num=a;
while(num!=1){
if (answer == 500) {
answer=-1;
break;
}
if(num%2==0){//짝수
num/=2;
}else{
num=num*3+1;
}
answer++;
}
return answer;
}
'코딩 > Programmers 알고리즘' 카테고리의 다른 글
| [C++] 위클리 챌린지 Level1 (0) | 2022.04.11 |
|---|---|
| [C++] Level 1 연습 문제 - 4 (0) | 2022.04.08 |
| [C++] Level 1 연습 문제 - 3 (0) | 2022.04.08 |
| [C++] Level 1 연습 문제 - 2 (0) | 2022.04.08 |
| Level 1 직사각형 별찍기 (0) | 2018.09.07 |