티스토리 뷰

algorithm

[c++] 층간소음

tonirr 2020. 12. 4. 04:01
  • 층간소음
#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, m, a, i, cnt=0, max=-2147000000;
	scanf("%d %d", &n, &m);
	for(i=0; i<n; i++){
		scanf("%d", &a);
		if(a>m) cnt++;
		else cnt=0;
		if(cnt>max) max=cnt;
		if(max==0) max=-1;
	}
	printf("%d", max);	
	return 0;
}

 

  • 분노유발자
    • 내가푼코드
      • 앞에서부터 확인
        • 이중 for문을 쓰므로 효율적이지 않은 것 같음
#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, a, i, j, cnt, fire=0;
	int h[101];
	scanf("%d", &n);
	for(i=1; i<=n; i++){
		scanf("%d", &a);
		h[i]=a;
	}
	for(i=1; i<n; i++){
		cnt=0;
		for(j=i+1; j<=n; j++){
			if(h[i]>h[j]){
				cnt++;
			}
		}
		if(cnt==(n-i)){
			fire++;
		}
	}
	printf("%d", fire);	
	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, a, i, cnt=0, max;
	int h[101];
	scanf("%d", &n);
	for(i=1; i<=n; i++){
		scanf("%d", &a);
		h[i]=a;
	}
	max=h[n];
	for(i=n-1; i>=1; i--){
		if(h[i]>max){
			max=h[i];
			cnt++;
		}
	}
	printf("%d", cnt);	
	return 0;
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함