티스토리 뷰
- 교집합
timelimit 난 코드
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
//freopen("input.txt", "rt", stdin);
int n, m, i, j, k, l, tmp, p1, p2, p3;
int a[101], b[101], c[201];
scanf("%d", &n);
for(i=1; i<=n; i++){
scanf("%d", &a[i]);
}
scanf("%d", &m);
for(i=1; i<=m; i++){
scanf("%d", &b[i]);
}
for(i=2; i<=n; i++){
tmp=a[i];
for(j=i-1; j>0; j--){
if(tmp<a[j]) {
a[j+1]=a[j];
}else {
break;
}
}
a[j+1]=tmp;
}
for(i=2; i<=m; i++){
tmp = b[i];
for(j=i-1; j>0; j--){
if(tmp<b[j]) {
b[j+1]=b[j];
}else {
break;
}
}
b[j+1]=tmp;
}
p1=1;
p2=1;
p3=1;
while(p1<=n && p2<=m){
if(a[p1]==b[p2]){
c[p3]=a[p1];
p1++;
p2++;
p3++;
}
else if(a[p1]>b[p2]){
p2++;
}
else if(a[p1]<b[p2]){
p1++;
}
}
for(i=1; i<p3; i++){
printf("%d ", c[i]);
}
return 0;
}
sort 사용
#include <iostream>
#include <vector>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
//freopen("input.txt", "rt", stdin);
int n, m, i, j, k, l, tmp, p1, p2, p3;
scanf("%d", &n);
vector<int> a(n);
for(i=0; i<n; i++){
scanf("%d", &a[i]);
}
scanf("%d", &m);
vector<int> b(m), c(n+m);
for(i=0; i<m; i++){
scanf("%d", &b[i]);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
p1=0;
p2=0;
p3=0;
while(p1<n && p2<m){
if(a[p1]==b[p2]){
c[p3]=a[p1];
p1++;
p2++;
p3++;
}
else if(a[p1]>b[p2]){
p2++;
}
else if(a[p1]<b[p2]){
p1++;
}
}
for(i=0; i<p3; i++){
printf("%d ", c[i]);
}
return 0;
}
- 연속된 자연수
#include <iostream>
#include <vector>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
//freopen("input.txt", "rt", stdin);
int n, m, i, j, k, l, cnt=0,sum, p1, p2, p3;
scanf("%d", &n);
for(i=n; i>0; i--){
sum = 0;
p1=0;
for(j=i; j<n; j++){
sum += j;
p1++;
if(sum==n) {
cnt++;
//printf("%d + ", i);
for(k=0; k<p1; k++){
if(k<p1-1){
printf("%d + ", i+k);
}
else if(k==p1-1){
printf("%d = ", i+k);
}
}
printf("%d\n", n);
break;
}
}
}
printf("%d", cnt);
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main() {
//freopen("input.txt", "rt", stdin);
int n, m, i, j, k, l, cnt=0, sum, tmp, rst, shr;
int a[1001];
scanf("%d", &n);
for(i=2; i<=n; i++){
sum=0;
for(j=1; j<=i; j++){
sum+=j;
}
tmp=n-sum;
if(tmp<0) break;
rst=tmp%i;
if(rst==0){
shr=tmp/i;
cnt++;
for(k=1; k<=i; k++){
if(k<i){
printf("%d + ", shr+k);
}
else if(k==i){
printf("%d = ", shr+k);
}
}
printf("%d\n", n);
}
}
printf("%d", cnt);
return 0;
}
'algorithm' 카테고리의 다른 글
[algorithm] 알고리즘의 기초 (0) | 2021.03.15 |
---|---|
[algorithm] 자료구조와 알고리즘 (0) | 2021.03.10 |
[c++] 두배열합치기 (0) | 2020.12.23 |
[c++] Least Recently Used, Inversion Sequence (0) | 2020.12.22 |
[c++] 삽입정렬, Special sort (0) | 2020.12.15 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 입출력장치
- 동적프로그래밍
- Stack
- 이진탐색
- 인접리스트
- 최단경로
- server side rendering
- 운영체제
- 병행프로세스
- 배열
- stackframe
- C++
- 클래스
- 알고리즘
- dfs
- BFS
- javascript
- 인접행렬
- react
- 세마포어
- 교착상태
- 자료구조
- 재귀함수
- Java
- client side rendering
- 소프트웨어
- 퀵정렬
- 구조체
- 스텍
- C
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함