-
Notifications
You must be signed in to change notification settings - Fork 0
Lesson 2 code style #2
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
a36d260
7a61498
fe702d6
9545999
2fb7853
c95b8b1
5d80066
b598f69
23d6683
54dc85b
7147a19
f61a65e
baf0a66
74e47b0
d22be0f
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,12 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| int main() | ||
| { | ||
| int x = 0; | ||
| scanf("%d", &x); | ||
| int t = x * x; | ||
| int result = (t + x) * (t + 1) + 1; | ||
| printf("%d\n", result); | ||
| 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. Зачтено. |
|
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. Зачтено. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| int main() | ||
| { | ||
| int a = 0; | ||
| int b = 0; | ||
| int howManyTimesBInA = 0; | ||
| scanf("%d %d", &a, &b); | ||
|
|
||
| int underZero = false; // флаг, чтобы понять какие значения введены | ||
|
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 - положит | ||
| if ((a * b) < 0) { | ||
| underZero = true; // впоследствии учтем, что ответ должен быть < 0 | ||
| } | ||
|
|
||
| a = abs(a); | ||
| b = abs(b); | ||
| int rememberA = abs(a); | ||
|
|
||
| if (b == 0) { | ||
| printf("devision by zero\n"); | ||
|
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. Оно, конечно, division, но это уже мелочи |
||
| return 1; | ||
| } | ||
|
|
||
| while (a - b > 0) { | ||
| a = a - b; | ||
| howManyTimesBInA += 1; | ||
| } | ||
| if (b * howManyTimesBInA + a == rememberA) { | ||
| if (underZero == true) { | ||
| printf("%d\n", howManyTimesBInA * (-1) - 1); | ||
| } else { | ||
| printf("%d\n", howManyTimesBInA); | ||
| } | ||
| } | ||
|
|
||
| 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. Решение корректное. Хотя по асимптотике (либо уже было на дискретке, либо вот-вот будет на Python) не идеальное. При желании можно найти более эффективное решение. Будет зачтено при инициализации переменных и использовании |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| /* Суть алгоритма такая: берется элемент, стоящий на нулевом индексе в списке, заносится в отдельную переменную. | ||
| * Далее отдельным циклом все элементы кроме нулевого смещаются на одну позицию влево. | ||
| * В конце этого цикла переменная, с элементом, который мы запомнили в начале, ставится на последнюю позицию. | ||
| * И делается это все ровно m раз. | ||
| * Для наглядности, что алгоритм работает, я заполнила его натуральными числами, упорядоченными по возрастанию. | ||
| * если у нас было [1, 2, 3, 4, 5] и m = 3 n = 2, то в конце программы список примет вид [4, 5, 1, 2, 3], | ||
| * то есть конец поменялся с началом. | ||
| */ | ||
|
|
||
| int main() | ||
| { | ||
| int n = 0; // длина конца | ||
| int m = 0; // длина начала | ||
| scanf("%d %d", &n, &m); // я так поняла, что пользователь сам вводит длину массива | ||
|
|
||
| int *list = calloc(n+m, sizeof(int)); | ||
| for (int i = 0; i < m + n; i++) { | ||
| list[i] = i + 1; // заполняем массив значениями, чтобы в нем хранилось что-то упорядоченное, для наглядности, что нужный порядок сохраняется | ||
| } | ||
| int fromBeginingToEnd = 0; | ||
| for (int i = 0; i < m; i++) { | ||
| fromBeginingToEnd = list[0]; | ||
| for (int j = 1; j <= m + n - 1; j++) { | ||
| list[j - 1] = list[j]; | ||
| } | ||
| list[m + n - 1] = fromBeginingToEnd; | ||
| } | ||
| int a = 0; | ||
| for (int i = 0; i < m + n; i++) { | ||
|
|
||
| if (i == m + n - 1) { | ||
| printf("%d\n", list[i]); | ||
| } else { | ||
| printf("%d", list[i]); | ||
| } | ||
| } | ||
| free(list); | ||
| return 0; | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| int main() | ||
| { | ||
| int stek[100]; // сделаем большой стек состоящий из 100 нулей | ||
| char string[100]; // строка из 100 символов | ||
| scanf("%s", string); // ввели строку (огранчение 100 символов) | ||
|
|
||
| int count = 0; // счетчик того, сколько позиций занято в стеке | ||
| char str1; // здесь будут символы из строки | ||
| char str2 = '('; | ||
| char str3 = ')'; | ||
|
|
||
| int everything_ok = 1; // флаг, что все скобки пока идут правильно | ||
|
|
||
| for (int i = 0; i < 100; i++) // перебор символов введенной строки | ||
| { | ||
| str1 = string[i]; | ||
|
|
||
| if (str1 == str2) // Ищем окрывающие скобки | ||
| { | ||
| stek[count] = 2; // пусть 2 - это открывающая скобка | ||
| count += 1; | ||
| } | ||
|
|
||
| else if (str1 == str3) // если скобка закрывающаяся | ||
| { | ||
| if (stek[count - 1] == 2) // то все ок | ||
| { | ||
| stek[count - 1] = 0; | ||
| count -= 1; | ||
| // этими действиями мы убрали закрытые скобки, будто их и не было | ||
| } else { | ||
|
|
||
| everything_ok = 0; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| // Если скобочная последовательность правильная, то стек в конце будет состоять только из нулей | ||
| if (everything_ok == 1 && stek[0] == 2) { | ||
| printf("Скобочная последовательность неправильная\n"); | ||
| } | ||
|
|
||
| else if (everything_ok == 1) { | ||
| printf("Скобочная последовательность правильная\n"); | ||
| } | ||
|
|
||
| else { | ||
| printf("Скобочная последовательность неправильная\n"); | ||
| } | ||
|
|
||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
||
| int main() | ||
| { | ||
| char list_s[] = "hellohellhellolllhello"; | ||
| char list_s1[] = "hello"; | ||
|
|
||
| size_t lenS = sizeof(list_s) / sizeof(char); | ||
| size_t lenS1 = sizeof(list_s1) / sizeof(char) - 1; // так как последний символ нулевой | ||
|
|
||
| int count = 0; // счетчик вхождений | ||
| for (int i = 0; i < lenS; i++) // i - индекс того эл-та с которого будем рассматривать строку s | ||
| { | ||
| int flag = 1; // Это значит, что все символы строк совпадают, но если хотя бы один символ будет отличаться, будем менять на 0 и выходить из цикла | ||
| for (int j = 0; j < lenS1; j++) { | ||
| if (list_s[i + j] != list_s1[j]) { | ||
| flag = 0; // опа, строчки отличаются | ||
| break; // собираем вещи и уходим | ||
| } | ||
| } | ||
| if (flag == 1) // все символы совпали | ||
| { | ||
| count += 1; | ||
| } | ||
| } | ||
| printf("%d\n", count); | ||
|
|
||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include <stdio.h> | ||
|
|
||
| int main() | ||
| { | ||
| // пусть массив задан в программе | ||
| int list[] = { 0, 1, 3, 0, 6, 0, 7, 4, 6, 2, 0, 0 }; | ||
| int count = 0; | ||
| size_t length = sizeof(list) / sizeof(int); | ||
|
|
||
| for (int i = 0; i < length; i++) { | ||
| if (list[i] == 0) { | ||
| count += 1; | ||
| } | ||
| } | ||
| printf("%d", count); | ||
| return 0; | ||
| } |
This file was deleted.
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.
Зачтено.