Ето решенията ми на задачите от оследната лекция от Част 1 за програмиране на C# – Цикли.
1. Write a program that prints all the numbers from 1 to N.
2. Write a program that prints all the numbers from 1 to N, that are not divisible by 3 and 7 at the same time.
3. Write a program that reads from the console a sequence of N integer numbers and returns the minimal and maximal of them.
4. Write a program that calculates N!/K! for given N and K (1<N<K).
5. Write a program that calculates N!*K! / (K-N)! for given N and K (1<N<K).
6. Write a program that, for a given two integer numbers N and X, calculates the sumS = 1 + 1!/X + 2!/X2 + … + N!/XN
7. Write a program that reads a number N and calculates the sum of the first N members of the sequence of Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, …
Each member of the Fibonacci sequence (except the first two) is a sum of the previous two members.
8. Write a program that calculates the greatest common divisor (GCD) of given two numbers. Use the Euclidean algorithm (find it in Internet).
9. In the combinatorial mathematics, the Catalan numbers are calculated by the following formula:
Write a program to calculate the Nth Catalan number by given N.
10. Write a program that prints all possible cards from a standard deck of 52 cards (without jokers). The cards should be printed with their English names. Use nested for loops and switch-case.
11. Write a program that reads from the console a positive integer number N (N < 20) and outputs a matrix like the following:
N = 3 | N = 4
1 2 3 | 1 2 3 4
2 3 4 | 2 3 4 5
3 4 5 | 3 4 5 6
| 4 5 6 7
12. * Write a program1 that calculates for given N how many trailing zeros present at the end of the number N!. Examples:
N = 10 N! = 3628800 2
N = 20 N! = 2432902008176640000 4
Does your program work for N = 50 000?
Hint: The trailing zeros in N! are equal to the number of its prime divisors of value 5. Think why!
13. * Write a program that reads a positive integer number N (N < 20) from console and outputs in the console the numbers 1 … N numbers arranged as a spiral.
Example for N = 4
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
Ето линк към решенията:
П.С. Задачите с факториел стават доста лесни ако изнесете логиката в метод 🙂
Решенията ми на задачите от тази лекция.
1. Write an if statement that examines two integer variables and exchanges their values if the first one is greater than the second one.
2. Write a program that shows the sign (+ or -) of the product of three real numbers without calculating it. Use a sequence of if statements.
3. Write a program that finds the biggest of three integers using nested if statements.
4. Sort 3 real values in descending order using nested if statements.
5. Write program that asks for a digit and depending on the input shows the name of that digit (in English) using a switch statement.
6. Write a program that enters the coefficients a, b and c of a quadratic equation
a*x2 + b*x + c = 0
and calculates and prints its real roots. Note that quadratic equations may have 0, 1 or 2 real roots.
7. Write a program that finds the greatest of given 5 variables.
8. Write a program that, depending on the user’s choice inputs int, double or string variable. If the variable is integer or double, increases it with 1. If the variable is string, appends “*” at its end. The program must show the value of that variable as a console output. Use switch statement.
9. We are given 5 integer numbers. Write a program that checks if the sum of some subset of them is 0. Example: 3, -2, 1, 1, 8 1+1-2=0.
10. Write a program that applies bonus scores to given scores in the range [1..9]. The program reads a digit as an input. If the digit is between 1 and 3, the program multiplies it by 10; if it is between 4 and 6, multiplies it by 100; if it is between 7 and 9, multiplies it by 1000. If it is zero or if the value is not a digit, the program must report an error.
Use a switch statement and at the end print the calculated new value in the console.
11. * Write a program that converts a number in the range [0…999] to a text corresponding to its English pronunciation. Examples:
273 “Two hundred seventy three”
400 “Four hundred”
501 “Five hundred and one”
711 “Seven hundred and eleven”
Ето и линк към решенията:
П.С. Последната задача ( 11-та ) е доста интересна, не толкова сложна колкото, доста обемна за писане. Лично успях да я съкратя с досата с масиви, но е напълно възможно да се нарпави и с условни конструкции + switch.