search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[C/C++ 演算法]- 上三角、下三角、對稱矩陣 – jashliao部落格

[C/C++ 演算法]- 上三角、下三角、對稱矩陣

剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。

拷貝來源:
http://openhome.cc/Gossip/AlgorithmGossip/
http://openhome.cc/Gossip/AlgorithmGossip/TriangleArray.htm

#include <stdio.h>
#include <stdlib.h>
#define N 5 
int main(void) {
int arr1[N][N] = {
{1, 2, 3,  4,   5},
{0, 6, 7,  8,   9},
{0, 0, 10, 11, 12},
{0, 0, 0,  13, 14},
{0, 0, 0,  0,  15}};
int arr2[N*(1+N)/2] = {0};
int i, j, loc = 0;
printf("\n以列為主:");
for(i = 0; i < N; i++) {
for(j = 0; j < N; j++) {
if(arr1[i][j] != 0)
arr2[loc++] = arr1[i][j];
}
}
for(i = 0; i < N*(1+N)/2; i++)
printf("%d", arr2[i]);
printf("\n輸入索引(i, j):");
scanf("%d, %d", &i, &j);
loc = N*i - i*(i+1)/2 + j;
printf("(%d, %d) = %d", i, j, arr2[loc]);
printf("\n");
return 0;
}

 



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦