По разделам

 

Решение задачи 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 Просмотров: 12680
For 2 Просмотров: 10242
For 3 Просмотров: 7696
For 4 Просмотров: 7273
For 5 Просмотров: 7702
For 6 Просмотров: 7290
For 7 Просмотров: 7477
For 8 Просмотров: 6247
For 9 Просмотров: 4750
For 10 Просмотров: 6232
For 11 Просмотров: 6378
For 12 Просмотров: 6182
For 13 Просмотров: 59294
For 14 Просмотров: 4751
For 15 Просмотров: 5603
For 16 Просмотров: 5833
For 17 Просмотров: 5114
For 18 Просмотров: 4231
For 19 Просмотров: 4222
For 20 Просмотров: 4677
For 21 Просмотров: 3368
For 22 Просмотров: 4932
For 23 Просмотров: 6131
For 24 Просмотров: 3520
For 25 Просмотров: 3167
For 26 Просмотров: 2866
For 27 Просмотров: 3490
For 28 Просмотров: 2409
For 29 Просмотров: 2288
For 30 Просмотров: 3503
For 31 Просмотров: 2730
For 32 Просмотров: 2383
For 33 Просмотров: 2853
For 34 Просмотров: 3223
For 35 Просмотров: 3135
For 36 Просмотров: 3544
For 37 Просмотров: 2717
For 38 Просмотров: 2948
For 39 Просмотров: 2979
For 40 Просмотров: 3362

Комментарии

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



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