Created
May 22, 2018 12:30
-
-
Save biacunha/91b9628eee13076c152f70e97f694fd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Escadinha - Fase 1 P1 - 2018 | |
//Por Lucio Cardoso | |
//Complexidade: O(n) | |
#include <bits/stdc++.h> | |
using namespace std; | |
const int MAXN = 1010; | |
// sequência de números | |
int num[MAXN]; | |
int main(void) | |
{ | |
int n; | |
cin >> n; | |
for (int i = 1; i <= n; i++) | |
cin >> num[i]; | |
// faremos esse caso separadamente | |
if (n == 1) | |
{ | |
cout << "1\n"; | |
return 0; | |
} | |
// resposta do problema | |
int ans = 0; | |
for (int i = 1; i < n; i++) | |
{ | |
// razão da PA | |
int dif = num[i]-num[i+1]; | |
for (int j = i; j < n; j++) | |
{ | |
// se j é o último número na PA | |
if (num[j]-num[j+1] != dif) | |
{ | |
// na prox. iteração, i receberá j-1+1 = j | |
i = j-1; | |
break; | |
} | |
// n é o último número da PA, então faço i = n-1 para sair do loop | |
if (j+1 == n) i = n-1; | |
} | |
// chegamos ao fim da PA atual, então incremento ans | |
ans++; | |
} | |
cout << ans << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment