-
Notifications
You must be signed in to change notification settings - Fork 0
Контрольная работа №1. Кальсина Яна. Б43 #6
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,28 @@ | ||||||||||
| #include <stdio.h> | ||||||||||
|
|
||||||||||
| // рекурсивная функция просто считающая числа фиббоначи, принимает номер числа | ||||||||||
| // возвращает число фибоначчи | ||||||||||
|
Comment on lines
+3
to
+4
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
:) |
||||||||||
| int fibonacci(int n) { | ||||||||||
| if (n == 0 || n == 1) { | ||||||||||
| return n; | ||||||||||
| } | ||||||||||
| return fibonacci(n - 1) + fibonacci(n - 2); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| int main() { | ||||||||||
| int summ = 0; | ||||||||||
| // временная переменная, она будет содержать в себе числа фибоначчи | ||||||||||
| int tmp = 0; | ||||||||||
|
Comment on lines
+14
to
+15
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Временные переменные лучше создать в месте использование. В данном случае прямо внутри цикла было бы лучше. |
||||||||||
| // номер числа фибоначчи | ||||||||||
| // первый номер равен 0, так что его можно не считать отдельно | ||||||||||
| int number = 1; | ||||||||||
| while (tmp <= 1000000) { | ||||||||||
| if ((tmp % 2) == 0) { | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Так тоже можно |
||||||||||
| summ += tmp; | ||||||||||
| } | ||||||||||
| number++; | ||||||||||
| tmp = fibonacci(number); | ||||||||||
| } | ||||||||||
| printf("%d\n", summ); | ||||||||||
| return 0; | ||||||||||
| } | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я сломался считать косяки в этой задачке. Так что пусть будет 4 балла и всё. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| bool palindrom(char *line, int counter) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. По стайлгайду и вообще с точки зрения грамотного английского ---
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Содержательно функция делает не то, что просили, она просто проверяет является ли строка палиндромом, не думая о пробелах. |
||
| if ((counter % 2) == 0) { | ||
| for (int i = 0; i < (counter / 2); i++) | ||
| if (line[i] != line[counter - i - 1]) { | ||
| return false; | ||
| } | ||
| } else { | ||
| for (int i = 0; i < (counter / 2); i++) | ||
| if (line[i] != line[counter - i - 1]) { | ||
| printf("%c %c\n", line[i], line[counter - i - 1]); | ||
| return false; | ||
| } | ||
| } | ||
|
Comment on lines
+6
to
+17
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В теле |
||
| return true; | ||
| } | ||
|
|
||
| int main() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вот здесь Вы попали в ловушку, потому что не следовали правилу "ввод-вывод отдельно, логика отдельно". Вы пытаетесь очистить строку от пробелов при вводе. Однако в итоге у Вас получается не C-string (нет |
||
| printf("Введите число символов, которое хотите ввести, завершите ввод нажав " | ||
| "enter\n"); | ||
| printf("На след строке введите строку и завершите ввод enter\n"); | ||
| int len = 0; | ||
| scanf("%d", &len); | ||
| getchar(); | ||
| char *arrayWithString = malloc(len * sizeof(char)); | ||
| // счетчик чтобы считать кол-во не пробелов | ||
| int counter = 0; | ||
|
Comment on lines
+29
to
+30
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если бы Вы сделали правильный C-string, то на ней бы работал |
||
| for (int i = 0; i < len; i++) { | ||
| char tmp = ' '; | ||
| scanf("%c", &tmp); | ||
| if (tmp != ' ') { | ||
| arrayWithString[counter] = tmp; | ||
| ++counter; | ||
| } | ||
|
Comment on lines
+34
to
+37
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И ещё вопрос: а пользователь должен вводить длину строки с пробелами или нет? Потому что сейчас у Вас кучу места в массиве остаётся неиспользованным. |
||
| } | ||
| if (palindrom(arrayWithString, counter)) { | ||
| printf("Строка является палиндромом\n"); | ||
| } else { | ||
| printf("Строка не является палиндромом\n"); | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
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.
Зачтено. 9 баллов. -1 за отступы и фигурные скобки у функций не по стайлгайду.