注:此为第六题的图
1.把分拣奇数偶数的程序用泛型实现。(List<int>)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 分拣奇偶
{
class Program
{
static void Main(string[] args)
{
string num = "2 3 4 5 6 7 8 9";
string[] nums = num.Split();
List<string > listOdd = new List<string>();
List<string > listEven = new List<string >();
for (int i = 0; i < nums.Length; i++)
{
if (Convert.ToInt32(nums[i]) % 2 != 0)
{
listOdd.Add(nums[i]);
}
else
{
listEven.Add(nums[i]);
}
}
listOdd.AddRange(listEven);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < listOdd.Count; i++)
{
sb.Append(listOdd[i] + " ");
}
Console.WriteLine(sb);
Console.ReadKey();
}
}
}
2.将int数组中的奇数放到一个新的int数组中返回。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 奇数放int中
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List<int> listOdd=new List<int>();
for(int i=0;i<numbers.Length;i++)
{
if(numbers[i]%2!=0)
listOdd.Add(numbers[i]);
}
int[] numOdd = listOdd.ToArray();
foreach (int tenp in numOdd)
{
Console.WriteLine(tenp);
}
Console.ReadKey();
}
}
}
3.从整数的List<int>中找最大值,不适用Max方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 从整数中找最大
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int temp = 0;
for (int i = 0; i < list.Count; i++)
{
if (list[i] > temp)
{
temp = list[i];
}
}
Console.WriteLine("max is:" + temp);
}
}
}
4.计算字符串中每种字符出现的次数。(面试题)“welcome to china! this is a beautiful county, i think you will like it.here is The great wall”提示:Dictionary<char,int>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 计算字符出现的次数
{
class Program
{
static void Main(string[] args)
{
string str = "Welcome to china! this is a beautiful county, i think you will like it.here is The great wall";
Console.WriteLine("在"+str+"中");
str = str.ToLower();
Dictionary<char, int> dict2 = new Dictionary<char, int>();
for (int i = 0; i < str.Length; i++)
{
if (char.IsLetter(str[i]))
{
if (dict2.ContainsKey(str[i]))
{
dict2[str[i]]++;
}
else
{
dict2.Add(str[i], 1);
}
}
}
foreach (KeyValuePair<char, int> kv in dict2)
{
Console.WriteLine("字符:{0},出现了{1}次",kv.Key,kv.Value);
}
Console.ReadKey();
}
}
public class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}
}
5.把1,2,3转换为壹 贰 叁
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 变简为繁
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个数:") ;
string number = Console.ReadLine();
string str = "1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零";
Dictionary<char, char> dict = new Dictionary<char, char>();
string[] parts = str.Split(' ');
for (int i = 0; i < parts.Length; i++)
{
dict.Add(parts[i][0], parts[i][1]);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < number.Length; i++)
{
sb.Append(dict[number[i]]);
}
Console.WriteLine(sb);
Console.ReadKey();
}
}
}
6.简体繁体字转换。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace 变简体为繁体
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Dictionary<char, char> dict = new Dictionary<char, char>();
private void btnConvert_Click(object sender, EventArgs e)
{
//获取用户输入的简体中文
string input = txtCHS.Text.Trim();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
//判断字典中是否包含这个键
if (dict.ContainsKey(input[i]))
{
sb.Append(dict[input[i]]);
}
else
{
//如果dict中不包含,原样输出
sb.Append(input[i]);
}
}
txtCHT.Text = sb.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines("ST.txt");
for (int i = 0; i < lines.Length; i++)
{
dict.Add(lines[i][0], lines[i][2]);
//lines[0] ---"啊=啊"
}
}
private void Form1_Layout(object sender, LayoutEventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 变简体为繁体
{
class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}