Bài 1: Cho n và dãy số nguyên a1, a2, …, an. Tính và in ra màn hình tổng các số trong dãy a.
Bài 2. Cho n và dãy số nguyên a1, a2, …, an. In ra màn hình giá trị nhỏ nhất trong dãy a.
Cho dãy A gồm n số nguyên a1 a2 ...an n.Hãy in ra màn hình dãy A và tính tổng giá trị các phần tử chẵn của dãy đó
uses crt;
var a:array[1..100]of integer;
i,n,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
writeln('Day A=');
for i:=1 to n do
write(a[i]:4);
writeln;
t:=0;
for i:=1 to n do
if a[i] mod 2=0 then t:=t+a[i];
writeln('Tong cac so chan la: ',t);
readln;
end.
uses crt;
var a:array[1..100]of integer;
i,n,dem,dem1,dem2,t:integer;
s:real;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
repeat
write('A[',i,']='); readln(a[i]);
until a[i]>0;
end;
for i:=1 to n do
write(a[i]:4);
writeln;
dem:=0;
for i:=1 to n do
if a[i]>10 then inc(dem);
writeln('So phan tu lon hon 10 la: ',dem);
dem1:=0;
dem2:=0;
for i:=1 to n do
begin
if a[i] mod 2=0 then inc(dem1)
else inc(dem2);
end;
writeln('So luong so chan la: ',dem1);
writeln('So luong so le la: ',dem2);
t:=0;
s:=1;
for i:=1 to n do
begin
if (i mod 2=0) and (a[i] mod 2<>1) then t:=t+a[i];
if (i mod 2=1) and (a[i] mod 2=0) then s:=s*a[i];
end;
writeln('Tong cac so o vi tri chan co gia tri le la: ',t);
writeln('Tich cac so o vi tri le co gia tri chan la: ',s:4:2);
writeln('Cac so le la: ');
for i:=1 to n do
if a[i] mod 2<>0 then write(a[i]:4);
writeln;
writeln('Cac so chan va lon hon 10 la: ');
for i:=1 to n do
if (a[i] mod 2=0) and (a[i]>10) then write(a[i]:4);
readln;
end.
Nhập vào số nguyên dương N (N ≤ 100) và dãy A gồm N số nguyên dương A1, A2, …, AN (Ai ≤2.1019). In ra màn hình số chính phương lớn nhất trong dãy A. Nếu trong dãy không có số chính phương nào thì in ra giá trị là -1
Cho số N nguyên dương và dãy số nguyên A gồm a1, a2,.....,an.
Hãy viết thuật toán
a) đưa ra màn hình tổng số các giá trị trong dãy
(Ví dụ: nhập n=4
Dãy A: 3 6 2 1 đưa ra màn hình tổng số bằng 12 <dạng> )
b) Đưa ra màn hình vị trí chứa giá trị lớn nhất. Nếu có 2 GTLN thì dưa ra vị trí nhỏ nhất chứa GTLN.
a:
#include <bits/stdc++.h>
using namespace std;
long long n,i,x,t;
int main()
{
cin>>n;
t=0;
for (i=1; i<=n;i++)
{
cin>>x;
t=t+x;
}
cout<<t;
return 0;
}
Lập trình giải quyết bài toán sau cho số nguyên dương N (N<-500) và dãy số nguyên a1,a2,...aN. Tính và đưa ra màn hình tổng các số chẵn ở vị trí lẻ trong dãy.
#include <bits/stdc++.h>
using namespace std;
long long a[1000],i,n,t;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
t=0;
for (i=1; i<=n; i++)
if (a[i]%2==0 && i%2==1) t+=a[i];
cout<<t;
return 0;
}
Viết chương trình nhập vào dãy gồm N số nguyên a1,a2,...,aN và số nguyên K bất kỳ . Tính và đưa ra màn hình tổng các số chia hết cho K trong dãy Giúp mình với mai mình nộp bài r 🤦
uses crt;
var a:array[1..100]of integer;
i,n,k,t:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
write('Nhap k='); readln(k);
t:=0;
for i:=1 to n do
if a[i] mod k=0 then t:=t+a[i];
writeln(t);
readln;
end.
bài 1 viết chương trình nhập vào từ bàn phím n và dãy số nguyên a1,a2,..,an (0<n<100)
a. In dãy số vừa nhập ra màn hình theo thứ tự ngược lại
b. Kiểm tra xem dãy a1,a2,...,an có phải là dãy số đối xứng hay không?biết dãy số đối xứng là dãy đọc từ trái qua phải cũng giống đọc từ phải qua trái
n = int(input("Nhập n: "))
a = [ ]
for i in range(n):
a.append(int(input(f"Nhập phần tử a[{i}]: ")))
print("Dãy số vừa nhập:")
# In dãy số theo thứ tự ngược lại
print(a[::-1])
if a == a[::-1]:
print("Dãy số là dãy đối xứng")
else:
print("Dãy số không phải là dãy đối xứng")
Viết chương trình nhập từ bàn phím số nguyên dương N (N<=100) và dãy A gồm N số nguyên A1, A2, …AN. Tìm và in ra số nhỏ nhất của dãy số vừa nhập? Sắp xếp dãy số A theo thứ tự giảm dần và in ra màn hình. Input: Số nguyên dương N và dãy số A gồm N số nguyên dương a1, a2, …, an. Output: In ra Phần tử nhỏ nhất của dãy số đó. Sắp xếp dãy số A theo thứ tự giảm dần và in ra màn hình.
uses crt;
var a:array[1..100]of integer;
i,n,min,tam,j:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
min:=a[1];
for i:=1 to n do
if min>a[i] then min:=a[i];
writeln('Gia tri nho nhat la: ',min);
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]<a[j] then
begin
tam:=a[i];
a[i]:=a[j];
a[j]:=tam;
end;
for i:=1 to n do write(a[i]:4);
readln;
end.
Viết thuật toán và chương trình nhập từ bàn phím dãy A gồm n phần tử a1, a2, a3,..an có kiểu số nguyên. Tìm và in ra màn hình các phần tử có giá trị lẻ, các phần tử có giá trị chẵn trong dãy A