The following program is my ACcepted code for NTHU-7492 .
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃NTHU 7492 的AC code!
先把卡片的數字累加!
讓card[i] 代表 第一張卡片加到第i張卡片的值
之後只要決定 "開頭" 跟 "結尾" 的index
就可以用 card[結尾]-card[開頭] = 從 "開頭" 到 "結尾" 的合
所以只要用 O(N^2) 得時間複雜度
//This program is for NTHU 7665 PD - Cake wants to win
//題目來源 Problem link: http://acm.cs.nthu.edu.tw/problem.php?pid=7665
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 2001
int main()
{
int N, K, tmp, ans;
int card[MAX];//card[i] is sum from 1st card to ith card.
bool findAns = false;
while(scanf("%d %d", &N, &K) != EOF)
{
memset(card, 0, sizeof(card));
findAns = false;
for(int i = 1; i <= N; i++)
{
scanf("%d", &tmp);
card[i] = card[i - 1] + tmp;
}
for(int i = 0; i < N; i++)
{
for(int j = i + 1; j <= N; j++)
{
tmp = card[j] - card[i];
if(tmp > K && !findAns)
{
findAns = true;
ans = tmp;
}
else if(findAns)
{
if(tmp > K && tmp < ans)
ans = tmp;
}
}
}
if(!findAns)
{
// if we can't find answer, but K < 0.
//Then we just don't choose any card and put 0!
if(K < 0)
printf("0\n");
else
printf("Cake will lose QAQ\n");
}
if(findAns)
{
//because K < 0, if answer < 0, then "this answer" is better than 0.
if(K < 0 && ans >= 0)
printf("0\n");
else
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/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃NTHU 7492 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
先把卡片的數字累加!
讓card[i] 代表 第一張卡片加到第i張卡片的值
之後只要決定 "開頭" 跟 "結尾" 的index
就可以用 card[結尾]-card[開頭] = 從 "開頭" 到 "結尾" 的合
所以只要用 O(N^2) 得時間複雜度
//This program is for NTHU 7665 PD - Cake wants to win
//題目來源 Problem link: http://acm.cs.nthu.edu.tw/problem.php?pid=7665
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 2001
int main()
{
int N, K, tmp, ans;
int card[MAX];//card[i] is sum from 1st card to ith card.
bool findAns = false;
while(scanf("%d %d", &N, &K) != EOF)
{
memset(card, 0, sizeof(card));
findAns = false;
for(int i = 1; i <= N; i++)
{
scanf("%d", &tmp);
card[i] = card[i - 1] + tmp;
}
for(int i = 0; i < N; i++)
{
for(int j = i + 1; j <= N; j++)
{
tmp = card[j] - card[i];
if(tmp > K && !findAns)
{
findAns = true;
ans = tmp;
}
else if(findAns)
{
if(tmp > K && tmp < ans)
ans = tmp;
}
}
}
if(!findAns)
{
// if we can't find answer, but K < 0.
//Then we just don't choose any card and put 0!
if(K < 0)
printf("0\n");
else
printf("Cake will lose QAQ\n");
}
if(findAns)
{
//because K < 0, if answer < 0, then "this answer" is better than 0.
if(K < 0 && ans >= 0)
printf("0\n");
else
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/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!