求离散数学c++代码,把一个序偶关系转换为关系矩阵,例如输入;;;;;会输出对应的关系矩阵
1条回答 默认 最新
threenewbee 2016-05-16 14:37关注用C#写一个给你
using System; using System.Linq; public class Test { public static void Main() { // your code goes here string s = "<a,b>;<b,c>;<a,d>;<c,e>;<e,a>;<d,b>"; int n = s.Where(x => x >= 'a' && x <= 'z').Max() - 'a' + 1; int[,] arr = new int[n, n]; foreach (var item in s.Split(';').Select(x => new { a = x[1] - 'a', b = x[3] - 'a' })) { arr[item.a, item.b] = 1; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(arr[i, j] + "\t"); } Console.WriteLine(); } } }0 1 0 1 0
0 0 1 0 0
0 0 0 0 1
0 1 0 0 0
1 0 0 0 0解决 无用评论 打赏 举报