返回的不应该是个元组(2345)吗为什么是120?没太懂是不是答案有问题
*b代表可变参数,所以这里面的b是(2,3,4,5)
a=a * n的意思是a等于a乘n
循环元组b中的元素依次与a相乘
所以最终a=1×2×3×4×5=120
提示:完整的程序包括程序前缀、静态顺序表的结构体类型定义、初始化子函数、表头插入新元素子函数、表尾删除元素子函数、表头删除元素子函数、表中删除元素子函数、输出子函数、主函数等。
#include<iostream>
using namespace std;
#define LIST_INIT_SIZE 100
//结构体类型定义
struct StaticList {
int elem[LIST_INIT_SIZE];
int length;
int listsize;
};
//初始化
void InitList_StaticList(StaticList &L) {
L. length=0;
L.listsize=LIST_INIT_SIZE;
}
//在表头插入新元素x
int Ins_SList1(StaticList &L,int x) {
//顺序表已满(没有多余空间),返回-1,表示插入失败
if(L.length>=L.listsize) {
printf("The list is overflow ! \n ");
return -1;
} else
for (int i=L.length-1; i>=0; i--) {
L.elem[i+1]=L.elem[i];//下标[0, length-1]的元素逐个后移
}
L.elem[0]=x;
L.length++;
return 1;
}
//输出
void print(StaticList &L) {
for(int i=0; i<=L.length-1 ; i++) {
cout<<L.elem[i]<<" ";
}
cout<<endl;
cout<<endl;
}
//删除表尾元素
int Del_SList2(StaticList &L) {
//顺序表为空(没有元素可删),返回-1,删除失败
if(L.length==0) {
printf("The list is empty ! \n");
return -1 ;
} else {
L.length--;
return 1 ;
}
}
//删除表头元素,第1个元素,下标为0
int Del_SList1(StaticList &L) {
//顺序表为空(没有元素可删),返回-1,删除失败
if(L.length==0) {
printf("The list is empty ! \n") ;
return -1;
} else { //数据由后往前移动
for(int i=1; i<=L.length-1 ; i++) {
L.elem[i-1]=L.elem[i];
}
L.length--;
return 1;
}
}
//删除表中数组下标为i的元素,第i+1个元素
int Del_SList(StaticList &L, int i) {
//顺序表为空(没有元素可删),返回-1,删除失败
if(L.length==0) {
printf("The list is empty ! \n") ;
return -1;
} else if(i<0 || i>L.length-1) {
printf("The index is error ! \n");
return 0;
} else { //i>=O && i<=L.length-1
for(int j=i+1; j<=L.length-1; j++) {
//下标[i+1, length-1]的元素逐个前移
L.elem[j-1]=L.elem[j];
}
L.length--;
return 1;
}
}
int main(int argc, char* argv[]) {
StaticList SL;
int num, pos;
InitList_StaticList(SL) ;
cout<<"enter numbers:"<<endl ;
while(1) {
int x;
cin>>x;
if(x==9999)
break ;
else
Ins_SList1 (SL,x) ;
}
cout<<"The list elems:"<<endl;
print(SL) ;
//删除表头元素
cout<<"delete the list_head element:" <<endl ;
Del_SList1(SL);
cout<<"The new list elems after deleting list_head element:"<<endl ;
print(SL);
//删除表尾元素
cout<<"delete the list_tail element:" <<endl;
Del_SList2(SL);
cout<<"The new list elems after deleting list_tail element:"<<endl ;
print(SL);
//删除表中间元素
cout<<"delete the list_middle element:" <<endl;
//输入删除元素的下标
cout<<"input the deleted position:"<<endl;
cin>>pos;
Del_SList(SL, pos-1) ;
cout<<"The new list elems:"<<endl ;
print(SL);
return 0;
}