zoukankan      html  css  js  c++  java
  • C#小技巧

    1)求全排列,采用递归分析,刚开始忘了 将数组恢复原样结果运行半天都是错误:

    代码
     private void button2_Click(object sender, EventArgs e)
            {
                perm(a, 
    02);
            }
            
    private void perm(int[] list, int i, int n)
            {
                
    int j;

                
    if (i == n)
                {
                    listprint(list);
                }
                
    else
                {
                    
    for (j = i; j <= n; j++)
                    {
                        SWAP(
    ref list[i], ref list[j]);
                        perm(list, i 
    + 1, n);
                        SWAP(
    ref list[i], ref list[j]);//一定要将数组恢复原样
                    }
                }
            }
            
    private void SWAP(ref int a, ref int b)
            {
                
    int c = a;
                a 
    = b;
                b 
    = c;
            }

    2)arraylist添加结构体单元时时,不需要每次都new一个结构体单元

    代码
     public struct pcoordinate
            {
                
    public int pX;
                
    public int pY;
            }
            ArrayList lt 
    = new ArrayList();
            
    private void button1_Click(object sender, EventArgs e)
            {
                pcoordinate pc 
    = new pcoordinate();
                pc.pX 
    = 1;
                pc.pY 
    = 1;
                lt.Add(pc);
               
     //不需要重新new一个pcoordinate,直接修改值添加到arraylist,不会改变原来的值
                pc.pX 
    = 2;
                pc.pY 
    = 2;
                lt.Add(pc);

                pc.pX 
    = 3;
                pc.pY 
    = 3;
                lt.Add(pc);

                
    foreach (pcoordinate p in lt)
                {
                    listBox1.Items.Add(p.pX.ToString() 
    + " " + p.pY.ToString());
                }
            }

    结果:1   1

            2   2

            3   3

    而不是1   1

            1   1

            1   1

  • 相关阅读:
    ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5, PySide or PySide2 package to be installed, but it was not found.
    cannot load library 'libportaudio.so.2': libportaudio.so.2: cannot open shared object file: No such file or directory
    如何解决Linux系统下pyaudio安装缺少文件问题error: portaudio.h: 没有那个文件或目录
    完美解决 python ImportError: Failed to import any qt binding
    Linux傻瓜式四步完美安装Python3.7
    为什么诺贝尔奖得主很少有中国人?
    Web前端入门知识
    MySQL通用编程
    其实我想做个诗人
    八皇后问题Python实现
  • 原文地址:https://www.cnblogs.com/king1302217/p/1641344.html
Copyright © 2011-2022 走看看