如何在C#中编写代码

编程入门 行业动态 更新时间:2024-10-27 11:16:10
本文介绍了如何在C#中编写代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我有一个必须满足以下三个条件的要求 1一个或多个2 ----只保留一个2,放下休息 Ex:2,2,2,2 - > 2 2.一个或多个3 ----只保留一个3,放下休息 Ex:3,3,3, 3 - > 3 3.一个或多个2 +一个或多个3 .....只保留2个,放弃其余 例如:2,2,3,3,3,3 - > 2. 请帮我编写符合上述所有条件的c#代码。 谢谢。 我尝试了什么: if(count of 2) )> 1 保留前2并删除休息 if(count of 3)> 1 保留前3并删除休息 第3个条件?

解决方案

if(count of 2)> 0 保留前2并删除休息 if(count of 3)> 0 保留前3并删除休息 if(2> 0 &&计数3> 0) 保留前2并删除休息

问题没有正确制定,配方不是从一开始就开始的。 这是个主意:如果input是一个数组,您需要了解在初始化后不能从数组中删除任何元素。因此,您必须创建一个全新的数组对象并将其返回。对于每种情况,您可以计算输出数组中需要多少个数组元素,并使用此长度初始化一个新数组。然后将元素从输入数组复制到输出数组,但是,根据具体情况,不是全部。
-SA

Hi all, I have a required which should satisfy all three conditions below 1. one or more 2 ---- keep only one 2, drop rest Ex : 2,2,2,2 - > 2 2. one or more 3----keep only one 3, drop rest Ex: 3,3,3,3 -> 3 3. one or more 2 + one or more 3 .....keep only 2, drop the rest Ex : 2,2,3,3,3,3 -> 2. Please help me in writing c # code which satisfies all above conditions. Thank you. What I have tried: if (count of 2) > 1 retain first 2 and delete rest if(count of 3) > 1 retain first 3 and delete rest 3rd condition?

解决方案

if (count of 2) > 0 retain first 2 and delete rest if(count of 3) > 0 retain first 3 and delete rest if (count of 2 > 0 && count of 3 > 0) retain first 2 and delete rest

The question is not correctly formulated, the formulation is not started from the beginning. Here is the idea: if input is an array, you need to understand that you cannot delete any elements from the array after it is initialized. Therefore, you would have to create a brand new array object and return it. For each case, you count how many array elements you need in an output array and initialize a new array with this length. Then you copy elements from the input array to the output array, but, depending on the case, not all of them.
—SA

更多推荐

如何在C#中编写代码

本文发布于:2023-07-22 10:26:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1186691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!