-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson1 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wheelman1995
wants to merge
11
commits into
master
Choose a base branch
from
lesson1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lesson1 #1
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7867f67
two tasks are done
wheelman1995 360bc4d
add the 3rd task and comments
wheelman1995 6a7982a
Delete workspace.xml
wheelman1995 5c36f88
Delete vcs.xml
wheelman1995 27b6e11
Delete modules.xml
wheelman1995 7d129aa
Delete misc.xml
wheelman1995 5ef437e
delete workspace.xml
wheelman1995 ad4787e
Merge branch 'lesson1' of https://github.com/wheelman1995/Algorithms_…
wheelman1995 e639881
Merge branch 'master' into lesson1
wheelman1995 352fe9a
Merge branch 'master' into lesson1
wheelman1995 196b613
delete imls
wheelman1995 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| public class MainClass { | ||
| public static void main(String[] args) { | ||
| System.out.println(pow(1.1, -3)); | ||
| System.out.println(arrayMin(new Integer[]{0, -6, 1}, 0)); | ||
| System.out.println(findMean(new Float[]{1.1f, 2.2f, 3.3f})); | ||
| } | ||
| //алгоритм работает за O(log n) умножений при возведении в чётную степень и за O(log n + 1) при возведении в нечётную | ||
| //если показатель степени чётный - то приводим x^n к виду x^(n/2) * x^(n/2), чтобы уменьшить кол-во умножений | ||
| //и передаём x^(n/2) в следующую итерацию метода, после получения результата перемножаем результаты - (x^(n/2) * x^(n/2)) | ||
| //если показатель степени нечётный - то приводим x^n к виду x * x^(n-1), таким образом, | ||
| //делая показатель степени чётным и передаём возведение в чётную степень (x^(n-1)) в следующую итерацию метода | ||
| //после получения результата умножаем его на основание x - (x * x^(n-1)) | ||
| //когда показатель степени доходит до 0, возвращаем 1 и идём вверх по итерациям к окончательному результату. | ||
| private static <T extends Number> double pow(T base, int exponent) { | ||
| if (exponent < 0) { | ||
| return 1 / pow(base, -exponent); | ||
| } else { | ||
| if (exponent == 0) return 1; | ||
| if (exponent % 2 == 0) { | ||
| double result = pow(base, exponent / 2); | ||
| return result * result; | ||
| } else { | ||
| return base.doubleValue() * pow(base, exponent - 1); | ||
| } | ||
| } | ||
| } | ||
| //алгоритм работает за O(n - 1) сравниваний | ||
| //начинаем с нулевого индекса запоминать каждый следующий элемент в переменную next, когда доходим до последнего элемента массива - возвращаем его в предыдущую итерацию метода | ||
| //в переменную next, сравниваем элемент массива под индексом, равным значению переменной index в текущей итерации метода с переменной next, и т. д. продолжаем идти в обратную | ||
| //сторону до первого элемента массива под индексом 0. | ||
| private static <T extends Number> double arrayMin(T[] t, int index) { | ||
| if (t.length - 1 > index) { | ||
| double next = arrayMin(t, index + 1); | ||
| return (t[index].doubleValue() < next) ? t[index].doubleValue() : next; | ||
| } else { | ||
| return t[t.length - 1].doubleValue(); | ||
| } | ||
| } | ||
|
|
||
| private static float findMean(Number[] number) { | ||
| float sum = 0; | ||
| for (int i = 0; i < number.length; i++) { | ||
| sum += number[i].doubleValue(); | ||
| } | ||
| return sum / number.length; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а входящий Т мы не хотим ограничить числами?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ограничен - T extends Number