Nhập từ bàn phím mảng a có n số nguyên. In ra màn hình mảng a vừa nhập, in phần tử lớn nhất, in phần tử nhỏ nhất của mảng, in vị trí phần tử lớn nhất, in vị trí phần tử nhỏ nhất của mảng.
#include <bits/stdc++.h>
using namespace std;
const long long maxint=1e6;
long long a[maxint],i,n,ln;
int main()
{
cin>>n;
for (i=1; i<=n; i++)
cin>>a[i];
ln=a[1];
for (i=1; i<=n; i++)
ln=max(ln,a[i]);
for (i=n; i>=1; i--)
if (ln==a[i])
{
cout<<i;
break;
}
return 0;
}
Viết chương trình nhập vào 1 mảng 1 chiều gồm n phần tử (với n được nhập vào từ bàn phím). Hãy thực hiện các công việc sau:
- Nhập các phần tử mảng từ bàn phím.
- Hiển thị mảng vừa nhập ra màn hình.
- In ra màn hình các số chính phương trong mảng.
- Đếm trong mảng có bao nhiêu phần tử có giá trị bằng 5. Nếu không có phần tử nào có giá trị bằng 5 thì in ra màn hình thông báo “Không có phần tử nào thỏa mãn điều kiện”
#include <bits/stdc++.h>
using namespace std;
long long n,i,a[10000];
int main()
{
cin>>n;
for (i=1; i<=n; i++)
cin>>a[i];
for (i=1; i<=n; i++) cout<<a[i]<<" ";
return 0;
}
bài 1:Viết chương trình nhập vào một mảng số nguyên gồm n phần tử(n<100) nhập từ bàn phím thực hiện các công việc sau:
a)In ra màn hình các phần tử của mảng vừa nhập.
b) Sắp xếp dãy số nguyên theo thứ tự tăng dần.
c) Nhập số nguyên x bất kì.Tính số chắn lớn nhất không được vượt quá x.
Bài 1:
uses crt;
var a:array[1..100]of integer;
i,n,kt,max,x,j,tam:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
writeln('Mang ban vua nhap la: ');
for i:=1 to n do
write(a[i]:4);
writeln;
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;
writeln('Day tang dan la: ');
for i:=1 to n do
write(a[i]:4);
writeln;
write('Nhap x='); readln(x);
max:=0;
kt:=0;
for i:=1 to n do
if (a[i] mod 2=0) and (a[i]<=x) then
begin
if max<a[i] then max:=a[i];
kt:=1;
end;
if kt=0 then writeln('Trong day khong co so le')
else writeln('So chan lon nhat khong vuot qua ',x,' la: ',max);
readln;
end.
Mảng 1 chiều B gồm N phần tử (N≤50), giá trị các phần tử là các số nguyên. Đưa ra màn hình:
- Mảng vừa nhập với thứ tự các phần tử ngược lại( NHập vào 3,6,8 đưa ra 8 6 3)
- đếm số phần tử chẵn
- Kiểm tra và đưa ra màn hình thông báo máy, A có lập thành cấp số cộng hay không.
var n,i,dem,t:integer;
a:array[1..50] of integer;
begin
dem:=0;
t:=0;
read(n);
for i:=1 to n do
read(a[i]);
for i:=n downto 1 do
write(a[i]);
writeln;
for i:=1 to n do
if (a[i] mod 2 = 0) then dem:=dem+1;
if a[i]<a[i-1] then t:=t+1;
writeln(dem);
if t=0 then write('Co lap thanh cap so +')
else write('Ko the lap thanh cap so +');
readln;
end.
Cho mảng A gồm N phần tử nguyên a1,a2,..,an. Với N < 103 nhập từ bàn phím. a. Hãy viết chương trình nhập mảng A. b. In ra màn hình tất cả các phần tử chẵn có trong mảng A. c. In ra màn hình tất cả các phần tử lẻ có trong mảng A.
Uses crt;
var i,n: longint;
a: array[1..103] of longint;
begin clrscr;
readln(n);
for i:=1 to n do read(a[i]); readln;
for i:=1 to n do if(a[i] mod 2=0) then write(a[i],' ');
for i:=1 to n do if(a[i] mod 2<>0) then write(a[i],' ');
readln;
end.
VCT nhập 1 dãy số nguyên từ bàn phím sau đó xóa phần tử ở vị trí i của mảng in ra màn hình các phần tử của mảng trước và sau khi xóa
VCT nhập 1 dãy số nguyên từ bàn phím sau đó xóa phần tử ở vị trí i của mảng in ra màn hình các phần tử của mảng trước và sau khi xóa
Cho mảng A có N phần tử (N được nhập vào từ bàn phím, các phần tử của mảng là các số nguyên). Hãy viết chương trình Pascal thực hiện các yêu cầu sau: a. Nhập các phần tử cho mảng A? b. In lên màn hình các phần tử của mảng A? c. Tính tổng các phần tử chẵn của mảng A và thông báo kết quả ra màn hình?
program Tinh_Tong_Phan_Tu_Chan;
var
A: array of Integer;
N, i, sum: Integer;
begin
Write('Nhap N: ');
Readln(N);
SetLength(A, N);
// Nhập các phần tử cho mảng A
for i := 0 to N - 1 do
begin
Write('Nhap phan tu thu ', i + 1, ': ');
Readln(A[i]);
end;
// In lên màn hình các phần tử của mảng A
Write('Cac phan tu cua mang la: ');
for i := 0 to N - 1 do
begin
Write(A[i], ' ');
end;
Writeln;
// Tính tổng các phần tử chẵn của mảng A và thông báo kết quả ra màn hình
sum := 0;
for i := 0 to N - 1 do
begin
if A[i] mod 2 = 0 then
sum := sum + A[i];
end;
Writeln('Tong cac phan tu chan cua mang la: ', sum);
Readln;
end.
Viết chương trình nhập vào mảng A gồm N phần tử (N<= 500) từ bàn phím giá trị các phần tử là các số nguyên đưa ra màn hình mảng vừa nhập với thứ tự các phần tử ngược lại, nhập 3, 6, 8 đưa ra 8, 6, 3. Đếm số phần tử có giá trị lớn hơn 10. Đưa ra màn hình số lượng số nguyên tố trong mảng
#include <bits/stdc++.h>
using namespace std;
int A[500],n,i,dem,dem1;
int main()
{
cin>>n;
for (int i=1; i<=n; i++) cin>>A[i];
for (int i=n; i>=1; i--) cout<<A[i]<<" ";
cout<<endl;
dem=0;
for (int i=1; i<=n; i++)
if (A[i]<10) dem++;
cout<<dem<<endl;
dem1=0;
for (int i=1; i<=n; i++)
if (A[i]>1)
{
bool kt=true;
for (int j=2; j*j<=A[i]; j++)
if (A[i]%j==0) kt=false;
if (kt==true) dem1++;
}
cout<<dem1;
return 0;
}