По разделам

 

Решение задачи For 17


Дано вещественное число A и целое число N (> 0). Используя один
цикл, найти сумму
1 + A + A
2 + A
3 + . . . + A
N .

Код (C/C++)

#include <bits/stdc++.h>
using namespace std;

int main() {
	srand((int)time(0));
	int N;
	N = rand() % 10 + 1;
	float A;
	//A = rand() % 100 / 10.0;
	A = rand() % 10;
	cout << "Number A: " << A << endl;
	cout << "Number N: " << N << endl;
	
	double p = 1, s = 1;
	for(int i = 1; i <= N; i++) {
		p *= A;
		s += p;
		cout << i << " : "<< p <<" : " << s << endl;
	}
	cout << "Result: " << s << endl;
	return 0;
}

Код (Python)

import random

N = random.randrange(1,10)
print('N = ', N)
A = random.randrange(-10,10)
print('A = ', A)

P = 1.0
S = 1.0
for i in range(1,N+1):
    P *= A
    S += P
    print(i," : ", P," : ", S)
print("Result:",S)
									

Код (Pascal)

program For17;
var
  A,Rez,Pow:Real;
  N, i :Integer;
begin
  Write('Введите A: ');
  Readln(A);
  Write('Введите N: ');
  Readln(N);
  Rez:=1;
  Pow:=1;
  For i:=1  to N do
   begin
    Pow:=Pow*A;
    Rez:=Rez+Pow;
   end;
  Writeln(Rez);
end.
									




For. Абрамян
For 1 Просмотров: 13161
For 2 Просмотров: 10567
For 3 Просмотров: 8003
For 4 Просмотров: 7572
For 5 Просмотров: 8010
For 6 Просмотров: 7571
For 7 Просмотров: 7857
For 8 Просмотров: 6502
For 9 Просмотров: 5017
For 10 Просмотров: 6500
For 11 Просмотров: 6644
For 12 Просмотров: 6511
For 13 Просмотров: 59586
For 14 Просмотров: 4960
For 15 Просмотров: 5833
For 16 Просмотров: 6088
For 17 Просмотров: 5391
For 18 Просмотров: 4427
For 19 Просмотров: 4426
For 20 Просмотров: 4892
For 21 Просмотров: 3534
For 22 Просмотров: 5154
For 23 Просмотров: 6348
For 24 Просмотров: 3666
For 25 Просмотров: 3310
For 26 Просмотров: 3044
For 27 Просмотров: 3686
For 28 Просмотров: 2551
For 29 Просмотров: 2440
For 30 Просмотров: 3664
For 31 Просмотров: 2872
For 32 Просмотров: 2525
For 33 Просмотров: 2993
For 34 Просмотров: 3395
For 35 Просмотров: 3312
For 36 Просмотров: 3734
For 37 Просмотров: 2876
For 38 Просмотров: 3122
For 39 Просмотров: 3166
For 40 Просмотров: 3544

Комментарии

Чтобы написать комментарии вам нужно войти в систему или зарегистрироваться



Заявка на расчет