נתונה מחלקה Number הבאה:
נתונה מחלקה Number הבאה:
class Number
{
private int num1;
private int num2;
public Number (int num)
{
this.num1 = 0;
this.num2 = 0;
if(num<0)num = num*(-1);
while(num > 0)
{
this.num1++;
this.num2+= num % 10;
num = num/10;
}
}
public int getNum1(){return this.num1;}
public int getNum2(){return this.num2;}
public boolean equals(Number other)
{
return this.num1 == other.num1 && this.num2 == other.num2;
}
public boolean isSame(Number other)
{
return this.num1 * other.num2 == this.num2 * other.num1;
}
}
סעיף א
נתונה פעולה mystery הבאה:
public static int mystery(int x, int y)
{
Number a = new Number (x);
Number b = new Number (y);
if(x == y)return 1;
if(a.equals(b))return 2;
if(a.isSame(b))return 3;
return 4;
}
- תנו דוגמה לזוג פרמטרים שעבורו הפעולה תחזיר 2.
- תנו דוגמה לזוג פרמטרים שעבורו הפעולה תחזיר 3.
- תנו דוגמה לזוג פרמטרים שעבורו הפעולה תחזיר 4.
- האם קיים מספר שלם תלת-ספרתי חיובי x שעבורו הפעולה mystery אף פעם לא תחזיר 2? הסבירו את תשובתכם.
- האם קיים מספר שלם תלת-ספרתי חיובי x שעבורו הפעולה mystery אף פעם לא תחזיר 3? הסבירו את תשובתכם.
סעיף ב
נתונה פעולה secret הבאה:
public static boolean secret(int [] arr)
{
Number temp1 = new Number (arr[0]);
Number temp2 = new Number (arr[0]);
int pos1 = 0;
int pos2 = 0;
Number cur;
for(int i = 1; i < arr.length; i++)
{
cur = new Number(arr[i]);
if(cur.getNum1() > temp1.getNum1())
{
pos1 = i;
temp1 = cur;
}
if(cur.getNum2() > temp2.getNum2())
{
pos2 = i;
temp2 = cur;
}
}
return (pos1 == pos2);
}
נתון המערך arr הבא: [3458,-45,7681,-875,6,13571,43]
עקבו אחרי ביצוע זימון הפעולה secret(arr) ורשמו תוצאת הזימון.
סעיף ג
מה מבצעת הפעולה secret באופן כללי עבור מערך של מספרים שלמים?
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.