pta刷题函数
最大子列和问题
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int s[100010];
int main() {
int i, j, n, ans = 0;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> s[i];
}
for (i = 1; i <= n; i++) {
int temp = s[i];
for (j = i + 1; j <= n; j++) {
temp += s[j];
if (temp > ans) {
ans = temp;
}
}
}
cout << ans << endl;
return 0;
}
两个有序链表序列的合并
List Merge( List L1, List L2 )
{
if(L1->Next==NULL&&L2->Next==NULL)return L1;
else
{
List head=(List)malloc(sizeof(List)),p;
ElementType f=0;
while(L1->Next!=NULL&&L2->Next!=NULL)
{
if(L1->Next->Data<L2->Next->Data)
{
if(f==0)
{
head->Next=L1->Next;
p=head->Next;
}
else
{
p->Next=L1->Next;
p=p->Next;
}
L1->Next=L1->Next->Next;
p->Next=NULL;
}
else
{
if(f==0)
{
head->Next=L2->Next;
p=head->Next;
}
else
{
p->Next=L2->Next;
p=p->Next;
}
L2->Next=L2->Next->Next;
p->Next=NULL;
}
f++;
}
if(L1->Next!=NULL)
{
p->Next=L1->Next;
L1->Next=NULL;
}
if(L2->Next!=NULL)
{
p->Next=L2->Next;
L2->Next=NULL;
}
return head;
}
}
线性表元素的区间删除
List Delete( List L, ElementType minD, ElementType maxD )
{
int k=0,i;
for(i=0;i<=L->Last;i++)
if(L->Data[i]<=minD||L->Data[i]>=maxD)L->Data[k++]=L->Data[i];
L->Last=k-1;
return L;
}
递归求简单交错幂级数的部分和
double fn( double x, int n )
{
if(n==1)return x;
else
{
if(n%2==0)return fn(x,n-1)-pow(x,n);
else return fn(x,n-1)+pow(x,n);
}
}