标签:turn 大于 小明 out using iostream namespace ccf style
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int n, k; 8 int a[1001] = {}; 9 int count = 0; 10 int i; 11 cin>>n>>k; 12 for(i = 0; i < n; i ++) 13 { 14 cin>>a[i]; 15 } 16 i = 0; 17 while(a[i] != 0) 18 { 19 int weight = 0; 20 for(; i < n; i ++) 21 { 22 if(weight >= k) 23 break; 24 weight += a[i]; 25 } 26 count ++; 27 } 28 cout<<count<<endl; 29 30 return 0; 31 }
后来看了其他人的。。发现能简化好多。。
如下。。
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int n, k; 8 int a[1001] = {}; 9 int count = 0, weight = 0; 10 int i; 11 cin>>n>>k; 12 for(i = 0; i < n; i ++) 13 { 14 cin>>a[i]; 15 weight += a[i]; 16 if(weight >= k) 17 { 18 weight = 0; 19 count++; 20 } 21 } 22 if(weight > 0) 23 count ++; 24 cout<<count<<endl; 25 26 return 0; 27 }
标签:turn 大于 小明 out using iostream namespace ccf style
原文地址:https://www.cnblogs.com/forfriendforfun/p/9357373.html