2014年3月7日 星期五

NTHU OJ - 7091 PA - Sum

The following program is my ACcepted code for NTHU-7091 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃NTHU 7091 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )

點這裡看題目 Click here to see this Problem!

這是比較慢的版本,在嚴格測資中可能TLE,請看這個新的較快版!

This article's program is slower. Maybe get TLE when there're serious test cases.
 Please see this faster version.

//This program is for NTHU 7091 PA - Sum
//NTHU 題目來源 Problem link: http://acm.cs.nthu.edu.tw/problem.php?pid=7091

#include<stdio.h>

int main()
{
    int K, N, Q, n[10001], q1, q2, ans;
 
    scanf("%d", &K);
 
    while(K--)
    {
        scanf("%d %d", &N, &Q);
     
        for(int i = 1; i <= N; i++)
            scanf("%d", &n[i]);
     
        while(Q--)
        {
            scanf("%d %d", &q1, &q2);
            ans = 0;
            for(int i = q1; i <= q2; i++)
                ans += n[i];
            printf("%d\n", ans);
        }
    }
    return 0;
}

Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D 

歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )

2 則留言:

  1. 這樣會TLE吧...
    一定是測資太簡單才會過...

    回覆刪除
  2. 真的,既然有人提了我就傳一個就較快版吧><
    請看這個新的較快版 ! (←請點它)
    我之前只是想先傳看看,因為我覺得可能沒那麼嚴格
    沒想到居然過了XD

    回覆刪除

請留下您的任何想法或建議!
Please leave any thought or comment!