一、题目描述

Given a polynomial and the value of the variable x, you task is to calculate the value of the polynomial.

二、输入

The first line of the input is a positive integer T. T is the number of test cases followed.

In each test case, the first line contains two integer n and x (0<=n<=50, 0<=x<=10000). The second line contains n+1 integers, representing the coefficients of a polynomial from power degree N down to power degree 0, each integer is no less than 0 and no more than 10000.

三、输出

The output of each test case should consist one line, containing the result of the polynomial.

例如:

输入:

1

2 5

1 2 3

输出:

38

四、解题思路

题意:这道题要求的是多项式值。如果只是简单的求多项式的值很简单。可题目有个条件是 (0<=n<=50, 0<=x<=10000),这样求出来的数已经超过了long long类型。

开始不知道怎么处理这个大数问题,在这里卡住了。后来同学说使用数组处理。以前没接触过使用输出保存大数的问题,当时也上不了网,于是自己思考一下,使用一个数组n表示n位十进制。每位表示十进制中的一位数。大数+(int类型)常数;大数*(int类型)常数。

1、多项式计算方法

记得高中学过求多项式值得方法能较少计算量:

如多项式:1*5^2+2*5^1+3*5^0

2、数组表示整数

例如439保存在数组bigInteger[3];bigInteger[2]=4;bigInteger[1]=3;bigInteger[0]=9;

1)数组表示的整数加上一个常数num

例如加上35。

先从最低位开始,例如bigInteger[0]+35=43;bigInteger[0]=43%10;进位数carryInt=43/10=4;

向高位逐步计算

bigInteger[1]+4=7,bigInteger[1]=7%10,carryInt=7/10。

当出现carryInt为0是停止计算,否则不断想高位计算。

计算的结果是:

bigInteger[2]=4;bigInteger[1]=7;bigInteger[0]=4;

2)数组表示的整数乘以一个常数num

例如bigInteger[2]=4;bigInteger[1]=7;bigInteger[0]=4;乘以4。

也是从最低位开始bigInteger[0]*4=16,bigInteger[0]=16%10=6;

进位数carryInt=16/10=1;

继续到下一位(高位):bigInteger[1]*4=28,28+carryInt=29,bigInteger[1]=29%10=9,

carryInt=29/10=2;

继续下一位:bigInteger[2]*4=16,16+carryInt=18,bigInteger[2]=18%10=8,

carryInt=18/10=1;

继续下一位:

bigInteger[3]开始至为0,所以bigInteger[3]=bigInteger[3]+carryInt=1。

最后计算结果为:bigInteger[3]=1,bigInteger[2]=8,bigInteger[1]=9,bigInteger[0]=6

五、代码

#include<iostream>

using namespace std;

const int BIT_NUM = 200;    //支持大数的最大位数(200位)

int main()
{
int times;
cin >> times; while(times--)
{
int bigInteger[BIT_NUM] = {0}; //用数组保存一个大数
int n, x, argue; //n-多项式的最高次数,x-位置数x的值,argue-每一项的系数
long long sum = 0; //多项式的结果
cin >> n >> x;
for(int k = 0; k < (n + 1); k++) //计算多项式,这里使用了一个公式例如1*5^2+2*5^1+3*5^0=38 可以写成(1*5+2)*5+3=38
{
cin >> argue;
int carryInt = 0; //进位(从低位算起,例如各位数是7乘以一个常数8,那么结果各位数是6,向高位数进5(carryInt))
for(int i = BIT_NUM; i > 0; i--) //一个大数乘以一个常数,从各位开始逐个与常数相乘,满十向高位进
{
bigInteger[i-1] = bigInteger[i-1] * x + carryInt;
carryInt = bigInteger[i-1] / 10;
bigInteger[i-1] %= 10;
} //大数加上一个常数
int addCarry = 0;
bigInteger[BIT_NUM - 1] += argue; //大数加上一个常数argue,先个位数加
for(int i = BIT_NUM; i > 0; i--)
{
bigInteger[i-1] = bigInteger[i-1] + addCarry;
addCarry = bigInteger[i-1] / 10; //addCarry保存进制数
bigInteger[i-1] %= 10;
if(addCarry == 0) break; //如果出现不需要向高位数进的退出
}
} int flag = false;
for(int i = 0; i < BIT_NUM; i++) //从高位第一个非0开始输出大数
{
if(bigInteger[i] != 0) {flag = true;}
if(flag) {cout << bigInteger[i];}
}
cout << endl; }
return 0;
}

最新文章

  1. [转]struts2处理.do后缀的请求
  2. c/c++面试题(8)memcopy/memmove/atoi/itoa
  3. 配置tomcat解压版
  4. 兼容90%标准C的词法分析器
  5. C# 跨线程操作无效
  6. 神奇的C语言
  7. 对 JimmyZhang 老师的文章《项目代码风格要求》的一些个人观点
  8. magento获取页面url的办法还有magento的常用函数
  9. [Codeforces137B]Permutation(贪心?思路?,水题)
  10. Asmack离线消息时间获取
  11. CentOS升级Python的方法
  12. Python中的判断、循环 if...else,while
  13. 经典关于多态的demo
  14. 从 3 个 IT 公司里学到的 57 条经验
  15. HDU 1231:最大连续子序列(DP)
  16. BZOJ_4773_负环_倍增弗洛伊德
  17. 几个python函数
  18. python pip源配置
  19. MySQL for Mac 5.7.x 版本忘记密码修改root密码
  20. python脚本1_给一个半径求圆的面积和周长

热门文章

  1. 《从零開始学Swift》学习笔记(Day5)——我所知道的标识符和keyword
  2. poj_3071概率dp
  3. fastdfs+nginx的安装部署
  4. MyBatis中关于SQL标签的用法(重用SQL 代码段)
  5. vue项目,封装api并使用
  6. cx-oracle-------------------安装
  7. BZOJ 3376 [Usaco2004 Open]Cube Stacking 方块游戏(带权并查集)
  8. python基础9 (迭代器、生成器)
  9. 斗地主算法的设计与实现(一)--项目介绍&amp;如何定义和构造一张牌
  10. vps上运行serv-u的问题