给定若干个大写字母组成的单词,以逗号“,”相隔,判断由相同字母组成的单词将其输出,有多个相同的全部输出。

来源:互联网 发布:字体预览软件 编辑:程序博客网 时间:2024/05/16 06:53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace sameWords
{
    class Program
    {
        public static Program ob = new Program();
        static void Main(string[] args)
        {
            string source = "ABCAB,ABCD,DABCAB,ABC,CD,CABC,DCDCCD";
            string nust = null;
            char[] septer = { ',', ' ', '.' };
            string[] spli = source.Split(septer);
            string[] middle = new string[spli.Length];
            int[] plus = new int[spli.Length];
            StringBuilder number = new StringBuilder();
            ArrayList re = new ArrayList();
            ArrayList num = new ArrayList();
            for (int p = 0; p < spli.Length; p++)
            {
                middle[p] = ob.rmRepeated(spli[p].ToString());
            }
            Console.WriteLine("原始字符串:" + source);
            Console.WriteLine("去重后的单词及个单词对应ASCII码值之和:");
            for (int b = 0; b < middle.Length; b++)
            {
                for (int h = 0; h < middle[b].Length; h++)
                {
                    plus[b] += Convert.ToInt32(middle[b][h]);
                }
                re.Insert(b, plus[b]);
                Console.Write(middle[b].ToString());
                Console.WriteLine("," + plus[b].ToString());

            }

            for (int y = 0; y < re.Capacity; y++)//将arraylist对象re中相等的ASCII码值之和所对应的index添加到num中
            {
                for (int x = y + 1; x < re.Capacity - 1; x++)
                {
                    if (Object.Equals(re[y], re[x]))//判断re[index]所对应的值是否相等;
                    {
                        if (num.Contains(y) && num.Contains(x))//如果num中已包含判断过的值得索引,则继续查找
                        {
                            continue;
                        }
                        else if (!num.Contains(y) && !num.Contains(x))//如果不包含y、x则将他们都添加进去
                        {
                            num.Add(y);
                            num.Add(x);
                        }
                        else if (num.Contains(y) && !num.Contains(x))//如果包含y、x则将他们都添加进去
                        {
                            num.Add(x);
                        }
                        else
                        {
                            break;
                        }
                        continue;
                    }
                }
                num.Add(",");
            }
            foreach (Object nu in num)
            {
                nust += nu.ToString();
            }
            number.Append(nust);
            number.Replace(",,", ",");
            number.Replace(",,", ",");
            string[] dunm = number.ToString().Trim().Split(septer);
            Console.WriteLine("分组后的单词:");
            for (int d = 0; d < dunm.Length; d++)
            {
                for (int y = 0; y < dunm[d].Length; y++)
                {
                    if (dunm[d][y] != ',')
                    {
                        Console.Write(spli[Convert.ToUInt32(dunm[d][y].ToString())]);
                        Console.Write(" ");
                    }
                }
                if (dunm[d] != "")
                {
                    Console.WriteLine();
                }
            }
            Console.WriteLine("Press Any Key To Continue...");
            Console.ReadKey();
        }
        public String rmRepeated(String s)
        {
            int len = s.Length;
            int k = 0;
            int count = 0;
            String str = "";
            char[] c = new char[len];
            c = s.ToCharArray();//将字符串转换为字符数组形式
            for (int i = 0; i < len; i++)
            {
                k = i + 1;
                while (k < len - count)
                {
                    if (c[i] == c[k])
                    {
                        for (int j = k; j < len - 1; j++)
                        {
                            c[j] = c[j + 1];//出现重复字母,从k位置开始将数组往前挪位
                        }
                        count++;//重复字母出现的次数
                        k--;
                    }
                    k++;
                }

            }
            for (int i = 0; i < len - count; i++)
            {
                str += c[i].ToString();//将字符数组转换成字符串
            }
            return str;

        }
    }
}

0 0