hdu 2015 偶數求和

     簡單題要注意過程的優化,代碼要寫得簡潔、調理。 

     判斷一個人編程的水平高不高,只需要寫一行代碼就知道了。

     一定要多多注意思維的全面性,多審題,最後提煉出最簡潔的代碼方案。加油啊,你要做的很多絕不是三下五除二就能完成的。 

     這道題在C++中可以編譯通過,用C出現編譯錯誤。

 

  1. #include <iostream> 
  2. #include <cmath> 
  3. using namespace std; 
  4.  
  5. int main() 
  6.     int n, m; 
  7.     while(scanf("%d %d", &n, &m) != EOF) 
  8.     { 
  9.         int cnt = 0, tot = 0; 
  10.         for(int i = 0, num = 2; i < n; ++i, num += 2) 
  11.         { 
  12.             tot += num; 
  13.             cnt++; 
  14.             if(cnt == m && i != n-1) 
  15.             { 
  16.                 printf("%d ", tot/cnt); 
  17.                 cnt = 0; 
  18.                 tot = 0; 
  19.             } 
  20.             else if(i == n-1) 
  21.             { 
  22.                 printf("%d", tot/cnt); 
  23.                 cnt = 0; 
  24.                 tot = 0; 
  25.             } 
  26.         } 
  27.         printf("\n"); 
  28.     } 
  29.     return 0; 

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章