English 中文(简体)
删除此 C# 程序的元素 [重复]
原标题:removing elements from this C# program [duplicate]
  • 时间:2012-05-25 15:56:16
  •  标签:
  • c#
  • .net
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Any chance to get unique records using Linq (C#)?

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

namespace WaysOf100
{
class WaysOf100Test
{
    static void Main(string[] args)
    {
        WaysOf100 test= new WaysOf100();
        test.Go();
        test.EliminateDuplicates();
    时 时
时 时

class WaysOf100
{        
    List<string> results = new List<string>();             

    public void Go()
    {
        int num = 5, temp=0;//to store the intermediate difference
        for (int i = 1; i <num; i++)
        {
            temp = num - i;
            for (int j = 1; j <= temp; j++)
            {
                if (temp % j == 0)
                {   
                    //Console.Write(i + " ");
                    string text = "";                        
                    text = i.ToString();                        
                    for (int k = 1; k <= (temp / j); k++)
                    {
                        //Console.Write(j + " ");  
                        text += j.ToString();
                    时 时
                    char[] rev = text.ToCharArray();
                    Array.Reverse(rev);                       
                    if(!(results.Contains(rev.ToString())))
                        results.Add(text);                        

                时 时
            时 时                
        时 时
    时 时

    public void EliminateDuplicates()
    {            
        //To eliminate the duplicates   

        /*for (int i = 0; i < results.Count; i++)
        {

            for (int j = 0; j < results.Count; j++)
            {

                if (!(results[i].Equals(results[j])))
                {
                    char [] rev = results[j].ToCharArray();
                    Array.Reverse(rev);                        
                    if (results[i]==rev.ToString())
                        results.Remove(rev.ToString());
                时 时                                       
            时 时
        时 时*/

        foreach (var result in results)
        {
           Console.WriteLine(result);
        时 时
        Console.WriteLine("Total number of elements is :{0时 时",results.Count);
    时 时
时 时

时 时

The result so far is 11111 122 14 2111 23 311 32 41

简言之,这就是我想要的:41的倒数是14, 14在列表中已经存在,所以我不想增加41。 同样,32的倒数是23, 也存在,因此32不应该增加。 但是,我写来实现功能的这一部分可能并没有带来预期的效果。

if(!(results.Contains(rev.ToString())))
      results.Add(text);
最佳回答

您遇到的问题是 rev. ToString () return " "System.Char[]]" , 而不是您想要/期待的字符串值。 对于您的逻辑, 请尝试如下 :

    public void EliminateDuplicates()
    {
        //Eliminate the duplicates    

        for (int i = 0; i < results.Count; i++)
        {
            for (int j = 0; j < results.Count; j++)
            {
                if (!(results[i].Equals(results[j])))
                {
                    char[] rev = results[j].ToCharArray();
                    char[] forward = results[i].ToCharArray();
                    Array.Reverse(rev);
                    bool bEqual = true;
                    for( int n = 0 ; n < results[j].Length && true == bEqual ; n++ )
                    {
                        if( rev[n] != forward[n] )
                        {
                            bEqual = false;
                        }
                    }
                    if( true == bEqual)
                        results.Remove(results[j] );
                }
            }
        }

        foreach (var result in results)
        {
            Console.WriteLine(result);
        }
        Console.WriteLine("Total number of elements is : {0} ", results.Count);
    }
问题回答

我终于自己解决了

if (!(results.Contains(new string(rev))))
    results.Add(text);

rev.toString () 改为 new string(rev) ,现在运作良好。我想要的已经实现。非常感谢各位的帮助。

反转是否是您想要检查的唯一案例? 一种方法是在比较之前将结果与排序顺序等相提并论。 所以在比较之前将132和213转换为123。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签