C#

Discussion on other languages at the COJ. This is the place to clear your doubts about these languages, and to share with the community the new things you learn about them.
User avatar
Phantom
Posts: 58
Joined: 7 years ago
Location: Cuba
Gender: None specified

Re: C#

Post by Phantom » 6 years ago

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();



axlluis
Posts: 17
Joined: 6 years ago
Gender: None specified

Re: C#

Post by axlluis » 6 years ago

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.

User avatar
Phantom
Posts: 58
Joined: 7 years ago
Location: Cuba
Gender: None specified

Re: C#

Post by Phantom » 6 years ago

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.

axlluis
Posts: 17
Joined: 6 years ago
Gender: None specified

Re: C#

Post by axlluis » 6 years ago

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.

axlluis
Posts: 17
Joined: 6 years ago
Gender: None specified

Re: C#

Post by axlluis » 6 years ago

Another question, how can i use Comparison for sort a list?

User avatar
Phantom
Posts: 58
Joined: 7 years ago
Location: Cuba
Gender: None specified

Re: C#

Post by Phantom » 6 years ago

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 }
Last edited by Phantom on Wed Jul 04, 2012 9:46 am, edited 1 time in total.

axlluis
Posts: 17
Joined: 6 years ago
Gender: None specified

Re: C#

Post by axlluis » 6 years ago

Very good, now im going to use that...

axlluis
Posts: 17
Joined: 6 years ago
Gender: None specified

Re: C#

Post by axlluis » 6 years ago

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.

EricNG
Posts: 2
Joined: 3 years ago
Gender: None specified

Re: C# input ended by EOF

Post by EricNG » 3 years ago

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 ?

sherrera
Posts: 1
Joined: 3 years ago
Gender: None specified

Stop condition C#

Post by sherrera » 3 years ago

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!

Post Reply

Return to “Other languages”