算法求解,控制台

走出一条新道路 2013-02-20 04:52:32
有两个一维数组,长度相等,数量不等,现在要两组数中对应的值相除,得到第三组数,找出第三组数中的最大值,并将与之对应的第一组中的数值自减1,再次相除,重复之前的过程,我是这样做的,求大神指出问题,谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NumMax
{
class Program
{
static void Main(string[] args)
{
float[] arrFirst = { 3, 2, 6, 4, 1, 9 };
float[] arrSecond = { 7, 14, 7, 12, 5, 22 };
float[] arrThird = new float[arrFirst.Length];
float[] arrForth = new float[arrFirst.Length];
//float temp = 0;
float max = arrThird[0];
int index = 0;
for (int x = 0; x < arrFirst[index]; x++)
{
for (int i = 0; i < arrFirst.Length; i++)
{
arrThird[i] = arrFirst[i] / arrSecond[i];
}
for (int j = 0; j < arrThird.Length; j++)
{
if (arrThird[j] > arrThird[index])
{
index = j;
max = arrThird[j];
arrForth[j] = max;
}
if (arrForth[j] == 0)
{
return;
}
else
{
Console.WriteLine(arrForth[j]);
Console.Read();
}
}
arrFirst[index] -= 1;
}
}
}
}

...全文
194 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 3 楼 gxingmin 的回复:
linq的用法 float[] arrFirst = { 3, 2, 6, 4, 1, 9 }; float[] arrSecond = { 7, 14, 7, 12, 5, 22 }; float[] arrThird = arrFirst.Select((x, i) => x / arrSecond[i])……
谢谢,我已经用一般的方法解决了,不过你提供的思路很好,学习了
gxingmin 2013-02-20
  • 打赏
  • 举报
回复
linq的用法 float[] arrFirst = { 3, 2, 6, 4, 1, 9 }; float[] arrSecond = { 7, 14, 7, 12, 5, 22 }; float[] arrThird = arrFirst.Select((x, i) => x / arrSecond[i]).ToArray<float>(); //数组1和数组2对应的相除得第3个数组 var max = arrThird.Max(); //取第三个数组最大的值 var index = Array.IndexOf(arrThird, max); //去最大值对应的位置索引 Console.WriteLine(max); //输出这个最大值 Console.Read(); if(index>-1) //其实这个判断可以去掉,肯定会大于-1的 arrFirst[index]--; //将最大值对应位置的第一个数组自减1
  • 打赏
  • 举报
回复
引用 1 楼 gxingmin 的回复:
C# code?123456789101112 float[] arrFirst = { 3, 2, 6, 4, 1, 9 }; float[] arrSecond = { 7, 14, 7, 12, 5, 22 }; float[] arrThird = arrFirst.Select((x, i) => x / arrS……
这个写法太高级了。。。很多都没用过,能稍微通俗一点吗?谢谢
gxingmin 2013-02-20
  • 打赏
  • 举报
回复
  float[] arrFirst = { 3, 2, 6, 4, 1, 9 };
            float[] arrSecond = { 7, 14, 7, 12, 5, 22 };

            float[] arrThird = arrFirst.Select((x, i) => x / arrSecond[i]).ToArray<float>();
            var max = arrThird.Max();
            var index = Array.IndexOf(arrThird, max);

            Console.WriteLine(max); 
            Console.Read();

            if(index>-1)
                arrFirst[index]--;

110,499

社区成员

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

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

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