Muhammad al-Xorazmiy nomidagi Toshkent Axborot Texnologiyalar Universititeti “R va ma” fakulteti


Download 341.57 Kb.
Sana31.01.2023
Hajmi341.57 Kb.
#1142974
Bog'liq
Alijonov.M MTA L-3


Muhammad al-Xorazmiy nomidagi
Toshkent Axborot Texnologiyalar
Universititeti “R va MA” fakulteti
051-20 guruh talabasi Alijonov Muhammadqodir.

Ma’lumotlar tuzilmasi va algoritmlar.
Kodlar C# (.NET 6) da yozildi.Masalalar LeetCode saytidan olindi.

Bajardi:Alijonov Muhammadqodir.
Tekshirdi:

Toshkent 2022


1-masala;
English version:
Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
Return the sum of the three integers.
You may assume that each input would have exactly one solution.
UZB version:
Uzunligi n boʻlgan butun son massivi va maqsadli butun son berilgan boʻlsa, yigʻindisi maqsadga eng yaqin boʻladigan tarzda uchta butun sonni toping.
Uchta butun sonning yig'indisini qaytaring.
Har bir kirishda aynan bitta yechim bo'ladi deb taxmin qilishingiz mumkin.

public class Solution {
public int ThreeSumClosest(int[] nums, int target) {
Array.Sort(nums);

int head=0,start=1,end=nums.Length-1;


int res=0,distance=13000;//maximum distance = 3*maximum(nums[i]) + Abs(minimum(target))
// = 3000 + 10000

for(;head
while(start int sum=nums[head]+nums[start]+nums[end];

if(sum == target){


return target;
}
else{
int Diff=Math.Abs(sum-target);

if(sum>target){


end--;
}
else{
start++;
}

if(distance > Diff){


distance=Diff;
res=sum;
}
}
}

start=head+2;


end=nums.Length-1;
}

return res;


}
}
2-masala
English version:
Implement pow(x, n), which calculates x raised to the power n (i.e., xn).
UZB version:
n (ya'ni, xn) darajasiga ko'tarilgan x ni hisoblaydigan pow(x, n) ni bajaring.

public class Solution {
public double MyPow(double x, int n) {
if(n < 0)
{
var pow = (n != int.MinValue) ? MyPowInner(x, -n) : MyPowInner(x*x, -(n/2));
return 1 / pow;
}
return MyPowInner(x, n);
}
private double MyPowInner(double x, int n)
{
if(n < 2)
{
return n == 1 ? x : 1;
}
else
{
return (n % 2 == 0) ? MyPow(x*x, n/2) : x*MyPow(x*x, n/2);
}
}
}

Download 341.57 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling