Status InitArray(int dimm,...)//若维数dim和随后的各维长度合法,则构造相应的数组A,并返回OK
Status DestroyArray() //销毁数组A
Status Locate(va_list ap,int &off) //若ap指示的各下标值合法,则求出该元素在A中相对地址off
Status Value(ElemType &e,...) //A是n维数组,e为元素变量,随后是n个下标值。若各下表不越界,则e赋值为所指定的A的元素值,并返回OK。
Status Assign(ElemType e,...) //A是n维数组,e为元素变量,随后是n各下表值。若各下标不越界,则将e的值付给所指定的A的元素,并返回OK。
1 //
2 //by coolxxx
3 //#include<bits/stdc++.h>
4 #include<iostream>
5 #include<algorithm>
6 #include<string>
7 #include<iomanip>
8 #include<map>
9 #include<stack>
10 #include<queue>
11 #include<set>
12 #include<bitset>
13 #include<memory.h>
14 #include<time.h>
15 #include<stdio.h>
16 #include<stdlib.h>
17 #include<string.h>
18 //#include<stdbool.h>
19 #include<math.h>
20 #define min(a,b) ((a)<(b)?(a):(b))
21 #define max(a,b) ((a)>(b)?(a):(b))
22 #define abs(a) ((a)>0?(a):(-(a)))
23 #define lowbit(a) (a&(-a))
24 #define sqr(a) ((a)*(a))
25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
26 #define mem(a,b) memset(a,b,sizeof(a))
27 #define eps (1e-10)
28 #define J 10000
29 #define mod 1000000007
30 #define MAX 0x7f7f7f7f
31 #define PI 3.14159265358979323
32 #pragma comment(linker,"/STACK:1024000000,1024000000")
33 #define N 8
34 const int OK=1;
35 const int ERROR=0;
36 const int INFEASIBLE=-1;
37 typedef int Status;
38 using namespace std;
39 typedef long long LL;
40 double anss;
41 LL aans;
42 int cas,cass;
43 LL n,m,lll,ans;
44
45 typedef int ElemType;
46 #include<stdarg.h> //标准头文件,提供宏va_start、va_arg、va_end 用于存取变长参数表
47 const int MAX_ARRAY_DIM=8; //假设数组维数的最大值为8
48 typedef struct
49 {
50 ElemType *base; //数组元素基址,由InitArray分配
51 int dim; //数组维数
52 int *bounds; //数组维界基址,由InitArray分配
53 int *constants; //数组映像函数常量基址,由InitArray分配
54 int elemtotal;
55 Status InitArray(int dimm,...)//若维数dim和随后的各维长度合法,则构造相应的数组A,并返回OK
56 {
57 int i;
58 va_list ap;
59 if(dimm<1 || dimm>MAX_ARRAY_DIM)return ERROR;
60 dim=dimm;
61 bounds=(int *)malloc(dim*sizeof(int));
62 if(!bounds)exit(OVERFLOW);//若各维长度合法,则存入A.bounds,并求出A的元素总数elemtotal
63 elemtotal=1;
64 va_start(ap,dim); //ap为va_list类型,是存放变长参量数表信息的数组
65 for(i=0;i<dim;i++)
66 {
67 bounds[i]=va_arg(ap,int);
68 if(bounds[i]<0)return UNDERFLOW;
69 elemtotal*=bounds[i];
70 }
71 va_end(ap);
72 base=(ElemType *)malloc(elemtotal*sizeof(ElemType));
73 if(!base)exit(OVERFLOW);
74 constants=(int *)malloc(dim*sizeof(int));
75 //求映像函数的常数ci,并存入A.constants[i-1],i=1,...,dim
76 if(!constants)exit(OVERFLOW);
77 constants[dim-1]=1; //L=1,指针的增减以元素的大小为单位
78 for(i=dim-2;i>=0;i--)
79 constants[i]=bounds[i+1]*constants[i+1];
80 return OK;
81 }//InitArray
82
83 Status DestroyArray() //销毁数组A
84 {
85 if(!base)return ERROR;
86 free(base);base=NULL;
87 if(!bounds)return ERROR;
88 free(bounds);bounds=NULL;
89 if(!constants)return ERROR;
90 free(constants);constants=NULL;
91 return OK;
92 }//DestroyArray
93
94 Status Locate(va_list ap,int &off) //若ap指示的各下标值合法,则求出该元素在A中相对地址off
95 {
96 int i,ind;
97 off=0;
98 for(i=0;i<dim;i++)
99 {
100 ind=va_arg(ap,int);
101 if(ind<0 || ind>=bounds[i])return OVERFLOW;
102 off+=constants[i]*ind;
103 }
104 return OK;
105 }//Locate
106
107 Status Value(ElemType &e,...) //A是n维数组,e为元素变量,随后是n个下标值。
108 //若各下表不越界,则e赋值为所指定的A的元素值,并返回OK。
109 {
110 va_list ap;
111 int result,off;
112 va_start(ap,e);
113 if((result=Locate(ap,off))<=0)return result;
114 e=*(base+off);
115 return OK;
116 }//Value
117
118 Status Assign(ElemType e,...) //A是n维数组,e为元素变量,随后是n各下表值。
119 //若各下标不越界,则将e的值付给所指定的A的元素,并返回OK。
120 {
121 va_list ap;
122 int result,off;
123 va_start(ap,e);
124 if((result=Locate(ap,off))<=0)return result;
125 *(base+off)=e;
126 return OK;
127 }//Assign
128
129 }Array;
130 void ArrayCheck()
131 {
132 int i,j,k;
133 Array A;
134 ElemType e;
135 A.InitArray(3,2,3,2);
136 printf("维度:%d
总元素个数:%d
各维维界:",A.dim,A.elemtotal);
137 for(i=0;i<A.dim;i++)
138 printf("%d ",A.bounds[i]);
139 puts("");
140 for(i=0;i<A.bounds[0];i++)
141 for(j=0;j<A.bounds[1];j++)
142 for(k=0;k<A.bounds[2];k++)
143 A.Assign(i*100+j*10+k+111,i,j,k);
144 for(i=0;i<A.bounds[0];i++,puts(""))
145 for(j=0;j<A.bounds[1];j++,puts(""))
146 for(k=0;k<A.bounds[2];k++)
147 printf("%d ",(A.Value(e,i,j,k),e));
148 A.DestroyArray();
149 puts("");
150 }
151 /*
152 程序结果:
153 维度:3
154 总元素个数:12
155 各维维界:2 3 2
156 111 112
157 121 122
158 131 132
159
160 211 212
161 221 222
162 231 232
163 */
164 int main()
165 {
166 #ifndef ONLINE_JUDGEW
167 // freopen("1.txt","r",stdin);
168 freopen("2.txt","w",stdout);
169 #endif
170 int i,j,k;
171 int x,y,z,xx,yy;
172 // init();
173 // for(scanf("%d",&cass);cass;cass--)
174 // for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
175 // while(~scanf("%s",s))
176 // while(~scanf("%d%d",&n,&m))
177 {
178 ArrayCheck();
179 }
180 return 0;
181 }
182 /*
183 //
184
185 //
186 */