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;
}