Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int alg(int* sequencia, int valor, int tamanho) {
  6. int soma;
  7. for(int i = 0; i < tamanho; i++){
  8. for(int j = i; j < tamanho; j++){
  9. soma = 0;
  10. for (int z = i; z <= j; z++){
  11. soma = soma + sequencia[z];
  12. }
  13. if(soma == valor){
  14. return i;
  15. }
  16. }
  17. }
  18. return -1;
  19. }
  20.  
  21. int main() {
  22. int tamanho;
  23. int valor;
  24. int numero;
  25. //int sequencia[10000];
  26. int resultado;
  27.  
  28.  
  29. while(1) {
  30. scanf("%d %d", &tamanho, &valor);
  31.  
  32. if(tamanho == 0 && valor == 0){
  33. exit(0);
  34. }
  35. else {
  36. int sequencia[tamanho];
  37.  
  38. for(int i = 0; i < tamanho; i++){
  39. scanf("%d", &numero);
  40. sequencia[i] = numero;
  41. }
  42.  
  43. resultado = alg(sequencia, valor, tamanho);
  44. if (resultado != -1) {
  45. printf("SUBSEQUENCIA NA POSICAO %d\n", resultado+1);
  46. }
  47. else {
  48. printf("SUBSEQUENCIA NAO ENCONTRADA\n");
  49. }
  50. }
  51. }
  52.  
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement