第一篇markdown,练习以下.
一个人对痛苦的感受能力和对无聊的感受能力成反比,这是由一个人的精神能力的大小决定的。也就是说,一个人精神的迟钝一般是与感觉的迟钝和缺乏兴奋密切相关,因此原因,精神迟钝的人也较少感受到痛苦和要求。但是,精神迟钝的后果就是内在空虚。人们对于外在世界所发生的微不足道的事情表现出的一刻不停的,强烈的关注,也暴露出他们的内在空虚。人的内在空虚就是无聊的真正根源,内心空虚之人无时无刻不在寻求外在刺激,试图借助某事某物使他们的精神和情绪活动起来。
-叔本华
多项式加法(5分)
题目内容:
一个多项式可以表达为x的各次幂与系数乘积的和,比如:
2x6+3x5+12x3+6x+20
现在,你的程序要读入两个多项式,然后输出这两个多项式的和,也就是把对应的幂上的系数相加然后输出。
程序要处理的幂最大为100。
输入格式:
总共要输入两个多项式,每个多项式的输入格式如下:
每行输入两个数字,第一个表示幂次,第二个表示该幂次的系数,所有的系数都是整数。第一行一定是最高幂,最后一行一定是0次幂。
注意第一行和最后一行之间不一定按照幂次降低顺序排列;如果某个幂次的系数为0,就不出现在输入数据中了;0次幂的系数为0时还是会出现在输入数据中。
输出格式:
从最高幂开始依次降到0幂,如:
2x6+3x5+12x3-6x+20
注意其中的x是小写字母x,而且所有的符号之间都没有空格,如果某个幂的系数为0则不需要有那项。
输入样例:
6 2
5 3
3 12
1 6
0 20
6 2
5 3
2 12
1 6
0 20
输出样例:
4x6+6x5+12x3+12x2+12x+40
#include <stdio.h>
void read(int *poly,int len){
int power = len-1;
scanf("%d", &poly[power]);
while (power!=0){
scanf("%d", &power) ;
scanf("%d", &poly[power]);
}
}
void add(int* poly_long, int* poly_short, int len_short){
int i;
for (i=0;i<len_short;i++){
poly_long[i] += poly_short[i];
}
}
void printpoly(int* poly,int len){
int isFirst = 1;
for (int exp=len-1;exp>=0;exp--){
if(poly[exp]!=0 ){
if(isFirst == 1){
isFirst = 0;
}else if (poly[exp]>0){
printf("+");
}
if (poly[exp] != 1 || exp == 0)
printf("%d",poly[exp]);
if (exp>=1)
printf("x");
if (exp>=2)
printf("%d",exp);
}
}
if (isFirst == 1)
printf("0");
}
int main(){
int power;
scanf("%d",&power);
int len1=power+1;
int poly1[len1];
for(int i=0;i<len1;i++)
poly1[i]=0;
read(poly1,len1);
scanf("%d",&power);
int len2=power+1;
int poly2[len2];
for(int i=0;i<len2;i++)
poly2[i]=0;
read(poly2,len2);
if (len2>len1){
add(poly2,poly1,len1);
printpoly(poly2,len2);
}else{
add(poly1,poly2,len2);
printpoly(poly1,len1);
}
return 0;
}