티스토리 뷰

  • 연속부분증가수열
// 내가 짠코드
#include <iostream>
#include <vector>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
	//freopen("input.txt", "rt", stdin);
	int n, i, cnt=1, max=-2147000000;
	scanf("%d", &n);
	std::vector<int> a(n);
	for(i=1; i<=n; i++){
		scanf("%d", &a[i]);
	}
	for(i=1; i<=n-1; i++){
		if(a[i] <= a[i+1]){
			cnt++;
			if(cnt > max){
				max=cnt;
			}
		} else {
			cnt=1;
		}
	}
	printf("%d", max);
	return 0;
}

// 참고한 코드
#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
	//freopen("input.txt", "rt", stdin);
	int n, i, pre, now, cnt=1, max=-2147000000;
	scanf("%d", &n);
	scanf("%d", &pre);
	for(i=2; i<=n; i++){
		scanf("%d", &now);
		if(pre <= now){
			cnt++;
			if(cnt > max) max=cnt;
		} else cnt=1;
		pre=now;
	}
	printf("%d", max);
	return 0;
}
  • 참고한 코드에서는 배열에 무조건 담지 않고 for문을 한개만 이용해서 문제를 품
    • 연속적으로 나오는 수를 단순 비교하면 배열에 담는 것을 먼저 생각하기 보다는 하나씩 읽어 나가는 것을 방법을 고려해보기

 

  • Jolly jumpers
#include <iostream>
#include <vector>
#include <algorithm>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main() {
	//freopen("input.txt", "rt", stdin);
	int n, i, pre, now, diff, cnt=1, max=-2147000000;
	scanf("%d", &n);
	scanf("%d", &pre);
	std::vector <int> a(n);
	for(i=2; i<=n; i++){
		scanf("%d", &now);
		diff = abs(now - pre);
		if(diff>0 && diff<n && a[diff]==0){
			a[diff]=1;
		}else {
			printf("NO\n");
			return 0;
		}
		pre=now;
	}
	printf("YES\n");
	return 0;
}

'algorithm' 카테고리의 다른 글

[c++] N!의 표현법, N!에서 0의 개수  (0) 2020.12.10
[c++] 석차 구하기  (0) 2020.12.08
[c++] 카드게임, 온도의 최대값  (0) 2020.12.06
[c++] 가위바위보  (0) 2020.12.05
[c++] 층간소음  (0) 2020.12.04
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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 29 30 31
글 보관함