初识c#

输入三角形或者长方形边长,计算其周长和面积并输出
 1 //输入三角形或者长方形边长,计算其周长和面积并输出
 2 using System;
 3 
 4 namespace tur1_1
 5 {
 6     class Program
 7     {
 8         static double calTriangleArea(double a,double b,double c)
 9         {
10             double p = (a + b + c) / 2;
11             double res = Math.Sqrt(p * (p - a) * (p - b) * (p - c));
12             return res;
13         }
14 
15         static double calTrianglePerimeter(double a,double b,double c)
16         {
17             double res = a + b + c;
18             return res;
19         }
20 
21         static double calRectangleArea(double a,double b)
22         {
23             double res = a * b;
24             return res;
25         }
26 
27         static double calRectanglePerimeter(double a,double b)
28         {
29             double res = (a + b) * 2;
30             return res;
31         }
32         static void Main(string[] args)
33         {
34             bool flag = true;
35             do
36             {
37                 Console.WriteLine("input rectangle or triangle side length,2 or 3 params split with ' ' as :2  3 ");
38                 string str = Console.ReadLine();
39                 string[] nums = str.Split(new string[] { " " }, StringSplitOptions.None);
40                 double[] num = new double[3];
41                 int i = 0;
42                 foreach (string n in nums)
43                 {
44                     num[i] = Convert.ToDouble(n);
45                     i++;
46                 }
47                 switch (i)
48                 {
49                     case 2:
50                         double rc = calRectanglePerimeter(num[0], num[1]);
51                         Console.WriteLine("Rectangle perimeter is " + rc);
52                         double rs = calRectangleArea(num[0], num[1]);
53                         Console.WriteLine("Rectangle area is " + rs);
54                         break;
55                     case 3:
56                         double tc = calTrianglePerimeter(num[0], num[1], num[2]);
57                         Console.WriteLine("Triangle primeter is " + tc);
58                         double ts = calTriangleArea(num[0], num[1], num[2]);
59                         Console.WriteLine("Triangle area is " + ts);
60                         break;
61                     default:
62                         Console.WriteLine("input error");
63                         break;
64                 }
65                 Console.WriteLine("continue(0),exit(1)");
66                 string ck = Console.ReadLine();
67                 int k = Convert.ToInt32(ck);
68                 if(k!=0)
69                 {
70                     flag = false;
71                 }
72             } while (flag);
73         }
74     }
75 }
View Code
根据输入的月份判断所在季节
 1 //根据输入的月份判断所在季节
 2 using System;
 3 
 4 namespace tur1_2
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             bool flag = true;
11             do
12             {
13                 Console.WriteLine("input month number");
14                 string str = Console.ReadLine();
15                 int month = Convert.ToInt32(str);
16                 switch (month)
17                 {
18                     case 3:
19                     case 4:
20                     case 5:
21                         Console.WriteLine("春季");
22                         break;
23                     case 6:
24                     case 7:
25                     case 8:
26                         Console.WriteLine("夏季");
27                         break;
28                     case 9:
29                     case 10:
30                     case 11:
31                         Console.WriteLine("秋季");
32                         break;
33                     case 12:
34                     case 1:
35                     case 2:
36                         Console.WriteLine("冬季");
37                         break;
38 
39                     default:
40                         Console.WriteLine("input error");
41                         break;
42                 }
43                 Console.WriteLine("continue(0),exit(1)");
44                 string ck = Console.ReadLine();
45                 int k = Convert.ToInt32(ck);
46                 if (k != 0)
47                 {
48                     flag = false;
49                 }
50             } while (flag);
51         }
52     }
53 }
View Code
用 while 循环语句实现下列功能:有一篮鸡蛋,不止一个,有人两个两
个数,多余一个,三个三个数,多余一个,再四个四个地数,也多余一个,请问这篮鸡蛋至
少有多少个
 1 /*
 2 用 while 循环语句实现下列功能:有一篮鸡蛋,不止一个,有人两个两
 3 个数,多余一个,三个三个数,多余一个,再四个四个地数,也多余一个,请问这篮鸡蛋至
 4 少有多少个
 5 */
 6 using System;
 7 
 8 namespace tur1_3
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             int i = 2;
15             while(i%2!=1||i%3!=1||i%4!=1)
16             {
17                 i++;
18             }
19             Console.WriteLine(i);
20         }
21     }
22 }
View Code
计算数组中奇数之和和偶数之和
 1 //计算数组中奇数之和和偶数之和
 2 using System;
 3 
 4 namespace tur1_4
 5 {
 6     class Program
 7     {
 8         static void Main(string[] args)
 9         {
10             bool flag = true;
11             do
12             {
13                 Console.WriteLine("input number array length");
14                 string str = Console.ReadLine();
15                 int length = Convert.ToInt32(str);
16                 int[] nums = new int[length];
17                 long even = 0;
18                 long odd = 0;
19                 for(int i=0;i<length;i++)
20                 {
21                     Console.WriteLine("input " + (i+1) + " number:");
22                     string s=Console.ReadLine();
23                     nums[i] = Convert.ToInt32(s);
24                     if(nums[i]%2==0)
25                     {
26                         even += nums[i];
27                     }
28                     else if(nums[i]%2==1)
29                     {
30                         odd += nums[i];
31                     }
32                     else
33                     {
34                         Console.WriteLine("error");
35                         break;
36                     }
37                 }
38                 Console.WriteLine("奇数和为 " + odd);
39                 Console.WriteLine("偶数和为 " + even);
40 
41                 Console.WriteLine("continue(0),exit(1)");
42                 string ck = Console.ReadLine();
43                 int k = Convert.ToInt32(ck);
44                 if (k != 0)
45                 {
46                     flag = false;
47                 }
48             } while (flag);
49         }
50     }
51 }
View Code
找一个二维数组中的鞍点
  1 //找一个二维数组中的鞍点
  2 using System;
  3 
  4 namespace tur1_5
  5 {
  6     class Program
  7     {
  8         static void Main(string[] args)
  9         {
 10             bool flag = true;
 11             do
 12             {
 13                 Console.WriteLine("input number array row length");
 14                 string srow = Console.ReadLine();
 15                 int row = Convert.ToInt32(srow);
 16                 Console.WriteLine("input number array col length");
 17                 string scol = Console.ReadLine();
 18                 int col = Convert.ToInt32(scol);
 19                 if(row<1||col<1)
 20                 {
 21                     continue;
 22                 }
 23                 int[,] nums = new int[row,col];
 24                 for (int i = 0; i < row; i++)
 25                 {
 26                     for(int j=0;j<col;j++)
 27                     {
 28                         Console.WriteLine("input " + (i + 1) + " row " + (j + 1) + " col number:");
 29                         string s = Console.ReadLine();
 30                         nums[i,j] = Convert.ToInt32(s);
 31                     }
 32                 }
 33                 for (int i = 0; i < row; i++)
 34                 {
 35                     for (int j = 0; j < col; j++)
 36                     {
 37                         Console.Write(nums[i, j] + " ");
 38                     }
 39                     Console.Write("
");
 40                 }
 41 
 42                 int[] saddle = new int[row*col];
 43                 int sadlen = 0;
 44 //                int sadmax = row > col ? row : col;
 45 //                bool sadflag = false;
 46                 int sadindex = 0;
 47 
 48                 /*
 49                 int rowmaxvalue;
 50                 int[] rowmax = new int[col];
 51                 int colminvalue;
 52                 int[] colmin = new int[row];
 53                 */
 54                 bool[] rowflag = new bool[col];
 55                 bool[] colflag = new bool[row];
 56                 
 57 
 58                 for(int i=0;i<row;i++)
 59                 {
 60                     int maxtemp = int.MinValue;
 61                     for(int j=0; j<col;j++)
 62                     {
 63                         if(maxtemp<nums[i,j])
 64                         {
 65                             maxtemp = nums[i, j];
 66                             rowflag[i] = true;
 67                         }
 68                     }
 69                     if(rowflag[i])
 70                     {
 71                         for(int j=0;j<col;j++)
 72                         {
 73                             if(nums[i,j]==maxtemp)
 74                             {
 75                                 bool kflag = true;
 76                                 for(int k=0;k<row;k++)
 77                                 {
 78                                     if(maxtemp>nums[k,j])
 79                                     {
 80                                         kflag = false;
 81                                         continue;
 82                                     }
 83                                 }
 84                                 if(kflag)
 85                                 {
 86                                     sadlen++;
 87                                     saddle[sadindex] = maxtemp;
 88                                     sadindex++;
 89                                 }
 90                             }
 91                         }
 92                     }
 93                 }
 94 
 95 
 96 
 97 
 98                 /*
 99                 for (int i = 0; i < row; i++)
100                 {
101                     int temp = int.MinValue;
102                     rowflag[i] = false;
103                     for (int j = 0; j < col; j++)
104                     {
105                         if(temp<nums[i,j])
106                         {
107                             temp = nums[i, j];
108                             rowmax[i] = j;
109                             rowflag[i] = true;
110                         }
111                     }
112                 }
113                 for (int j = 0; j < col; j++)
114                 {
115                     int temp = int.MaxValue;
116                     colflag[j] = false;
117                     for (int i = 0; i < row; i++)
118                     {
119                         if (temp > nums[i, j])
120                         {
121                             temp = nums[i, j];
122                             colmin[j] = i;
123                             colflag[j] = true;
124                         }
125                     }
126                 }
127                 for(int p=0;p<col;p++)
128                 {
129                     if(rowflag[p])
130                     {
131                         if(colflag[rowmax[p]])
132                         {
133                             if(colmin[rowmax[p]] ==p)
134                             {
135                                 saddle[sadindex] = nums[colmin[rowmax[p]], rowmax[p]];
136                                 sadlen++;
137                             }
138                         }
139                     }
140                 }
141                 */
142 
143 
144 
145 
146                 if(sadlen>0)
147                 {
148                     Console.Write("saddle point(s):");
149                     for (int i=0;i<sadlen;i++)
150                     {
151                         Console.Write(saddle[i] + " ");
152                     }
153                     Console.Write("
");
154                 }
155                 else
156                 {
157                     Console.WriteLine("no saddle point");
158                 }
159                 Console.WriteLine("continue(0),exit(1)");
160                 string ck = Console.ReadLine();
161                 int end = Convert.ToInt32(ck);
162                 if (end != 0)
163                 {
164                     flag = false;
165                 }
166             } while (flag);
167         }
168     }
169 }
View Code
原文地址:https://www.cnblogs.com/unknowcry/p/11863013.html