Arrays 如何得到C中矩阵对角线的一侧
我必须输入数组长度及其元素,如代码所示。数组长度表示城市的数量,其元素表示城市之间的距离,如下图所示 所以输入应该是这样的:5和2 3 4 1 我必须找到像I-I一样的圆的两点之间的最短距离;I-II;I-III。。。II-I;II-II。。。直到我填充一个矩阵,其大小为数组大小x数组大小(在本例中为5x5) 输出需要如下所示:Arrays 如何得到C中矩阵对角线的一侧,arrays,c,matrix,dynamic-arrays,Arrays,C,Matrix,Dynamic Arrays,我必须输入数组长度及其元素,如代码所示。数组长度表示城市的数量,其元素表示城市之间的距离,如下图所示 所以输入应该是这样的:5和2 3 4 1 我必须找到像I-I一样的圆的两点之间的最短距离;I-II;I-III。。。II-I;II-II。。。直到我填充一个矩阵,其大小为数组大小x数组大小(在本例中为5x5) 输出需要如下所示: 0 2 5 2 1 2 0 3 4 3 5 3 0 4 5 2 4 4 0 1 1 3 5 1 0 这是我的代码: #include <stdio.h>
0 2 5 2 1
2 0 3 4 3
5 3 0 4 5
2 4 4 0 1
1 3 5 1 0
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int l,n, i, m, x,y,s,j,t;
int **p;
scanf("%d", &l);
ptr = (int*)malloc(l * sizeof(int));
if (ptr == NULL) {
exit(0);
}
else {
for (i = 0; i < l; ++i)
{
scanf("%d", &ptr[i]);
if(ptr[i]<0)
{
exit(0);
}
}
for (i = 0; i < l; ++i) {
printf("%d ", ptr[i]);
}
}
n=l;
m=l;
while(m>0 && n>0){
p= malloc(m*sizeof(int*));
for (i=0;i<m;i++){
p[i]=malloc(n*sizeof(int));
for(j=0;j<n;j++){
x=0;
y=0;
for (t = 0; t < j; t++) {
x += ptr[t];
}
for (t = n; t >j; t--) {
y += ptr[t];
}
if (x > y) {
s = y;
} else {
s = x;
}
p[i][j]=s;
}
}
}
for(i=0;i<n;i++) {
for(j=0;j<m;j++) {
printf("%d", p[i][j]);
}
putchar('\n');
}
return 0;
}
#包括
#包括
int main()
{
int*ptr;
int l,n,i,m,x,y,s,j,t;
int**p;
scanf(“%d”和“&l”);
ptr=(int*)malloc(l*sizeof(int));
如果(ptr==NULL){
出口(0);
}
否则{
对于(i=0;i0){
p=malloc(m*sizeof(int*);
对于(i=0;i y){
s=y;
}否则{
s=x;
}
p[i][j]=s;
}
}
}
对于(i=0;i好吧,首先,我认为你的程序将运行一个无止境的循环,因为你有这行代码
while(m>0 && n>0)
但是“m”和“n”在循环内不会改变。
我认为这是可行的,至少在你的例子中是这样的,所以看一下:
#include <stdio.h>
#include <stdlib.h>
int main(){
int* ptr;
int l,n, i, m, x,y,s,j,t;
int **p;
scanf("%d", &l);
ptr = (int*)malloc(l * sizeof(int));
if (ptr == NULL) {
exit(0);
}
else {
for (i = 0; i < l; ++i)
{
scanf("%d", &ptr[i]);
if(ptr[i]<0)
{
exit(0);
}
}
for (i = 0; i < l; ++i) {
printf("%d ", ptr[i]);
}
printf("\n");
}
//Everything up to here is the same, now we initialize the "result" matrix
p = (int**) malloc (l * sizeof(int*));
for(i = 0; i < l; i++){
p[i] = (int*) malloc(l * sizeof(int));
p[i][i] = 0;//this will just fill the diagonal with 0's
}
for(i = 0; i < l; i++){//this is the starting point
for(j = i+1; j < l; j++){//this is the end point. Note we are starting at i+1 because we only need to fill one since the other is just mirrored
int l1 = 0, l2 = 0; //2 possible lengths, one going forward for index i -> i+1 ->...-> j and one going backwards i -> i-1 -> i-2 ->...->j
//going forwards
for(m = i; m <j; m++)
l1 += ptr[m];
//going backwards
n = i-1;
if(n == -1)//if we are out of bounds->go back
n = l-1;
while(n != j){
l2 += ptr[n];
n--;
if(n == -1)//if we are out of bounds->go back
n = l-1;
}
l2 += ptr[j];
//we compare to find the minimum
if(l1 < l2)
p[j][i] = l1;
else
p[j][i] = l2;
}
}
//we now mirror the results for the other half
for(i = 0; i < l; i++)
for(j = i+1; j < l; j++)
p[i][j] = p[j][i];
printf("RESULTS: \n");
//and we print
for(i = 0; i < l; i++){
for(j = 0; j < l; j++){
printf("%d ", p[i][j]);
}
printf("\n");
}
return 0;
}
#包括
#包括
int main(){
int*ptr;
int l,n,i,m,x,y,s,j,t;
int**p;
scanf(“%d”和“&l”);
ptr=(int*)malloc(l*sizeof(int));
如果(ptr==NULL){
出口(0);
}
否则{
对于(i=0;i 如果(ptr[i]如果calloc()
所有元素都将被初始化为零:ptr=calloc(l,sizeof(int));
…并且,为了获得良好的实践效果,请记住在不再需要使用内存时free()
内存。通常,对称矩阵通过强制执行只谈论坐标[i][j]来表示其中i>j。如果您碰巧需要访问i