Page 2 of 3

Re: C#

Posted: Thu Jun 28, 2012 3:24 pm
by Phantom
Of course!! In C# and not only in this language (like Java, Python, Ruby,…) the list is by reference. It means that when you do something like this:

Code: Select all

List<int> l_reverse = lista;
You say that <l_reverse> and <lista> gonna point to the same memory space. For this reason, if you change anything in <lista>, you are doing the same change in <l_reverse>, and vice-versa. If you want don’t happen this, you most copy the values one by one. Something like this:

Code: Select all

foreach (int x in lista) 
   l_reverse.Add(x);
l_reverse.Reverse();

Re: C#

Posted: Mon Jul 02, 2012 8:26 am
by axlluis
Thanks again phantom for you attetion, i already know that something like that happens, and i copy each element like you, but thats take a lot time, most be there other efficent solution to not get time limited excedeed.

Re: C#

Posted: Mon Jul 02, 2012 2:57 pm
by Phantom
Jajajajaja I regret to say that these is the only way. In C# exist an static function of the class <Array> named <Copy>, that do this for you, but... what do you think this method do behind??

You can do something like this:
Array.Copy(<source>, <destination>, <quantity>); //Find it in the documentation for more details.

Re: C#

Posted: Tue Jul 03, 2012 1:33 pm
by axlluis
thanks bro, help mw with this please, if i have this input:
aaaaa
bbbbb
ccccc
ddddd
eeeee
How , can i get that information , if is not given the amount of lines that you have to read, (ends of file), please write the code to do this.

Re: C#

Posted: Tue Jul 03, 2012 3:45 pm
by axlluis
Another question, how can i use Comparison for sort a list?

Re: C#

Posted: Tue Jul 03, 2012 5:52 pm
by Phantom
First Question:
axlluis wrote: thanks bro, help mw with this please, if i have this input:
aaaaa
bbbbb
ccccc
ddddd
eeeee
How , can i get that information , if is not given the amount of lines that you have to read, (ends of file), please write the code to do this.
Easy. In difference with other languages, in C# you haven’t question for the EOF. You read while the input is distinct to null. The code is the next one:

Code: Select all

string input = Console.Readline();
while(input != null)
{ 
     input = Console.Readline();
}
Second Question:
Another question, how can i use Comparison for sort a list?
Very easy too. I’ll show you 3 easy ways to do that(Sorting an array from major to minor):

Code: Select all

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

namespace COJ
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] values = { 2, 6, 8, 9, 3, 5, 4, 34, 45, 7, 678, 79, 8, 99, 453, 42, 4, 32 };
            Array.Sort<int>(values, (x, y) => y.CompareTo(x));        //First Way
            Array.Sort<int>(values, new Comparison<int>(MajorMinor)); //Second Way
            Array.Sort<int>(values, new CMP_Major_Minor());           //Third Way
        }

        class CMP_Major_Minor : Comparer<int>
        { public override int Compare(int x, int y) { return y.CompareTo(x); } }

        static int MajorMinor(int x, int y) { return y.CompareTo(x); }
    }
}
In Python:

Code: Select all

a = [2, 6, 8, 9, 3, 5, 4, 34, 45, 7, 678, 79, 8, 99, 453, 42, 4, 32]
a.sort(lambda x, y:cmp(y, x))
In Ruby:

Code: Select all

a = [2, 6, 8, 9, 3, 5, 4, 34, 45, 7, 678, 79, 8, 99, 453, 42, 4, 32]
a.sort! { |a1, a2| a2 <=> a1 }

Re: C#

Posted: Wed Jul 04, 2012 9:17 am
by axlluis
Very good, now im going to use that...

Re: C#

Posted: Thu Jul 12, 2012 1:51 pm
by axlluis
Hi phantom, please help me with this, is i have a double value, how many way are to round de value after the ",", for example two decimals.

Re: C# input ended by EOF

Posted: Fri Sep 25, 2015 11:23 am
by EricNG
Como puedo trabajar con EOF en C#... se que en C/C++ puede hacerse mediante el uso del cin o el get pero en C# el trabajo con la consola tiene algunas limitaciones que no se como salvar

arriba esta muy interesante la respuesta trabajando con null pero aun asi cuando el Console.ReadLine() recibe una cadena vacia no se hace null... se le asigna ""... entonces no resuelve el problema... pudieras explicarme mejor como aplicar eso a algun codigo concreto?

i don' t speak english very well and i try to do my best for comunicate with my several problems in this lenguaje... up is the translate to spanish if someone understand that better than this one... How can i work with EOF in C#... i know that in C/C++ can be represented with the use of the cin function but in c# the work with console have some limitations that i dont know how confront...
is very interesting the answer working with the null value up side... but when the Console.ReadLine() gets an empty chain assign an empty string and not a null value.... then not resolve the problem... can you explain better to me how to use that method in some concret code ?

Stop condition C#

Posted: Wed Oct 14, 2015 5:44 pm
by sherrera
Can anyone help me? I need to know how to handle input data for problem that does not specify the number of cases, for example problem 1418. What I want to know is the stop condition.
Thanks!