알고리즘 스터디
백준 알고리즘 11651번 : 좌표 정렬하기
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void){
int N;
cin>>N;
vector<vector<int>> arr(N, vector<int>(2,0));
for(int i = 0; i < N; i++){
cin>>arr[i][1];
cin>>arr[i][0];
}
sort(arr.begin(),arr.end());
for(int i = 0; i < arr.size(); i++) {
cout<<arr[i][1]<<" "<<arr[i][0]<<'\n';
}
}
백준 알고리즘 2740번 : 행렬 곱셈
#include <iostream>
using namespace std;
int main()
{
int n1, m1;
int n2, m2;
int a[100][100];
int b[100][100];
int sum;
cin >> n1 >> m1;
for (int i = 0; i < n1; i++)
{
for (int k = 0; k < m1; k++)
cin >> a[i][k];
}
cin >> n2 >> m2;
for (int i = 0; i < n2; i++)
{
for (int k = 0; k < m2; k++)
cin >> b[i][k];
}
for (int i = 0; i < n1; i++)
{
for (int k = 0; k < m2; k++)
{
sum = 0;
for (int l = 0; l < n2; l++)
sum += a[i][l] * b[l][k];
cout << sum << " ";
}
cout << endl;
}
return 0;
}
백준 알고리즘 15649번 : N과 M (1)
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
int n;
int m;
int compare(vector<int> v, int start)
{
for (int i = 0; i < start; i++)
{
if (v[start] == v[i])
return (1);
}
return (0);
}
void n_and_m(int start)
{
if (start == m)
{
for (int i = 0; i < m; i++)
cout << v[i] << " ";
cout << '\n';
return ;
}
for (int i = 1; i <= n; i++)
{
v[start] = i;
if (compare(v, start) == 0)
n_and_m(start + 1);
}
}
int main()
{
cin >> n >> m;
v.resize(m);
for (int i = 0; i < m; i++)
v[i] = 0;
n_and_m(0);
return 0;
}
백준 알고리즘 13305번 : 주유소
#include <iostream>
using namespace std;
int main(void)
{
int n;
cin >> n;
int* dist = new int[n - 1];
int* price = new int[n];
long long greedy, sum = 0;
for (int i = 0; i < n - 1; i++)
cin >> dist[i];
for (int i = 0; i < n; i++)
cin >> price[i];
greedy = price[0];
for (int i = 0; i < n - 1; i++)
{
if (price[i] < greedy)
greedy = price[i];
sum += greedy * dist[i];
}
cout << sum;
}