第一次写博客,我的处女作就献给了hdu1003了了了
http://acm.hdu.edu.cn/showproblem.php?pid=1003
第一次写博客呀。。好激动,什么都不会,写的不好请吐槽。谢谢。
hdu1003,本来还以为是水题,没想到他喵的困了我那么久啊,不能忍啊……
其实只要注意将当前记忆的最大序列和mmax与临时用来判断要不要换掉mmax的sum区分开来就好,另外一点就是mmax是与sum同步更新的,就是说每一次sum变化都要判断一下要不要把mmax给换了。额,别忘了初始化。
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 int main() 6 { 7 int t, n; 8 int doubee; 9 cin >> t; 10 for(int cas = 1; cas <= t; cas++) 11 { 12 int mmax = -1111, left = 0, right = 0; 13 int sum = -1111, tmpl = 0, tmpr = 0; 14 cin >> n; 15 for(int i = 0; i < n; i++) 16 { 17 cin >> doubee; 18 if(sum < 0) //避免mmax加上了负数,这种累赘一定要及时甩掉 19 sum = doubee, tmpl = tmpr = i;//更新和的时候即更新下标 20 else 21 sum += doubee, tmpr = i; 22 if(mmax < sum)//每一次sum的值变化都要考虑要不要把mmax换了 23 mmax = sum, right = tmpr, left = tmpl; 24 } 25 cout << "Case " << cas << ":" << endl; 26 cout << mmax << " " << left + 1 << " " << right + 1 << endl; 27 if(cas != t) 28 cout << endl;//末尾没有空行别忘了。。 29 } 30 return 0; 31 }