זקוק להכוונה בתרגיל ב-C# - עמוד 3 - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

זקוק להכוונה בתרגיל ב-C#


gshhar

Recommended Posts

שוב אני אומר, אני מתחיל לכתוב ונתקע, בקוד הבא הכנסתי למערך את 2 כי הוא נתון ראשוני ומתחיל לבדוק מהמספר 3, עכשיו הבעיה של היא ליצור לולאה שאם יש לי מספר למשל 4 אז שתבדוק מול 2 המספרים שכבר נמצאים במערך ואני יודע שאמרת לי לא להכניס את זה למערך אבל אני פשוט לא מצליח לכתוב את זה



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


namespace T206
{
class Program
{
static void Main(string[] args)
{
//define prime number array
int[] PrimeNum = new int[29];


//insert first prime number into array
PrimeNum[0] = 2;
Console.WriteLine(2);


//number of primes currently in the array
int nPrimes = 1;


//first munber to check
int num = 3;


int count = 1;


for (int i = 1; i < nPrimes; i++)
{
if (num % PrimeNum[i] == 0)
{
num++;
}
}


Console.ReadLine();
}
}
}

קישור לתוכן
שתף באתרים אחרים

  • תגובות 51
  • נוצר
  • תגובה אחרונה

ה-3 שורות האחרונות לא ממש ברורות לי

אני כבר מיואש, אני עם התרגיל הזה מאתמול ועד עכשיו לא היו לי בעיות בתרגילים אז חשוב לי מאוד להבין אותו ולפתור אותו.

קישור לתוכן
שתף באתרים אחרים

כן אבל בכל מקרה הייתי רוצה לנסות ולפתור את זה בלי ה-BOOL

יש מצב שאתה פותר לי את זה ואני יסתכל על זה ויבין ? אני פשוט מתחרבש על זה וזה מתסכל אותי

אגב מה הכוונה במעקב שרשמו לי פה ?

קישור לתוכן
שתף באתרים אחרים

או אם אפשר לכוון אותי למה שאני רוצה לעשות אני אשמח, בעירון אני מתחיל מהמספר 3: בודק אותי מול מה שיש לי ראשוני שזה 2 ואז מכניס אותו למערך, ןאת המספר הבא שהוא 4 בודק מול מה שיש לי במערך (כידוע באחד מהם הוא לא מתחלק אז הוא לא ראשוני) ואז שמגיע 5 אני מתקשה לכתוב איך הוא יבדק מול 2 ומול 3 שקיימים כבר במערך, עכשיו אני יודע שאמרת לי לעשות את זה בדרך שונה אבל בתרגיל רשום לי ככה:

Prime Numbers: A Prime Number cannot be divided without remainder by any number, except by 1 and itself. For example: 7 is prime, because it cannot be divided by any number, except 7 and 1.

Actually a number n is prime if it is cannot be divided into any prime number which is less than n. So in the case of 7, you only need to check if it is divided by 5, 3 and 2 to decide that it is prime.

Find the first 30 prime numbers by using the following algorithm:

We all know that 2 is prime. Now we check 3: since 3 cannot be divided by 2 – it is also prime. Then we check 4 by trying to divide it by 3 and 2. Since it is divided by 2 – it is not prime. Now we check 5 by dividing it by 3 and 2. We continue with this process until we find the first 30 (or you may go even more) prime numbers.

אתה צריך להבין שלכתוב קוד שבודק מספר בודד אם הוא ראשוני או לא אני יודע וכבר עשיתי את זה ללא מערך אבל פה זה שונה.

קישור לתוכן
שתף באתרים אחרים

הוא מגיב בת'רד כדי שהת'רד יופיע ב"הראה תגובות חדשות להודעות שלי".

אם אתה מתעקש:



namespace T206
{
class Program
{
static void Main(string[] args)
{
//define prime number array
int[] PrimeNum = new int[30];

//number of primes currently in the array
int nPrimes = 0;

//first munber to check
int num = 2;

//while we haven't yet collected 30 primes
while (nPrimes < 30)
{
int i = 0;

// go over all the primes currently in the array
for (i = 0 ; i < nPrimes ; i++)
{
if (num % PrimeNum[i] == 0)
{
// number is not prime - no need to continue the loop
break;
}
}

// If we finished the loop without breaking, the number is prime
if (i == nPrimes)
{
// insert the number to the primes array
PrimeNum[nPrimes] = num;
nPrimes++;
}

// Get the next number to check
num++;
}


Console.ReadLine();
}
}
}

(כמובן לא הכנסתי את הקטע של ההדפסה, אני מניח שאתה יודע לעשות את זה בעצמך)

שים לב שהשתמשתי בשתי לולאות - החיצונית שעוברת על מספרים, והפנימית שבודקת אם המספר הנתון הוא ראשוני.

קישור לתוכן
שתף באתרים אחרים

using System;
namespace T206
{
class Program
{
static void Main(string[] args)
{
//define prime number array
int[] PrimeNum = new int[30];


//number of primes currently in the array
int nPrimes = 0;


//first munber to check
int num = 2;


//while we haven't yet collected 30 primes
while (nPrimes < 30)
{
int i = 0;


// go over all the primes currently in the array
for (i = 0; i < nPrimes; i++)
{
if (num % PrimeNum[i] == 0)
{
//number is not prime - no need to continue the loop
break;
}
}


// If we finished the loop without breaking, the number is prime
if (i == nPrimes)
{
// insert the number to the primes array
PrimeNum[nPrimes] = num;
nPrimes++;
}


// Get the next number to check
num++;
}




Console.ReadLine();
}
}}

מוזר כי כשאני לוחץ על השגיאה לראות איפה היא הוא מוביל אותי לשורה שהיא בכלל הערה:

//number is not prime - no need to continue the loop

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.


×
  • צור חדש...