Loops
Страница 1 от 1
Loops
1. Напишете програма, която отпечатва на конзолата числата от 1 до N. Числото N трябва да се чете от стандартния вход.
2. Напишете програма, която отпечатва на конзолата числата от 1 до N, които не се делят едновременно на 3 и 7. Числото N да се чете от стандартния вход.
3. Напишете програма, която чете от конзолата поредица от цели числа и отпечатва най-малкото и най-голямото от тях.
4. Напишете програма, която отпечатва всички възможни карти от стандартно тесте карти без джокери (имаме 52 карти: 4 бои по 13 карти).
5. Напишете програма, която чете от конзолата числото N и отпечатва сумата на първите N члена от редицата на Фибоначи: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...
6. Напишете програма, която пресмята N!/K! за дадени N и K (1<K<N).
7. Напишете програма, която пресмята N!*K!/(N-K)! за дадени N и K (1<K<N).
8. В комбинаториката числата на Каталан (Catalan’s numbers) се изчисляват по следната формула: , за n ≥ 0. Напишете програма, която изчислява n-тото число на Каталан за дадено n.
9. Напишете програма, която за дадено цяло число n, пресмята сумата:
10. Напишете програма, която чете от конзолата положително цяло число N (N < 20) и отпечатва матрица с числа като на фигурата по-долу:
N = 3 N = 4
11. Напишете програма, която пресмята с колко нули завършва факториелът на дадено число. Примери:
N = 10 -> N! = 3628800 -> 2
N = 20 -> N! = 2432902008176640000 -> 4
12. Напишете програма, която преобразува дадено число от десетична в двоична бройна система.
13. Напишете програма, която преобразува дадено число от двоична в десетична бройна система.
14. Напишете програма, която преобразува дадено число от десетична в шестнайсетична бройна система.
15. Напишете програма, която преобразува дадено число от шестнайсетична в десетична бройна система.
16. Напишете програма, която по дадено число N отпечатва числата от 1 до N, разбъркани в случаен ред.
17. Напишете програма, която за дадени две числа, намира най-големия им общ делител.
18. Напишете програма, която по дадено число n, извежда матрица във формата на спирала:
пример при n=4
2. Напишете програма, която отпечатва на конзолата числата от 1 до N, които не се делят едновременно на 3 и 7. Числото N да се чете от стандартния вход.
3. Напишете програма, която чете от конзолата поредица от цели числа и отпечатва най-малкото и най-голямото от тях.
4. Напишете програма, която отпечатва всички възможни карти от стандартно тесте карти без джокери (имаме 52 карти: 4 бои по 13 карти).
5. Напишете програма, която чете от конзолата числото N и отпечатва сумата на първите N члена от редицата на Фибоначи: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...
6. Напишете програма, която пресмята N!/K! за дадени N и K (1<K<N).
7. Напишете програма, която пресмята N!*K!/(N-K)! за дадени N и K (1<K<N).
8. В комбинаториката числата на Каталан (Catalan’s numbers) се изчисляват по следната формула: , за n ≥ 0. Напишете програма, която изчислява n-тото число на Каталан за дадено n.
9. Напишете програма, която за дадено цяло число n, пресмята сумата:
10. Напишете програма, която чете от конзолата положително цяло число N (N < 20) и отпечатва матрица с числа като на фигурата по-долу:
N = 3 N = 4
11. Напишете програма, която пресмята с колко нули завършва факториелът на дадено число. Примери:
N = 10 -> N! = 3628800 -> 2
N = 20 -> N! = 2432902008176640000 -> 4
12. Напишете програма, която преобразува дадено число от десетична в двоична бройна система.
13. Напишете програма, която преобразува дадено число от двоична в десетична бройна система.
14. Напишете програма, която преобразува дадено число от десетична в шестнайсетична бройна система.
15. Напишете програма, която преобразува дадено число от шестнайсетична в десетична бройна система.
16. Напишете програма, която по дадено число N отпечатва числата от 1 до N, разбъркани в случаен ред.
17. Напишете програма, която за дадени две числа, намира най-големия им общ делител.
18. Напишете програма, която по дадено число n, извежда матрица във формата на спирала:
пример при n=4
зад.1
//1. Напишете програма, която отпечатва на конзолата числата от 1 до N. Числото N трябва да се чете от стандартния вход.
1. Write a program that prints on the console the numbers from 1 to N. The number N should be read from the standard input.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homework
{
class homework
{
static void Main(string[] args)
{
Console.Write("N= ");
int N = int.Parse(Console.ReadLine());
for (int i = 1; i <= N; i++)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
}
}
1. Write a program that prints on the console the numbers from 1 to N. The number N should be read from the standard input.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homework
{
class homework
{
static void Main(string[] args)
{
Console.Write("N= ");
int N = int.Parse(Console.ReadLine());
for (int i = 1; i <= N; i++)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
}
}
Последната промяна е направена от iodan на Вто Апр 28, 2015 9:17 pm; мнението е било променяно общо 4 пъти
зад.2
//2. Напишете програма, която отпечатва на конзолата числата от 1 до N, които не се делят едновременно на 3 и 7. Числото N да се чете от стандартния вход.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homework
{
class homework
{
static void Main(string[] args)
{
Console.Write("N= ");
int n = int.Parse(Console.ReadLine());
for (int i = 1; i <=n; i++)
{
if (i%21!=0)
{
Console.Write(i+" ");
}
}
}
}
}
Re: Loops
//3. Напишете програма, която чете от конзолата поредица от цели числа и отпечатва най-малкото и най-голямото от тях.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homework
{
class homework
{
static void Main(string[] args)
{
Console.Write("set the number of numbers = ");
int n = int.Parse(Console.ReadLine());
int counter;
int maxNumber=0;
int minNumber = Int32.MaxValue;
for (counter = 0; counter < n; counter++)
{
Console.Write("next number: ");
int nextNumber = int.Parse(Console.ReadLine());
if (minNumber > nextNumber)
{
minNumber = nextNumber;
}
if (maxNumber < nextNumber)
{
maxNumber = nextNumber;
}
}
Console.WriteLine("max= " + maxNumber);
Console.WriteLine("min= " + minNumber);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homework
{
class homework
{
static void Main(string[] args)
{
Console.Write("set the number of numbers = ");
int n = int.Parse(Console.ReadLine());
int counter;
int maxNumber=0;
int minNumber = Int32.MaxValue;
for (counter = 0; counter < n; counter++)
{
Console.Write("next number: ");
int nextNumber = int.Parse(Console.ReadLine());
if (minNumber > nextNumber)
{
minNumber = nextNumber;
}
if (maxNumber < nextNumber)
{
maxNumber = nextNumber;
}
}
Console.WriteLine("max= " + maxNumber);
Console.WriteLine("min= " + minNumber);
}
}
}
Re: Loops
4. Напишете програма, която отпечатва всички възможни карти от стандартно тесте карти без джокери (имаме 52 карти: 4 бои по 13 карти).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
for (int karta = 2; karta <=14; karta++)
{
for (int color = 1; color < 5; color++)
{
if (karta<10)
{
Console.Write(" "+karta);
}
if (karta==10)
{
Console.Write(" " + karta);
}
if (karta==11)
{
Console.Write(" J ");
}
if (karta==12)
{
Console.Write(" Q ");
}
if (karta==13)
{
Console.Write(" K ");
}
if (karta==14)
{
Console.Write(" A ");
}
if (color==1)
{
Console.Write(" diamond ");
}
if (color==2)
{
Console.Write(" heart ");
}
if (color==3)
{
Console.Write(" spade ");
}
if (color==4)
{
Console.Write(" clubs ");
}
}
Console.WriteLine();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
for (int karta = 2; karta <=14; karta++)
{
for (int color = 1; color < 5; color++)
{
if (karta<10)
{
Console.Write(" "+karta);
}
if (karta==10)
{
Console.Write(" " + karta);
}
if (karta==11)
{
Console.Write(" J ");
}
if (karta==12)
{
Console.Write(" Q ");
}
if (karta==13)
{
Console.Write(" K ");
}
if (karta==14)
{
Console.Write(" A ");
}
if (color==1)
{
Console.Write(" diamond ");
}
if (color==2)
{
Console.Write(" heart ");
}
if (color==3)
{
Console.Write(" spade ");
}
if (color==4)
{
Console.Write(" clubs ");
}
}
Console.WriteLine();
}
}
}
}
Последната промяна е направена от iodan на Пет Май 01, 2015 5:19 pm; мнението е било променяно общо 2 пъти
Re: Loops
5. Напишете програма, която чете от конзолата числото N и отпечатва сумата на първите N члена от редицата на Фибоначи: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
long firstAddend=0;
long secondAddend=1;
long sum=0;
Console.Write("0, 1, ");
for (int i = 1; i <= n; i++)
{
sum = firstAddend + secondAddend;
firstAddend = secondAddend;
secondAddend = sum;
Console.Write(sum +", ");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
long firstAddend=0;
long secondAddend=1;
long sum=0;
Console.Write("0, 1, ");
for (int i = 1; i <= n; i++)
{
sum = firstAddend + secondAddend;
firstAddend = secondAddend;
secondAddend = sum;
Console.Write(sum +", ");
}
}
}
}
Последната промяна е направена от iodan на Пет Май 01, 2015 6:07 pm; мнението е било променяно общо 2 пъти
Re: Loops
6. Напишете програма, която пресмята N!/K! за дадени N и K (1<K<N).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
Console.Write("N= ");
int N = int.Parse(Console.ReadLine());
Console.Write("K= ");
int K = int.Parse(Console.ReadLine());
BigInteger totalN=1;
BigInteger totalK=1;
if (N>K&&K>1)
{
for (int i = N; i > 1; i--)
{
totalN *= i;
}
for (int i = K; i > 1; i--)
{
totalK *= i;
}
Console.WriteLine("N!={0}", totalN);
Console.WriteLine("K!={0}", totalK);
BigInteger calculation= totalN / totalK;
Console.WriteLine(calculation);
}
else Console.WriteLine("ERROR!! Incorrect values");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
Console.Write("N= ");
int N = int.Parse(Console.ReadLine());
Console.Write("K= ");
int K = int.Parse(Console.ReadLine());
BigInteger totalN=1;
BigInteger totalK=1;
if (N>K&&K>1)
{
for (int i = N; i > 1; i--)
{
totalN *= i;
}
for (int i = K; i > 1; i--)
{
totalK *= i;
}
Console.WriteLine("N!={0}", totalN);
Console.WriteLine("K!={0}", totalK);
BigInteger calculation= totalN / totalK;
Console.WriteLine(calculation);
}
else Console.WriteLine("ERROR!! Incorrect values");
}
}
}
Последната промяна е направена от iodan на Съб Май 02, 2015 12:30 pm; мнението е било променяно общо 2 пъти
Re: Loops
7. Напишете програма, която пресмята N!*K!/(N-K)! за дадени N и K (1<K<N).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
Console.Write("N= ");
BigInteger N = BigInteger.Parse(Console.ReadLine());
Console.Write("K= ");
BigInteger K = BigInteger.Parse(Console.ReadLine());
BigInteger totalN=1;
BigInteger totalK = 1;
if (N>K&&K>1)
{
for (BigInteger i = N; i >= 1; i--)
{
totalN *= i;
}
//calculating K!
for (BigInteger i = K; i > 1; i--)
{
totalK *= i;
}
//calculating N!*K!
BigInteger divident = totalN * totalK;
//calculating N-K
BigInteger nminusK = N - K;
//calculating (N-K)!
BigInteger nminusKfact = 1;
for (BigInteger i = nminusK; i > 1; i--)
{
nminusKfact *= i; //(N-K)fact
}
//final calculation N!*K!/(N-K)!
BigInteger result = (BigInteger)(divident / nminusKfact);
Console.WriteLine("N!= " + totalN);
Console.WriteLine("K!= " + totalK);
Console.WriteLine("N!*K!= " + divident);
Console.WriteLine("N-K= " + (N - K));
Console.WriteLine("(N-K)!= " + nminusKfact);
Console.WriteLine("N!*K!/(N-K)!= " + result);
}
else
Console.WriteLine("ERROR!! Incorrect values!!");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
Console.Write("N= ");
BigInteger N = BigInteger.Parse(Console.ReadLine());
Console.Write("K= ");
BigInteger K = BigInteger.Parse(Console.ReadLine());
BigInteger totalN=1;
BigInteger totalK = 1;
if (N>K&&K>1)
{
for (BigInteger i = N; i >= 1; i--)
{
totalN *= i;
}
//calculating K!
for (BigInteger i = K; i > 1; i--)
{
totalK *= i;
}
//calculating N!*K!
BigInteger divident = totalN * totalK;
//calculating N-K
BigInteger nminusK = N - K;
//calculating (N-K)!
BigInteger nminusKfact = 1;
for (BigInteger i = nminusK; i > 1; i--)
{
nminusKfact *= i; //(N-K)fact
}
//final calculation N!*K!/(N-K)!
BigInteger result = (BigInteger)(divident / nminusKfact);
Console.WriteLine("N!= " + totalN);
Console.WriteLine("K!= " + totalK);
Console.WriteLine("N!*K!= " + divident);
Console.WriteLine("N-K= " + (N - K));
Console.WriteLine("(N-K)!= " + nminusKfact);
Console.WriteLine("N!*K!/(N-K)!= " + result);
}
else
Console.WriteLine("ERROR!! Incorrect values!!");
}
}
}
Последната промяна е направена от iodan на Съб Май 02, 2015 12:30 pm; мнението е било променяно общо 2 пъти
Re: Loops
8. В комбинаториката числата на Каталан (Catalan’s numbers) се изчисляват по следната формула: , за n ≥ 0. Напишете програма, която изчислява n-тото число на Каталан за дадено n.
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
Console.Write("n= ");
BigInteger n = BigInteger.Parse(Console.ReadLine());
BigInteger multiplicationNandTwo = n * 2;
BigInteger dividentFact = 1;
// calculating (2*n)!
for (BigInteger i = multiplicationNandTwo; i > 1; i--)
{
dividentFact *= i;
}
Console.WriteLine("(2*n)!= "+dividentFact); //print (2*n)!
//calculating n+1 and (n+1)!
BigInteger dividerNplus1 = n + 1;
BigInteger dividerNplus1fact = 1;
for (BigInteger i = dividerNplus1; i > 1; i--)
{
dividerNplus1fact *= i; //(n+1)!
}
Console.WriteLine("(n+1)!= "+dividerNplus1fact); //print (n+1)!
//calculating n!
BigInteger factN = 1;
for (BigInteger i = n; i > 1; i--)
{
factN *= i;
}
Console.WriteLine("n!= "+factN);
//final calculation
BigInteger catalansNumber = dividentFact / (dividerNplus1fact * factN);
Console.WriteLine("Cn= "+catalansNumber); //print Catalan`s numbers
}
}
}
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
Console.Write("n= ");
BigInteger n = BigInteger.Parse(Console.ReadLine());
BigInteger multiplicationNandTwo = n * 2;
BigInteger dividentFact = 1;
// calculating (2*n)!
for (BigInteger i = multiplicationNandTwo; i > 1; i--)
{
dividentFact *= i;
}
Console.WriteLine("(2*n)!= "+dividentFact); //print (2*n)!
//calculating n+1 and (n+1)!
BigInteger dividerNplus1 = n + 1;
BigInteger dividerNplus1fact = 1;
for (BigInteger i = dividerNplus1; i > 1; i--)
{
dividerNplus1fact *= i; //(n+1)!
}
Console.WriteLine("(n+1)!= "+dividerNplus1fact); //print (n+1)!
//calculating n!
BigInteger factN = 1;
for (BigInteger i = n; i > 1; i--)
{
factN *= i;
}
Console.WriteLine("n!= "+factN);
//final calculation
BigInteger catalansNumber = dividentFact / (dividerNplus1fact * factN);
Console.WriteLine("Cn= "+catalansNumber); //print Catalan`s numbers
}
}
}
Последната промяна е направена от iodan на Съб Май 02, 2015 12:30 pm; мнението е било променяно общо 1 път
Re: Loops
10. Напишете програма, която чете от конзолата положително цяло число N (N < 20) и отпечатва матрица с числа като на фигурата по-долу:
N = 3 N = 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homework
{
class Program
{
static void Main(string[] args)
{
int n=int.Parse(Console.ReadLine());
int row=1;
int column=0;
for (column = 0; column < n; column++)
{
for (row = 1+column; row <= n+column; row++)
{
if (row<10)
{
Console.Write(" ");
}
Console.Write(row+" ");
}
Console.WriteLine();
}
}
}
}
N = 3 N = 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homework
{
class Program
{
static void Main(string[] args)
{
int n=int.Parse(Console.ReadLine());
int row=1;
int column=0;
for (column = 0; column < n; column++)
{
for (row = 1+column; row <= n+column; row++)
{
if (row<10)
{
Console.Write(" ");
}
Console.Write(row+" ");
}
Console.WriteLine();
}
}
}
}
Последната промяна е направена от iodan на Пет Май 01, 2015 4:03 pm; мнението е било променяно общо 1 път
Re: Loops
11. Напишете програма, която пресмята с колко нули завършва факториелът на дадено число. Примери:
N = 10 -> N! = 3628800 -> 2
N = 20 -> N! = 2432902008176640000 -> 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
Console.Write("n= ");
int number = int.Parse(Console.ReadLine());
BigInteger sum = 1;
int counter = 0;
BigInteger divider = 10;
for (int i = 1; i <= number; i++)
{
sum*=i;
while (sum%divider==0)
{
divider *= 10;
counter++;
}
}
Console.WriteLine(counter);
Console.WriteLine(sum);
}
}
}
N = 10 -> N! = 3628800 -> 2
N = 20 -> N! = 2432902008176640000 -> 4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
Console.Write("n= ");
int number = int.Parse(Console.ReadLine());
BigInteger sum = 1;
int counter = 0;
BigInteger divider = 10;
for (int i = 1; i <= number; i++)
{
sum*=i;
while (sum%divider==0)
{
divider *= 10;
counter++;
}
}
Console.WriteLine(counter);
Console.WriteLine(sum);
}
}
}
Последната промяна е направена от iodan на Пет Май 01, 2015 4:07 pm; мнението е било променяно общо 2 пъти
Re: Loops
12. Напишете програма, която преобразува дадено число от десетична в двоична бройна система.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
Console.Write("Decimal: ");
int decimalN = int.Parse(Console.ReadLine());
int rest;
string result = string.Empty;
while (decimalN > 0)
{
rest = decimalN % 2;
decimalN /= 2;
result += rest.ToString();
}
Console.WriteLine("Binary: {0}", result);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
Console.Write("Decimal: ");
int decimalN = int.Parse(Console.ReadLine());
int rest;
string result = string.Empty;
while (decimalN > 0)
{
rest = decimalN % 2;
decimalN /= 2;
result += rest.ToString();
}
Console.WriteLine("Binary: {0}", result);
}
}
}
Re: Loops
13. Напишете програма, която преобразува дадено число от двоична в десетична бройна система.
Re: Loops
14. Напишете програма, която преобразува дадено число от десетична в шестнайсетична бройна система.
Re: Loops
15. Напишете програма, която преобразува дадено число от шестнайсетична в десетична бройна система.
Re: Loops
16. Напишете програма, която по дадено число N отпечатва числата от 1 до N, разбъркани в случаен ред.
Re: Loops
17. Напишете програма, която за дадени две числа, намира най-големия им общ делител.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
while (a != 0 && b != 0)
{
if (a > b)
{
a %= b;
}
else
{
b %= a;
}
}
if (a == 0)
{
Console.WriteLine(b);
}
else
{
Console.WriteLine(a);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace homeworks
{
class Program
{
static void Main(string[] args)
{
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
while (a != 0 && b != 0)
{
if (a > b)
{
a %= b;
}
else
{
b %= a;
}
}
if (a == 0)
{
Console.WriteLine(b);
}
else
{
Console.WriteLine(a);
}
}
}
}
Страница 1 от 1
Права за този форум:
Не Можете да отговаряте на темите
|
|