oid Sort(int a[][N], Node b[])//排序函数 { for (int i = 0; i < N; i++) { b[i].num = i;//初始化结点颜色 b[i].degree = 0;//初始化结点的度 for (int j = 0; j < N; j++)//内层嵌套,计算结点的度 { b[i].degree = b[i].degree + a[i][j]; } } for (int i = 0; i < N; i++)//冒泡排序 { for (int j = 0; j < N - 1; j++) { if (b[j].degree < b[j + 1].degree)//倒序,排序结点的度 { Node tem = b[j]; b[j] = b[j + 1]; b[j + 1] = tem; } } } } 解释一下这一段代码
时间: 2024-03-15 16:42:00 浏览: 71
这段代码是一个排序函数,用来对一个二维数组 a[N][N] 中的结点进行排序,排序依据是结点的度(即与其他结点相连的边的数量)。函数中使用了一个结构体 Node,其中包含了结点的编号 num 和度 degree。
首先,函数会遍历二维数组 a[N][N] 中的每一个结点,计算每个结点的度,并将其存储在相应的 Node 结构体对象的 degree 属性中。然后,函数使用冒泡排序算法来按照结点的度从大到小对 Node 结构体对象进行排序,排序结果存储在原数组 b[] 中。
在冒泡排序算法中,使用两个嵌套的循环,外层循环控制排序轮数,内层循环控制每轮比较和交换。比较时,将相邻两个结点的度进行比较,如果前一个结点的度小于后一个结点的度,则进行交换,从而实现按照结点的度从大到小排序的目的。
最后,函数不返回任何值,只是对传入的参数进行了修改。
相关问题
<?xml version="1.0"?> <Results xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Code>0</Code> <Message></Message> <Data xsi:type="BlcInOutStoreResponse"> <InOutStoreHead> <oid>1</oid> <sasStockPreentNo>1</sasStockPreentNo> <sasStockNo>1</sasStockNo> <chgTmsCnt>1</chgTmsCnt> </InOutStoreHead> <InOutStoreList> <headOid>1</headOid> <sasStockSeqno>1</sasStockSeqno> <chgTmsCnt>1</chgTmsCnt> <gdsMtno>1</gdsMtno> </InOutStoreList> <InOutStorePassPortList> <PassportNo>1</PassportNo> </InOutStorePassPortList> </Data> </Results> 创建相对应的java类
根据上面的XML文件可以创建以下Java类:
```java
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Results")
public class BlcInOutStoreResponse {
@XmlElement(name = "InOutStoreHead")
private InOutStoreHead inOutStoreHead;
@XmlElement(name = "InOutStoreList")
private InOutStoreList inOutStoreList;
@XmlElement(name = "InOutStorePassPortList")
private InOutStorePassPortList inOutStorePassPortList;
// getters and setters
}
@XmlAccessorType(XmlAccessType.FIELD)
class InOutStoreHead {
@XmlElement(name = "oid")
private int oid;
@XmlElement(name = "sasStockPreentNo")
private int sasStockPreentNo;
@XmlElement(name = "sasStockNo")
private int sasStockNo;
@XmlElement(name = "chgTmsCnt")
private int chgTmsCnt;
// getters and setters
}
@XmlAccessorType(XmlAccessType.FIELD)
class InOutStoreList {
@XmlElement(name = "headOid")
private int headOid;
@XmlElement(name = "sasStockSeqno")
private int sasStockSeqno;
@XmlElement(name = "chgTmsCnt")
private int chgTmsCnt;
@XmlElement(name = "gdsMtno")
private int gdsMtno;
// getters and setters
}
@XmlAccessorType(XmlAccessType.FIELD)
class InOutStorePassPortList {
@XmlElement(name = "PassportNo")
private int passportNo;
// getters and setters
}
```
这里使用了JAXB(Java Architecture for XML Binding)来进行XML和Java类之间的映射。注意要添加相应的依赖,比如在Maven项目中可以添加以下依赖:
```xml
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.2</version>
</dependency>
```
<?xml version="1.0"?> <Results xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Code>0</Code> <Message></Message> <Data xsi:type="BlcInOutStoreResponse"> <InOutStoreHead> <oid>1</oid> <sasStockPreentNo>1</sasStockPreentNo> <sasStockNo>1</sasStockNo> <chgTmsCnt>1</chgTmsCnt> </InOutStoreHead> <InOutStoreList> <headOid>1</headOid> <sasStockSeqno>1</sasStockSeqno> <chgTmsCnt>1</chgTmsCnt> <gdsMtno>1</gdsMtno> </InOutStoreList> <InOutStorePassPortList> <PassportNo>1</PassportNo> </InOutStorePassPortList> </Data> </Results>根据以上数据创建相应的java类
可以使用以下代码创建对应的Java类:
```java
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "Results")
@XmlType(propOrder = {"code", "message", "data"})
public class BlcInOutStoreResponse {
private int code;
private String message;
private InOutStoreData data;
public BlcInOutStoreResponse() {}
public int getCode() {
return code;
}
@XmlElement(name = "Code")
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
@XmlElement(name = "Message")
public void setMessage(String message) {
this.message = message;
}
public InOutStoreData getData() {
return data;
}
@XmlElement(name = "Data")
public void setData(InOutStoreData data) {
this.data = data;
}
@Override
public String toString() {
return "BlcInOutStoreResponse{" +
"code=" + code +
", message='" + message + '\'' +
", data=" + data +
'}';
}
}
@XmlRootElement(name = "Data")
@XmlType(propOrder = {"inOutStoreHead", "inOutStoreList", "inOutStorePassPortList"})
class InOutStoreData {
private InOutStoreHead inOutStoreHead;
private InOutStoreList inOutStoreList;
private InOutStorePassPortList inOutStorePassPortList;
public InOutStoreHead getInOutStoreHead() {
return inOutStoreHead;
}
@XmlElement(name = "InOutStoreHead")
public void setInOutStoreHead(InOutStoreHead inOutStoreHead) {
this.inOutStoreHead = inOutStoreHead;
}
public InOutStoreList getInOutStoreList() {
return inOutStoreList;
}
@XmlElement(name = "InOutStoreList")
public void setInOutStoreList(InOutStoreList inOutStoreList) {
this.inOutStoreList = inOutStoreList;
}
public InOutStorePassPortList getInOutStorePassPortList() {
return inOutStorePassPortList;
}
@XmlElement(name = "InOutStorePassPortList")
public void setInOutStorePassPortList(InOutStorePassPortList inOutStorePassPortList) {
this.inOutStorePassPortList = inOutStorePassPortList;
}
@Override
public String toString() {
return "InOutStoreData{" +
"inOutStoreHead=" + inOutStoreHead +
", inOutStoreList=" + inOutStoreList +
", inOutStorePassPortList=" + inOutStorePassPortList +
'}';
}
}
@XmlRootElement(name = "InOutStoreHead")
@XmlType(propOrder = {"oid", "sasStockPreentNo", "sasStockNo", "chgTmsCnt"})
class InOutStoreHead {
private int oid;
private int sasStockPreentNo;
private int sasStockNo;
private int chgTmsCnt;
public int getOid() {
return oid;
}
@XmlElement(name = "oid")
public void setOid(int oid) {
this.oid = oid;
}
public int getSasStockPreentNo() {
return sasStockPreentNo;
}
@XmlElement(name = "sasStockPreentNo")
public void setSasStockPreentNo(int sasStockPreentNo) {
this.sasStockPreentNo = sasStockPreentNo;
}
public int getSasStockNo() {
return sasStockNo;
}
@XmlElement(name = "sasStockNo")
public void setSasStockNo(int sasStockNo) {
this.sasStockNo = sasStockNo;
}
public int getChgTmsCnt() {
return chgTmsCnt;
}
@XmlElement(name = "chgTmsCnt")
public void setChgTmsCnt(int chgTmsCnt) {
this.chgTmsCnt = chgTmsCnt;
}
@Override
public String toString() {
return "InOutStoreHead{" +
"oid=" + oid +
", sasStockPreentNo=" + sasStockPreentNo +
", sasStockNo=" + sasStockNo +
", chgTmsCnt=" + chgTmsCnt +
'}';
}
}
@XmlRootElement(name = "InOutStoreList")
@XmlType(propOrder = {"headOid", "sasStockSeqno", "chgTmsCnt", "gdsMtno"})
class InOutStoreList {
private int headOid;
private int sasStockSeqno;
private int chgTmsCnt;
private int gdsMtno;
public int getHeadOid() {
return headOid;
}
@XmlElement(name = "headOid")
public void setHeadOid(int headOid) {
this.headOid = headOid;
}
public int getSasStockSeqno() {
return sasStockSeqno;
}
@XmlElement(name = "sasStockSeqno")
public void setSasStockSeqno(int sasStockSeqno) {
this.sasStockSeqno = sasStockSeqno;
}
public int getChgTmsCnt() {
return chgTmsCnt;
}
@XmlElement(name = "chgTmsCnt")
public void setChgTmsCnt(int chgTmsCnt) {
this.chgTmsCnt = chgTmsCnt;
}
public int getGdsMtno() {
return gdsMtno;
}
@XmlElement(name = "gdsMtno")
public void setGdsMtno(int gdsMtno) {
this.gdsMtno = gdsMtno;
}
@Override
public String toString() {
return "InOutStoreList{" +
"headOid=" + headOid +
", sasStockSeqno=" + sasStockSeqno +
", chgTmsCnt=" + chgTmsCnt +
", gdsMtno=" + gdsMtno +
'}';
}
}
@XmlRootElement(name = "InOutStorePassPortList")
@XmlType(propOrder = {"passportNo"})
class InOutStorePassPortList {
private int passportNo;
public int getPassportNo() {
return passportNo;
}
@XmlElement(name = "PassportNo")
public void setPassportNo(int passportNo) {
this.passportNo = passportNo;
}
@Override
public String toString() {
return "InOutStorePassPortList{" +
"passportNo=" + passportNo +
'}';
}
}
```
这个类中包含了所有的数据字段,以及对应的getter和setter方法,用于将XML数据转换为Java对象。在这个类中,我们使用了JAXB(Java Architecture for XML Binding)来实现XML数据和Java对象之间的转换。
阅读全文