51Nod 1094 和为k的连续区间

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long LL;
 6 const int maxn = 10000 + 5;
 7 int a[maxn];
 8 LL sum[maxn];
 9 
10 int main(){
11     std::ios::sync_with_stdio(false);
12     int n, k;
13     cin >> n >> k;
14     memset(sum, 0, sizeof(sum));
15     for (int i = 1; i <= n; i++){
16         cin >> a[i];
17         sum[i] = sum[i - 1] + a[i];
18     }
19     bool ok = false;
20     for (int i = 0; i <= n; i++){        //可能只是自己,所以从0开始
21         for (int j = i; j <= n; j++){    
22             if (sum[j] - sum[i] == k){    
23                 cout << i+1 << " " << j    << endl;        //因为从0开始,所以i需要加1
24                 ok = true;
25                 break;
26             }
27         }
28         if (ok)
29             break;
30     }
31     if (!ok)
32         cout << "No Solution" << endl;
33     //system("pause");
34     return 0;
35 }
原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/8074910.html