给定若干个大写字母组成的单词,以逗号“,”相隔,判断由相同字母组成的单词将其输出,有多个相同的全部输出。
来源:互联网 发布:字体预览软件 编辑:程序博客网 时间: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;
}
}
}
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
- 给定若干个大写字母组成的单词,以逗号“,”相隔,判断由相同字母组成的单词将其输出,有多个相同的全部输出。
- leetcode题目 聚合相同的字母组成的单词
- 8由空格和字母组成的字符串,输出最长单词
- 字符串问题1:单词是否由相同字母组成
- 字符串之输出所有字母组合相同的单词
- leetcode 相同字母组成的不同单词归为一类即所谓的变位词
- leetcode49-Group Anagrams(同构词(相同字母组成的单词)分类)
- [ACM]输入若干个单词,输出它们的平均长度。单词只包含大写字母和小写字母,用一个或多个空格隔开。
- C#--输入一个由若干字符组成的字符串,输出其中的大写字母、小写字母、数字和其他字符的个数。
- 编写一个程序,输入一串不超过60个字符组成的且不包含换行符的字符串,将字符串中的A到Z的字母按英文字典字母顺序重新排列,排列后的单词的长度要与原始句子中的长度相同,并且要求只对A到Z的字母重新排列,其
- 如何判断一个字符串是否由相同的字符组成
- 判断两个字符串是否由相同的字符组成
- 判断两个字符串是否由相同的字符组成
- Java 判断两个字符串是否由相同的字符组成
- 如何判断两个字符串是否由相同的字符组成
- 判断两个字符串是否由相同的字符组成
- 242.两个字符串是否是由相同字母(出现次数也相同)组成的
- 给一个由n个单词组成的字符串排序
- Xcode 制作framework
- VS2010下对话框添加菜单栏和工具栏及其启用,禁用同步
- openstack常见错误
- 单链表的增删
- Validform.js 实时验证用户名是否已经注册过
- 给定若干个大写字母组成的单词,以逗号“,”相隔,判断由相同字母组成的单词将其输出,有多个相同的全部输出。
- 静态绑定,异常
- 用了这么久的组件监听类,看了无数博文,终于真正理解回调机制
- 利用匿名管道向Android手机发送adb命令
- 移动web端<input type="number">手机上点击直接打开数字键盘,并修改样式去除最右端上下箭头
- 调试stm32时经常出现"Flash Download Failed-"Cortex-M3"
- maven 常用命令goal
- Thinkpad T440p安装Linux的种种问题
- java笔记——数据类型