2009年2月10日 星期二

Q498: Polly the Polynomial

// Run time: 0.530
#include <stdio.h>
#define N 1000
int arr[N];
void calculate(int x, int num);

int main()
{
int i;
int num;
int x;
while (true)
{
for ( i = 0; ; i++)
{
if (scanf("%d", &arr[i]) != EOF)
{
if (getchar() == '\n')
break;
}
else
return 0;
}
num = i; // <=

while (true)
{
if (scanf("%d", &x) != EOF)
{
calculate(x, num);
if (getchar() == '\n')
{
putchar('\n');
break;
}
else
putchar(' ');
}
}

}

return 0;
}

void calculate(int x, int num)
{
int i, j;
int sum = 0;
int product;
int weight = num;
for ( i = 0; i <= num; i++)
{
product = 1;
for ( j = 0; j < weight; j++)
product *= x;
sum += arr[i]*product;
weight--;
}
printf("%d", sum);
}

沒有留言: