1003.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // 1003.cpp: 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. using namespace std;
  6. struct Node{
  7. int sum;
  8. int start, end;
  9. };
  10. Node* Combine(int *digs,int N, int length) {
  11. Node *nodes = new Node[length];
  12. bool gteZero, next = true;
  13. int index = 0;
  14. for (int i = 0; i < N; i++) {
  15. if (i == 0) {
  16. nodes[index].sum = 0;
  17. nodes[index].start = 0;
  18. nodes[index].end = -1;
  19. }
  20. else if (digs[i] >= 0 != gteZero) {
  21. nodes[index].end = i - 1;
  22. index++;
  23. nodes[index].sum = 0;
  24. nodes[index].start = i;
  25. nodes[index].end = -1;
  26. }
  27. nodes[index].sum += digs[i];
  28. gteZero = digs[i] >= 0;
  29. }
  30. if (nodes[index].end == -1)
  31. {
  32. nodes[index].end = N - 1;
  33. }
  34. return nodes;
  35. }
  36. int getResult() {
  37. }
  38. int main()
  39. {
  40. int T, N;
  41. while (cin >> T) {
  42. for (int index = 0; index < T; index++) {
  43. cin >> N;
  44. int *digs = new int[N];
  45. Node *nodes;
  46. bool gteZero = false;
  47. int count = 0;
  48. for (int i = 0; i < N; i++) {
  49. cin >> digs[i];
  50. if (i == 0) {
  51. count++;
  52. }
  53. else if (digs[i] >= 0 != gteZero) {
  54. count++;
  55. }
  56. gteZero = digs[i] >= 0;
  57. }
  58. nodes = Combine(digs, N, count);
  59. free(digs);
  60. }
  61. }
  62. return 0;
  63. }