using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Node treeRoot = CreateTree();
            scanTree(treeRoot);
        }
  
        private static void scanTree(Node treeRoot)
        {
            List<Node> list = new List<Node>();
            list.Add(treeRoot);
            Node point = treeRoot;
            Write(treeRoot);
            while (true)
            {
                if (!list.Contains(point))
                { //上一轮是移除的操作
                    if (treeRoot.leftSon == point)
                    {//移除的是左结点
                        if (treeRoot.rightSon != null)
                        {
                            treeRoot = treeRoot.rightSon;
                            list.Add(treeRoot);
                            Write(treeRoot);
                            point = treeRoot;
                            continue;
                        }
                        list.Remove(treeRoot);
                        if (list.Count == 0)
                        {
                            break;
                        }
                        point = treeRoot;
                        treeRoot = list[list.Count - 1];
                    }
                    else
                    {//移除的是右结点
                        list.Remove(treeRoot);
                        if (list.Count == 0)
                        {
                            break;
                        }
                        point = treeRoot;
                        treeRoot = list[list.Count - 1];
                    }
                    continue;
                }
  
                if (treeRoot.leftSon != null)
                {
                    treeRoot = treeRoot.leftSon;
                    Write(treeRoot);
                    list.Add(treeRoot);
                    point = treeRoot;
                    continue;
                }
                if (treeRoot.rightSon != null)
                {
                    treeRoot = treeRoot.rightSon;
                    Write(treeRoot);
                    point = treeRoot;
                    list.Add(treeRoot);
                    continue;
                }
                if (treeRoot.leftSon == null && treeRoot.rightSon == null)
                {
                    list.Remove(treeRoot);
                    if (list.Count == 0)
                    {
                        break;
                    }
                    point = treeRoot;
                    treeRoot = list[list.Count - 1];
                }
            }
  
        }
  
        public static void Write(Node node)
        {
            Console.WriteLine(node.Data);
        }
  
        private static Node CreateTree()
        {
            Node a = new Node("A");
           a.leftSon = new Node("B");
            a.rightSon = new Node("C");
            
           a.leftSon.leftSon = new Node("D");
           a.leftSon.rightSon = new Node("E");
            
           a.rightSon.leftSon = new Node("F");
           a.rightSon.rightSon = new Node("G");
  
           a.leftSon.leftSon.leftSon = new Node("H");
           a.leftSon.leftSon.rightSon = new Node("I");
            return a;
        }
    }
  
    class Node
    {
        public string Data { get; set; }
        public Node leftSon { get; set; }
        public Node rightSon { get; set; }
  
        public Node(string data)
        {
            Data = data;
        }
    }
}

C#先序遍历二叉树(非递归)

//根据categoryName ,counterName,instanceName 获得性能情况显示,取5秒内的平均值
private static float CPUPerformance(string CategoryName, string InstanceName, string CounterName)
{
    PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
  
    float cpuload = 0;
    float averageload = 0;
    for (int x = 0; x < 5; x++)
    {
        Thread.Sleep(1000);
        cpuload += pc.NextValue();
    }
    averageload = cpuload / 5;
    return averageload;
}

//CPU使用率
float CPU_Usage = CPUPerformance("Processor", "_Total", "% Processor Time");

获取 CPU 5秒内的平均使用情况

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace staggerArray
{
    class Program
    {
        static void Main(string[] args)
        {
            int [][] a=new int[5][]; 
            Random ran = new Random();
            int[] sum = new int[a.Length];
            double num = 0;
            int i , temp=0;
            for ( i = 0; i < a.Length;i++ )
            {
               a[i] = new int[ran.Next(5)+1];
                if(temp < a[i].Length){
                    temp = a[i].Length;
                }
            } 
            for (i = 0; i < a.Length; i++)
            {
                Console.Write("第{0}行:", i + 1);
                for(int j = 0;j < a[i].Length;j++){
                    a[i][j] = ran.Next(100)+1;
                    Console.Write(a[i][j]);
                    Console.Write("\\t");
                    sum[i] += a[i][j];
                }
                for (int n = 1; n <= temp - a[i].Length; n ++ )
                {
                    Console.Write("\\t");
                }
                num = (double)sum[i] / a[i].Length;
                Console.Write("平均值:{0:f2}",num);
                Console.WriteLine();
            }
        }
    }
}

交错数组(求平均数)

using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
  
namespace CI
{
    class CI_Encrypt
    {
        private string key;
        private string hash_type = "sha1";
  
        public CI_Encrypt(string key = "")
        {
            if(key.Length > 0)
            {
                this.key = key;
            }
        }
        public string encode(string data)
        {
            RijndaelManaged aes = new RijndaelManaged();
            aes.KeySize = 256;
            aes.BlockSize = 256;
            aes.Padding = PaddingMode.Zeros;
            aes.Mode = CipherMode.CBC;
  
            aes.Key = this.get_key(key);
            aes.GenerateIV();
            var _IV = aes.IV;
  
            byte[] buffer;
              
            ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key, aes.IV);
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, AESEncrypt, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
  
                        //Write all data to the stream.
                        swEncrypt.Write(data);
                    }
                    buffer = msEncrypt.ToArray();
                }
            }
  
            var enc = new byte [ _IV.Length + buffer.Length ];
  
            Array.Copy(_IV,enc ,_IV.Length);
            Array.Copy(buffer,0,enc,_IV.Length,buffer.Length);
  
            return this.add_cipher_noise(enc);
        }
  
        public string decode(string data)
        {
            string plainText;
            byte[] _enc_data = this.remove_cipher_noise(Convert.FromBase64String(data));
            using (RijndaelManaged aes = new RijndaelManaged())
            {
                aes.KeySize = 256;
                aes.BlockSize = 256;
                aes.Padding = PaddingMode.Zeros;
                aes.Mode = CipherMode.CBC;
                aes.Key = this.get_key(key);
                var _IV = new byte[aes.IV.Length];
                Array.Copy(_enc_data, _IV, _IV.Length);
                aes.IV = _IV;
                var endata = new byte[_enc_data.Length - aes.IV.Length];
                Array.Copy(_enc_data, aes.IV.Length, endata, 0, endata.Length);
  
                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
  
                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(endata))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
  
                            // Read the decrypted bytes from the decrypting stream 
                            // and place them in a string.
                            plainText = srDecrypt.ReadToEnd();
                        }
                    }
                }
  
            }
            return plainText;
        }
  
        public string add_cipher_noise(byte[] data)
        {
            string hashkey = this.hash(System.Text.Encoding.Default.GetString(this.get_key(this.key)));
            char[] keyChar = hashkey.ToCharArray();
            byte[] encrpytData = new byte[data.Length];
            for (int i = 0, j = 0; i < data.Length; i++, j++)
            {
                if (j >= hashkey.Length)
                {
                    j = 0;
                }
                var temp = (((int)data[i] + (int)keyChar[j]) % 256);
                encrpytData[i] = (byte)temp;
            }
            return System.Convert.ToBase64String(encrpytData);
        }
  
        public byte[] remove_cipher_noise(byte[] data)
        {
            string hashkey = this.hash(System.Text.Encoding.Default.GetString(this.get_key(this.key)));
            char[] keyChar = hashkey.ToCharArray();
            byte[] encrpytData = new byte[data.Length];
            int temp;
  
            for (int i = 0, j = 0; i < data.Length; i++, j++)
            {
                if (j >= hashkey.Length)
                {
                    j = 0;
                }
                temp = (int)data[i] - (int)keyChar[j];
                if(temp < 0)
                {
                    temp += 256;
                }
                encrpytData[i] = (byte)temp;
            }
            return encrpytData;
        }
  
        private string hash(string key)
        {
            byte[] hashBytes;
            if (this.hash_type == "sha1")
            {
                SHA1 sha = new SHA1CryptoServiceProvider();
                hashBytes = (sha.ComputeHash(System.Text.Encoding.Default.GetBytes(key)));
            }
            else
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                hashBytes = (md5.ComputeHash(System.Text.Encoding.Default.GetBytes(key)));
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2"));
            }
            return sb.ToString().ToLower();
        }
  
        private byte[] get_key(string key)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] hashBytes = (md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key)));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2"));
            }
            return System.Text.Encoding.Default.GetBytes(sb.ToString().ToLower());
        }
    }
}

用C#实现跟CI里面的加密解密

package com.arry;
  
import java.util.Scanner;
  
public class TextOperatorArray {
    /*
     * 数组复制
     */
    public static void copy(){
        int array[] = new int[]{1,2,3,4};
        int temp1[] = new int[array.length];
        int temp2[] = new int[array.length];
        System.arraycopy(array, 0, temp1, 0, array.length);
        System.out.println("复制后的数组结果:");
        for(int i=0;i<array.length;i++)
            System.out.print(temp1[i]+" ");
        System.out.println();
        temp2 = array;
    }
    /*
     * 数组插入
     */
    public static void insert(){
        int i,j;
        int n =5;
        int num[] = new int[n+1];
        for(i=0;i<num.length;i++){
            num[i] = (i+1)*6;
        }
        int lenght = num.length;        //获得数组长度
        System.out.println("输入数字之前的数组为:");
        for(i = 0;i<lenght;i++)          //循环显示数组元素
            if(num[i]==0)
                System.out.print("存数空间");
            else
                System.out.print(num[i]+" ");
        System.out.println();
        System.out.println("输入一个要输入的数:");
        Scanner scan = new Scanner(System.in);  //键盘输入一个数
        int in = scan.nextInt();
        for(i=0;i<lenght-1;i++){             //循环找一个大于要插入数的位置
            if(num[i]>in)                        //找到大于要插入数就跳出
                break;
        }
        for(j=lenght-1;j<i;j--){             //为要插入的数留位置
            num[j]=num[j-1];
        }
        num[i] = in;                            //把要插入的数保存到该位置  
        for(i=0;i<lenght;i++)                    //循环显示数组元素
            System.out.print(num[i]+" ");
        System.out.println();
    }
    /*
     * 数组合并并排序
     */
    public static int[] combine(int[] a,int[] b){
        int alen = a.length;        //获得传入数组长度
        int blen = b.length;
        int length = alen + blen;
        int i,j;
        System.out.println("合并之间的两个数组:");
        for(i = 0;i<alen;i++)        //循环a数组元素
            System.out.print(a[i]+" ");
        System.out.println();
        for(i = 0;i < blen;i++)      //循环b数组元素
            System.out.print(b[i]+" ");
        System.out.println();
        int[] c=new int[length];    //声明数组长度为传入两个数组长度之和
        for(i =0,j=0;i < alen && j < blen;){      //循环查看元素进行比较
            if(a[i]<b[j]){           //判断两个数组元素值的大小
                c[i+j] = a[i];
                i++;
            }else{
                c[i+j] = b[j];
                j++;
            }
        }
        if(i==alen)
            System.arraycopy(b, j, c, alen + j, blen - j);
        if(j==blen)
            System.arraycopy(a, i, c, blen+i, alen-i);
        System.out.println("合并并排序之后的新数组:");
        for(int k =0;k<a.length+b.length;k++)    //循环输出合并后的数组的元素
            System.out.print(c[k]+" ");
        System.out.println();
        return c;
          
    }
    public static void main(String[] args) {
        System.out.println("1.数组复制:");
        copy();
        int a[] = {1,2,3,12};
        int b[] = {5,6,7,8};
        System.out.println("2.数组合并:");
        int c[] = combine(a, b);
        System.out.println("3.数组插入");
        insert();
    }
      
}

一维数组复制、插入和合并

package chu;
  
import java.util.ArrayList;
  
/**
 * @author chufazheng@gmail.com
 * update: 2014-11-26
 */
public final class SudokuSolver {
  
    private ArrayList<Integer> blank;
    private ArrayList<Integer> fixed;
    private ArrayList<TrialPoint> trials;
    private int[] cell = new int[81];
  
    public static void main(String[] arg) {
        String str;
        if (arg != null && arg.length > 0 && arg[0].length() >= 81) {
            str = arg[0];
        } else {
            //str="000000500000008300600100000080093000000000020700000000058000000000200017090000060";//the least given
            str = "800000000003600000070090200050007000000045700000100030001000068008500010090000400";//the world hardest
        }
        SudokuSolver game = new SudokuSolver();
        game.load(str);
        game.solve();
    }
  
    public void solve() {
        if (fixed.size() < 17) {
            System.out.println("Invalid game!");
            System.exit(0);
        }
        showBoard(true);
        long start = System.currentTimeMillis();
        if (!basicSolve()) {
            trialSolve();
        }
        long end = System.currentTimeMillis();
        showBoard(false);
        if (check()) {
            System.out.println("[" + (end - start) + "ms OK!]");
        } else {
            System.out.println("[" + (end - start) + "ms, cannot solve it, are you sure it has solution?");
        }
    }
  
    public SudokuSolver() {
        blank = new ArrayList();
        fixed = new ArrayList();
        trials = new ArrayList();
    }
  
    public void load(String str) {
        blank.clear();
        fixed.clear();
        char[] chr = str.toCharArray();
        int i = 0, j = 0, len = chr.length;
        while (i < len && j < 81) {
            if (Character.isDigit(chr[i])) {
                if (chr[i] == '0') {
                    cell[j] = 0x1FF;
                    blank.add(j);
                } else {
                    cell[j] = 1 << chr[i] - 49;
                    fixed.add(j);
                }
                j++;
            }
            i++;
        }
        //if the numbers input less than 81, add '0's.
        for (;j < 81; j++) {
            cell[j] = 0x1FF;
            blank.add(j);
        }
    }
  
    private void reset(int[] data) {
        blank.clear();
        fixed.clear();
        System.arraycopy(data, 0, cell, 0, 81);
        for (int i = 0; i < 81; i++) {
            if (Integer.bitCount(cell[i]) != 1) {
                blank.add(i);
            } else {
                fixed.add(i);
            }
        }
    }
  
    private void showCell(int idx, boolean hide) {
        if (hide) {
            if (Integer.bitCount(cell[idx]) == 1) {
                System.out.print("[" + (Integer.numberOfTrailingZeros(cell[idx]) + 1) + "]");
            } else {
                System.out.print("[ ]");
            }
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append('[');
            for (int i = 0; i < 9; i++) {
                if ((cell[idx] >>> i & 1) == 1) {
                    sb.append(i + 1);
                }
            }
            sb.append(']');
            System.out.print(sb);
        }
    }
  
    private void showBoard(boolean hide) {
        System.out.print("---------------------------");
        for (int i = 0; i < 81; i++) {
            if (i % 9 == 0) {
                System.out.print("\\n");
            }
            showCell(i, hide);
        }
        System.out.println("\\n---------------------------");
    }
  
    private boolean updateCandidates(ArrayList<Integer> fixed) {
        for (int i : fixed) {
            int opt = 0x1FF ^ cell[i];
            for (int j : X(i)) {
                cell[j] &= opt;
                //!notice
                if (cell[j] == 0) {
                    return false;
                }
            }
        }
        return true;
    }
  
    private void seekFixedable() {
        fixed.clear();
        for (int i : blank) {
            if (Integer.bitCount(cell[i]) == 1) {
                fixed.add(i);
                //System.out.println("fixed:"+i+"=>"+cell[i]);
            }
        }
        blank.removeAll(fixed);
    }
  
    private boolean check() {
        int[] checkpoint = new int[]{0, 12, 24, 28, 40, 52, 56, 68, 80};
        for (int i : checkpoint) {
            int r, b, c;
            r = b = c = cell[i];
            for (int j = 0; j < 8; j++) {
                r ^= cell[X(i)[j]];
                c ^= cell[X(i)[8 + j]];
                b ^= cell[X(i)[16 + j]];
            }
            if ((r & b & c) != 0x1FF) {
                return false;
            }
        }
        return true;
    }
  
    private void seekUniqueCandidate() {
        for (int cel_idx : blank) {
            int row = 0, col = 0, box = 0;
            for (int i = 0; i < 8; i++) {
                row |= cell[X(cel_idx)[i]];
                col |= cell[X(cel_idx)[8 + i]];
                box |= cell[X(cel_idx)[16 + i]];
            }
            if (Integer.bitCount(cell[cel_idx] & ~row) == 1) {
                cell[cel_idx] &= ~row;
                continue;
            }
            if (Integer.bitCount(cell[cel_idx] & ~col) == 1) {
                cell[cel_idx] &= ~col;
                continue;
            }
            if (Integer.bitCount(cell[cel_idx] & ~box) == 1) {
                cell[cel_idx] &= ~box;
            }
        }
    }
  
    private void seekMutexCell() {
        ArrayList<Integer> two = new ArrayList();
        for (int i : blank) {
            if (Integer.bitCount(cell[i]) == 2) {
                two.add(i);
            }
        }
        for (int i = 0; i < two.size(); i++) {
            for (int j = i + 1; j < two.size(); j++) {
                if (cell[two.get(i)] == cell[two.get(j)]) {
                    int opt = ~cell[two.get(i)];
                    if (two.get(i) / 9 == two.get(j) / 9) {//same row
                        for (int n = 0; n < 8; n++) {
                            cell[X(two.get(i))[n]] &= opt;
                        }
                    }
                    if ((two.get(i) - two.get(j)) % 9 == 0) {//same col                        
                        for (int n = 8; n < 16; n++) {
                            cell[X(two.get(i))[n]] &= opt;
                        }
                    }
                    if ((two.get(i) / 27 * 3 + two.get(i) % 9 / 3) == (two.get(j) / 27 * 3 + two.get(j) % 9 / 3)) {//same box
                        for (int n = 16; n <24; n++) {
                            cell[X(two.get(i))[n]] &= opt;
                        }
                    }
                    cell[two.get(j)] = ~opt;
                }
            }
        }
    }
  
    private boolean basicSolve() {
        do {
            if (!updateCandidates(fixed)) {
                backForward();
            }
            seekUniqueCandidate();
            seekMutexCell();
            seekFixedable();
        } while (!fixed.isEmpty());
        return blank.isEmpty();
    }
  
    private boolean setTrialCell() {
        for (int i : blank) {
            if (Integer.bitCount(cell[i]) == 2) {
                int trialValue = 1 << Integer.numberOfTrailingZeros(cell[i]);
                int waitingValue = cell[i] ^ trialValue;
                //System.out.println("Try:[" + i + "]->" + (Integer.numberOfTrailingZeros(trialValue) + 1) + "#" + (Integer.numberOfTrailingZeros(waitingValue) + 1));
                cell[i] = trialValue;
                trials.add(new TrialPoint(i, waitingValue, cell));
                return true;
            }
        }
        return false;
    }
  
    private void backForward() {
        if (trials.isEmpty()) {
            System.out.println("Can not go backforword, no trial point, maybe no solution!");
            return;
        }
        TrialPoint back = trials.get(trials.size() - 1);
        trials.remove(back);
        reset(back.cel);
        cell[back.idx] = back.val;
        fixed.add(back.idx);
        //System.out.println("Back:[" + back.idx + "]->" + (Integer.numberOfTrailingZeros(back.val) + 1));
    }
  
    private void trialSolve() {
        while (!blank.isEmpty()) {
            if (setTrialCell()) {
                basicSolve();
            } else {
                if (trials.isEmpty()) {
                    //System.out.println("Maybe no solution.");
                    break;
                } else {
                    backForward();
                    basicSolve();
                }
            }
        }
    }
    private int[] X(int idx){
        int neighbors[]=new int[24];
        int box[]=new int[]{0,1,2,9,10,11,18,19,20};
        int r=idx/9,c=idx%9,xs=idx/27*27+idx%9/3*3;
        int i=0;
        for(int n=0;n<9;n++){
            if(n==c)continue;
            neighbors[i++]=r*9+n;
        }
        for(int n=0;n<9;n++){
            if(n==r)continue;
            neighbors[i++]=c+n*9;
        }
        for(int n=0;n<9;n++){
            int t=xs+box[n];
            if(t==idx)continue;
            neighbors[i++]=t;
        }
          return neighbors;
    }    
}
final class TrialPoint {
  
    public int idx;
    public int val;
    public int[] cel = new int[81];
  
    public TrialPoint(int i, int v, int[] board) {
        idx = i;
        val = v;
        System.arraycopy(board, 0, cel, 0, 81);
    }
}

数独解法Java实现

private void GetFrames(string pPath,  string pSavedPath)
{
    DrawImage.Image gif = DrawImage.Image.FromFile(pPath);
    Imaging.FrameDimension fd = new Imaging.FrameDimension(gif.FrameDimensionsList[0]);
    int count = gif.GetFrameCount(fd);
    for (int i = 0; i < count; i++)
    {
        gif.SelectActiveFrame(fd, i);
        gif.Save(pSavedPath + "\\\\frame_" + i + ".jpg", ImageFormat.Jpeg);
    }
}

GIF抽帧

// 倒置字符串 修改版,再次修改版
static public string Reverse( string s )
{
    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );
}

C#倒置字符串方法

using System;
using System.Collections;
using System.Runtime.Remoting;

namespace Utilities
{
    /// <summary>
    /// This class is used to obtain a Remoting object from a registered list of well known client
    /// types. Use this class instead of manually calling Activator.GetObject.
    /// </summary>
    /// <remarks>This class was taken from the book "Advanced .NET Remoting" by Ingo Rammer (pg. 185-186).</remarks>
    public class RemotingHelper
    {
        private static bool m_isInit;
        private static IDictionary m_wellKnownTypes;

        // Constructor made private to prevent unwanted instantiation
        private RemotingHelper() { }

        /// <summary>
        /// Used to get the Remoting object.
        /// </summary>
        /// <param name="type">The type of object to get.</param>
        /// <returns>An Object representing the well known type.</returns>
        /// <exception cref="System.Runtime.Remoting.RemotingException">Thrown when the type is not found.</exception>
        /// <example>
        /// <code>
        ///     ICustomType obj = (ICustomType)RemotingHelper.GetObject(typeof(ICustomType));
        /// </code>
        /// </example>
        public static Object GetObject(Type type)
        {
            if (!m_isInit)
            {
                InitTypeCache();
            }

            WellKnownClientTypeEntry entr = (WellKnownClientTypeEntry) m_wellKnownTypes[type];
            if (entr == null)
            {
                string asmName = type.Assembly.GetName().Name;
                string typeName = type.ToString();
                string msg = string.Format("Type '{0}' not found in assembly '{1}'", typeName, asmName);
                throw new RemotingException(msg);
            }

            return Activator.GetObject(entr.ObjectType, entr.ObjectUrl);
        }

        private static void InitTypeCache() 
        {
            m_isInit = true;

            m_wellKnownTypes= new Hashtable();
            foreach (WellKnownClientTypeEntry entr in RemotingConfiguration.GetRegisteredWellKnownClientTypes()) 
            {
                if (entr.ObjectType == null)
                {
                    string msg = string.Format("Configured well-known client entry '{0}' could not be found", entr.ToString());
                    throw new RemotingException(msg);
                }
                m_wellKnownTypes.Add(entr.ObjectType, entr);
            }
        }
    }
}

C#远程访问通用类