Set Theory Programming Exercises | C++ | Discrete Mathematics

Topic 1. Seeking the transitive closure of a relationship

enter

Input a relational matrix at a time, and two adjacent elements in each row are separated by a space, and the rows and columns of the input elements correspond to the rows and columns of the relational matrix, respectively. The cardinality of the relationship is less than 12.

output

Output the relation matrix corresponding to the transitive closure of the relation.

Friendly reminder: you can use while (scanf("%d",&a)!=EOF)

test input expected output time limit memory limit extra process
Test case 1 display as text
  1. 0 1 0 0↵
  2. 1 0 1 0↵
  3. 0 0 0 1↵
  4. 0 1 0 0↵
display as text
  1. 1 1 1 1↵
  2. 1 1 1 1↵
  3. 1 1 1 1↵
  4. 1 1 1 1↵
1 second 64M 0

-> This question refers to the Warshell algorithm

#include<bits/stdc++.h>   
using namespace std;  
int Matrix[144];  
  
int main(){  
        
    int n=0, a;  
    while ( scanf("%d",&a)!=EOF ) {  
        Matrix[n] = a;  
        n++;  
    }  
    a = sqrt(n);  
      
    for(int k=0;k<a;k++){    
        for(int i=0;i<a;i++){   
            for(int j=0;j<a;j++){    
                if(Matrix[i*a+j]!=1)  
                    Matrix[i*a+j]=Matrix[i*a+k]&Matrix[k*a+j];  
            }  
        }  
    }  
    for (int i=0; i<a; i++) {  
        for (int j=0; j<a; j++) {  
            if (j==0) printf("%d",Matrix[i*a+j]);  
            else      printf(" %d",Matrix[i*a+j]);  
        }  
        printf("\n");  
    }  
       
    return 0;  
}  

Topic 2. Find the maximal and minimal elements in a partially ordered set

enter

Input a partially ordered set <A, ≤>. The number of elements in A does not exceed 20, and they are represented by a single lowercase English letter.

The first line of input gives the individual elements of A, with commas separating two adjacent elements.

The second line of the input gives the partial order relationship ≤, given in the form of ordered pairs (only the ordered pairs formed by the two elements satisfying coverage in the Haas diagram are given), such as <a,b>,< c, a> and so on, two adjacent ordered pairs are separated by commas.

output

Output the minimum element and maximum element of A.

The first line of the output gives each minimal element, and two adjacent elements are separated by commas. The output elements are required to be arranged in the natural order of the English letters.

The second line of the output gives each maximal element, and two adjacent elements are separated by commas, and the output elements are required to be output in the natural order of the English letters.

test input expected output time limit memory limit extra process
Test case 1 display as text
  1. a,b,c,d↵
  2. <a,b>,<c,d>↵
display as text
  1. a,c↵
  2. b,d↵
1 second 64M 0

For each element, count its prefix and suffix. If its prefix number is 0, it indicates that it is a minimum value; if its suffix number is 0, it indicates that it is a maximum value.

#include<bits/stdc++.h>   
using namespace std;  
struct ch {  
    char name;  
    int before=0, after=0;  
};  
ch character[20];  
int main(){  
       
    string str;  
    cin >> str;  
    for (int i=0; i<=str.length(); i+=2) {  
        character[i/2].name = str[i];  
    }  
    int n = (str.length()+1)/2;  
      
    cin >> str;  
    for (int i=0; i<=str.length(); i+=6) {  
        for (int j=0; j<n; j++) {  
            if (str[i+1]==character[j].name) {  
                character[j].after++;  
                break;  
            }  
        }  
        for (int j=0; j<n; j++) {  
            if (str[i+3]==character[j].name) {  
                character[j].before++;  
                break;  
            }  
        }  
    }  
      
    int k=0;  
    for (int i=0; i<n; i++) {  
        if (character[i].before==0) {  
            if (k==0) {  
                printf("%c",character[i].name);  
                k=1;  
            }  
            else {  
                printf(",%c",character[i].name);  
            }  
        }  
    }  
    printf("\n");  
      
    k=0;  
    for (int i=0; i<n; i++) {  
        if (character[i].after==0) {  
            if (k==0) {  
                printf("%c",character[i].name);  
                k=1;  
            }  
            else {  
                printf(",%c",character[i].name);  
            }  
        }  
    }  
    printf("\n");  
      
    return 0;  
}  

 These two questions are very simple, don't waste your time

Guess you like

Origin blog.csdn.net/m0_70241024/article/details/127701093