zoukankan      html  css  js  c++  java
  • 一些常用算法 练手的的代码

    看了平时写的一些东西 做的一些例子 让电脑按照你的意图去显示一些东西 你会发现 “原来编程真的很有意思的”

    1 using System;
    2  using System.Collections.Generic;
    3  using System.Linq;
    4  using System.Text;
    5  using System.Data;
    6 using System.Data.SqlClient;
    7 using System.IO;
    8
    9 namespace studyDemo
    10 {
    11 class Program
    12 {
    13 staticvoid Main(string[] args)
    14 {
    15
    16 //recursion(1, 0);
    17 //sortDemo();
    18 //commonMultiple(13, 4);
    19
    20 //getDir(new DirectoryInfo(@"c:\"),0);
    21 //numToString(757657578);
    22
    23 //string str = "hello world!!";
    24 //foreach (char c in str)
    25 // Console.WriteLine(c);
    26 }
    27
    28 staticvoid numToString(int num)//把整型转换成字符串
    29 {
    30 char[] str = { '', '', '', '', '', '', '', '', '' };
    31 int indx =0;
    32
    33 while (num >0)
    34 {
    35 int tmpNum = num %10;
    36 char tmpChar;
    37 switch (tmpNum)
    38 {
    39 case1:
    40 tmpChar ='';
    41 break;
    42 case2:
    43 tmpChar ='';
    44 break;
    45 case3:
    46 tmpChar ='';
    47 break;
    48 case4:
    49 tmpChar ='';
    50 break;
    51 case5:
    52 tmpChar ='';
    53 break;
    54 case6:
    55 tmpChar ='';
    56 break;
    57 case7:
    58 tmpChar ='';
    59 break;
    60 case8:
    61 tmpChar ='';
    62 break;
    63 case9:
    64 tmpChar ='';
    65 break;
    66 default:
    67 tmpChar ='';
    68 break;
    69 }
    70 if (indx < str.Length)
    71 str[str.Length - indx -1] = tmpChar;
    72 indx++;
    73
    74 num /=10;
    75 }
    76
    77 Console.WriteLine(newstring(str).Trim());
    78 }
    79
    80 staticvoid commonMultiple(int num1, int num2)//求最小公倍数 的算法
    81 {
    82 int comNum =0;
    83 int tmp =-1;
    84 if (num1 > num2)
    85 {
    86 tmp = num1;
    87 num1 = num2;
    88 num2 = tmp;
    89 }
    90
    91 for (int i = num2; i <= num1 * num2; i++)
    92 {
    93 if (i % num1 ==0&& i % num2 ==0)
    94 {
    95 comNum = i;
    96 break;
    97 }
    98 }
    99 if (tmp ==-1)
    100 Console.WriteLine("{0}跟{1}的最小公倍数是:{2}", num1, num2, comNum);
    101 else
    102 Console.WriteLine("{0}跟{1}的最小公倍数是:{2}",num2 ,num1 , comNum);
    103 }
    104
    105 staticvoid sortDemo()//排序算法
    106 {
    107 int[] arr = { 4, 5, 3, 678, 12, 78, 90, 1 };
    108
    109 for (int i =0; i < arr.Length; i++)
    110 {
    111 for (int j =0; j < arr.Length -1; j++)
    112 {
    113 if (arr[j +1] > arr[j])
    114 {
    115 int tmp;
    116 tmp = arr[j];
    117 arr[j] = arr[j +1];
    118 arr[j +1] = tmp;
    119 }
    120 }
    121 }
    122
    123 foreach (int n in arr)
    124 {
    125 Console.WriteLine(n);
    126 }
    127 }
    128
    129 staticvoid getDir(DirectoryInfo dir, int level)//递归算法 遍历目录
    130 {
    131 foreach (DirectoryInfo d in dir.GetDirectories())
    132 {
    133 for (int i =0; i < level; i++)
    134 Console.Write("");
    135 Console.WriteLine(d.Name);
    136 getDir(d, level +1);
    137 }
    138 }
    139
    140 staticvoid recursion(int node, int level)//递归算法 树状图
    141 {
    142 //数据库里表的结构:
    143 /*
    144 * create table tree
    145 * (
    146 * id int primary key identity (1,1) ,
    147 * nm varchar(20),
    148 * pId int
    149 * )
    150 * id主键自增列 nm名称 pId父ID(从属于)
    151 */
    152 string sql ="select * from tree where pid=@1";
    153 SqlConnection conn =new SqlConnection(@"server=.\sqlexpress;database=tempdb;Integrated Security=true");
    154 conn.Open();
    155
    156 SqlCommand cmd =new SqlCommand(sql, conn);
    157 cmd.Parameters.Add(new SqlParameter("@1", node));
    158
    159 SqlDataReader dr = cmd.ExecuteReader();
    160
    161 while (dr.Read())
    162 {
    163 for (int i =0; i <= level; i++)
    164 Console.Write("---");
    165 Console.WriteLine(dr["nm"]);
    166 int id = dr.GetInt32(0);
    167
    168 SqlConnection conn2 =new SqlConnection(@"server=.\sqlexpress;database=tempdb;Integrated Security=true");
    169 conn2.Open();
    170 SqlCommand cmd2 =new SqlCommand("select count(*) from tree where pid=@1", conn2);
    171 cmd2.Parameters.Add(new SqlParameter("@1", id));
    172 object hasChild = cmd2.ExecuteScalar();
    173 if (Convert.ToInt32(hasChild) !=0)
    174 recursion(id, level +1);
    175
    176 conn2.Close();
    177 }
    178
    179 dr.Close();
    180 conn.Close();
    181 }
    182 }
    183 }
    184
  • 相关阅读:
    Intent flag 与启动模式的对应关系
    Activity的启动模式---总结
    NDK---使用,开发步骤
    自定义ViewGroup
    android:process结合activity启动模式的一次实践
    Java单元测试之覆盖率统计eclemma
    Java单元测试之JUnit篇
    结对编程1
    个人作业1——四则运算题目生成程序(基于控制台)
    Coding使用方法
  • 原文地址:https://www.cnblogs.com/assassinx/p/1809659.html
Copyright © 2011-2022 走看看