hdu_2015_偶數求和

一次a;

http://acm.hdu.edu.cn/showproblem.php?pid=2015

偶數求和

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19451    Accepted Submission(s): 8453


Problem Description
有一個長度爲n(n<=100)的數列,該數列定義爲從2開始的遞增有序偶數,現在要求你按照順序每m個數求出一個平均值,如果最後不足m個,則以實際數量求平均值。編程輸出該平均值序列。
 

Input
輸入數據有多組,每組佔一行,包含兩個正整數n和m,n和m的含義如上所述。
 

Output
對於每組輸入數據,輸出一個平均值序列,每組輸出佔一行。
 

Sample Input
3 2 4 2
 

Sample Output
3 6 3 7
 
        1. #include<iostream>
        2. using namespace std;
        3. int main()
        4. {
        5. int i,j,sum=0,num,times;
        6. double n,m;
        7. while(cin>>n>>m)
        8. {
        9. if(n/m>(int)(n/m))
        10. times=(int)(n/m)+1;
        11. else times=(int)(n/m);
        12. for(i=1,num=2;i<=times;i++)
        13. {
        14. for(j=1;j<=m&&num<=n*2;j++)
        15. {
        16. sum+=num;
        17. num+=2;
        18. }
        19. j--;
        20. cout<<sum/j;
        21. sum=0;
        22. if(i!=times)
        23. cout<<" ";
        24. else
        25. cout<<"\n";
        26. }
        27. }
        28. return 0;
        29. }

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