求一个数组算法,不,是解法,只要能解就谢谢了。(80分,在线等~)

刘物峰 2009-01-14 10:28:19
求一个算法,我有一个数组{1,1,4,3,3},或{2,3,5,5,1}

{1,1,4,3,3}:找出相同的数,并相加存下来{2,4,6}

{2,3,5,5,1}:{2,3,10,1}

...全文
137 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiguixin 2009-01-14
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 jiguixin 的回复:]
少写了个变量:int len = 0;cou后面
[/Quote]
晕了,int len = -1;
jiguixin 2009-01-14
  • 打赏
  • 举报
回复
少写了个变量:int len = 0;cou后面
jiguixin 2009-01-14
  • 打赏
  • 举报
回复
有点乱不过功能实现了。
int[] a = { 1, 3, 4,4,1, 1, 3, 3 };

List<int> aa = new List<int>();
List<int> bb = new List<int>();
List<int> ll = new List<int>();
for (int i = 0; i < a.Length; i++)
{
aa.Add(a[i]);
}
int cou = 0;

for (int i = 0; i < aa.Count; i++)
{
while ((len = aa.IndexOf(aa[i], ++len)) >= 0)
{
cou++;
ll.Add(aa[len]);
}
if (cou != 0)
{
bb.Add(cou * aa[i]);
}
else
{
bb.Add(aa[i]);
}
cou = 0;

for (int j = 0; j < ll.Count; j++)
{
aa.Remove(ll[j]);
}
}
wanghui0380 2009-01-14
  • 打赏
  • 举报
回复
net3.0以上版本直接使用linq就可以了


var Girls = new[] {"Christine", "Eva", "Jean", "Novia", "Winnie"};
var PMs = new[] {"Eva", "Novia", "Vincent", "Williams", "Winnie"};
// 是 Girl 且是 PM (交集)
foreach (var s in Girls.Intersect(PMs))
{
Console.WriteLine(s
}
Console.WriteLine();






如果是net2.0的话,请参考
http://blog.blueshop.com.tw/uni2tw/archive/2008/02/14/54315.aspx
sprc_lcl 2009-01-14
  • 打赏
  • 举报
回复
string[] ints = new string[] { "2", "3", "5", "5", "1" };
int length = ints.Length;
string[] ints2 = new string[length];
int length2 = 0;
for (int i = 0; i < length; i++)
{
string thisint = ints[i];
bool isget = true;
for (int j = 0; j < length2; j++)
{
if (ints[j] == thisint)
{
isget = false;
break;
}
}
if (isget)
{
string thisintsum = thisint;
for (int j = i + 1; j < length; j++)
{
if (ints[j] == thisint)
{
thisintsum += thisint;
}
}
ints2[length2] = thisintsum;
length2++;
}
}
string[] ints3 = new string[length2];
Array.Copy(ints2, 0, ints3, 0, length2);

//改下变量类型就好
sprc_lcl 2009-01-14
  • 打赏
  • 举报
回复
 int[] ints = new int[] { 2, 3, 5, 5, 1 };
int length = ints.Length;
int[] ints2 = new int[length];
int length2 = 0;
for (int i = 0; i < length; i++)
{
int thisint = ints[i];
bool isget = true;
for (int j = 0; j < length2; j++)
{
if (ints[j] == thisint)
{
isget = false;
break;
}
}
if (isget)
{
int thisintsum = thisint;
for (int j = i + 1; j < length; j++)
{
if (ints[j] == thisint)
{
thisintsum += thisint;
}
}
ints2[length2] = thisintsum;
length2++;
}
}
int[] ints3 = new int[length2];
Array.Copy(ints2, 0, ints3, 0, length2);
刘物峰 2009-01-14
  • 打赏
  • 举报
回复
谢谢,如果是{s,s,d,c,c}
那又如何{ss,d,cc}
zgke 2009-01-14
  • 打赏
  • 举报
回复
你把所有的int换成 double不就好了~~
刘物峰 2009-01-14
  • 打赏
  • 举报
回复
我只是假设了二个数组
真时的不是整数
zgke 2009-01-14
  • 打赏
  • 举报
回复
int[] _Temp = new int[] { 1, 1, 4, 3, 3 };


IList<int> _Value = new List<int>();
IList<int> _ValueCount = new List<int>();


for (int i = 0; i != _Temp.Length; i++)
{
int _Index = _Value.IndexOf(_Temp[i]);
if (_Index == -1)
{
_Value.Add(_Temp[i]);
_ValueCount.Add(1);
}
else
{
_ValueCount[_Index]++;
}
}

int[] _IntValue = new int[_Value.Count];

for (int i = 0; i != _Value.Count; i++)
{
_IntValue[i] = _Value[i] * _ValueCount[i];
}
Crazy_Xia 2009-01-14
  • 打赏
  • 举报
回复
如果允许,排序后很好处理了
GTX280 2009-01-14
  • 打赏
  • 举报
回复
先对数组排序,然后分别对相同的元素相加
Crazy_Xia 2009-01-14
  • 打赏
  • 举报
回复
允许对数组进行排序吗
刘物峰 2009-01-14
  • 打赏
  • 举报
回复
List<string> int_33 = new List<string> ( );//重复的数
List<string> int_11 = new List<string> ( );//不重复的数

List<string> ints = new List<string> { "2" , "2" , "1" , "5" , "5" , "2" , "2","3" };
List<string> ints_1 = new List<string> { "2" , "5" };
foreach ( string s in ints_1 )
{
foreach ( string str in ints )
{
if ( s == str )
int_11.Add ( str );
}
string sTmp = "";
if ( int_11.Count > 0 )
{
foreach ( string stri in int_11 )
{
sTmp += stri;
}
int_33.Add ( sTmp );
int_11.Clear ( );
}
}
bool b = false;
foreach ( string s in ints )
{
foreach ( string str in ints_1 )
{
if ( s == str )
{
b = true;
continue;
}
}
if ( b == false )
{
int_11.Add ( s );
}
b = false;
}
刘物峰 2009-01-14
  • 打赏
  • 举报
回复
妈的,我认为这个有点简单却用了一上午的时间,唉~~~~~~~~~~
List<string> int_33 = new List<string> ( );//重复的数
List<string> int_11 = new List<string> ( );//不重复的数

List<string> ints = new List<string> { "2" , "2" , "1" , "5" , "5" , "2" , "2","3" };
List<string> ints_1 = new List<string> { "2" , "5" };
foreach ( string s in ints_1 )
{
foreach ( string str in ints )
{
if ( s == str )
int_11.Add ( str );
}
string sTmp = "";
if ( int_11.Count > 0 )
{
foreach ( string stri in int_11 )
{
sTmp += stri;
}
int_33.Add ( sTmp );
int_11.Clear ( );
}
}
bool b = false;
foreach ( string s in ints )
{
foreach ( string str in ints_1 )
{
if ( s == str )
{
b = true;
continue;
}
}
if ( b == false )
{
int_11.Add ( s );
}
b = false;
}
还是要谢谢各位,把分数分了

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧