c#問題~~(拜託一定要用c#不要c++或c語言)

請設計一個程式:由鍵盤輸入一個正整數,列印出其質因數的連乘積,由小到大依序排列之,例如12=2*2*3。

希望可以幫我打程式出來~

謝謝~

Update:

那可以幫我看一下我打的程式還要加些什麼東西~

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

int n, q, w, x = 1,y;

Console.Write("請輸入一個正整數:");

Update 2:

n = int.Parse(Console.ReadLine());

Console.Write("{0}=",n);

for (q = 2; q <= n; q++)

{

Update 3:

for (w = 2; w < q; w++)

{

if (q % w == 0)

{

x = 0;

break;

Update 4:

}

x = 1;

}

if (x == 1&&n%w==0)

Console.Write(" {0}", w);

}

Console.ReadLine();

}

}

}

Update 5:

還有下面的問號是什麼意思啊??

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace ST1

    {

    class st

    {

    static void Main(string[] args)

    {

    Console.WriteLine("Enter a positive number N >= 2, N= ?");

    String s = Console.ReadLine();

    int i = Convert.ToInt16(s);

    String sOut = null;

    for(int j=2; j <= i;) if(i%j == 0) { sOut = ((null == sOut) ? (s + "=") : (sOut + "*")) + j; i /= j;} else ++j;

    Console.WriteLine(sOut);

    Console.ReadLine();

    }

    }

    }

    2009-11-23 22:15:49 補充:

    for(int j=2; j <= i;) if(i%j == 0) { sOut = ((null == sOut) ? (s + "=") : (sOut + "*")) + j; i /= j;} else ++j;

    can be written as:

    for(int j=2; j<=i; )

    if( 0 == i%j ) {

    sOut = ( (null == sOut) ? (s + "=") : (sOut + "*") ) + j;

    i = i/j;

    } else ++j;

    2009-11-23 22:19:47 補充:

    A ? B : C

    is an expression evaluated to B if A is true, otherwise, evaluated to C.

    For example:

    int i = 100;

    j = (0 < i) ? (i+1) : (i-1); // j will be assigned to i+1 or 101 because (0 < i) is true

    ? : is the only trinary operator in C# programming language.

    2009-11-23 22:49:39 補充:

    那可以幫我看一下我打的程式還要加些什麼東西~

    Not bad - I have NOT learned the Console,Write() format IO yet. But your program can be changed to:

    int n, q, w;

    Console.Write("請輸入一個正整數:");

    n = int.Parse(Console.ReadLine());

    Console.Write("{0}=",n);

    for (q = 2; q <= n; q++)

    {

    for (; 0 == n%q; n /= q)Console.Write(" {0}",q);

    }

    2009-11-23 22:53:31 補充:

    I think your code is easier to read than mine, but I still like to write:

    for (q = 2; q <= n; q++) for (; 0 == n%q; n /= q) Console.Write(" {0}",q);

    in one single line for the reason that it is a single logic to me.

Still have questions? Get your answers by asking now.