如何在C#中编写代码 [英] How do code this in C#

查看:73
本文介绍了如何在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#中编写代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆