Nhập từ bàn phím mảng số nguyên gồm n phân tử
a. tìm vị trí số nhỏ nhất trong mảng
b. tìm vị trí số lớn nhất trong mảng
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 mảng 1 chiều chứa tối đa 100 số
a : tìm số lớn nhất số nhỏ nhất trong mảng
b : tìm vị trí số lớn nhất, nhỏ nhất
c : tính tổng tất cả các số trong mảng
d : tính tổng các số 0 vị trí chẵn trong mảng
e : tính tổng các số âm 0 vị trí lẻ trong mảng
Viết thuật toán và chương trình tìm giá trị lớn nhất(in ra vị trí chứa phần tử có giá trị lớn nhất) của dãy số gồm n số nguyên được nhập từ bàn phím. (Sử dụng mảng).
uses crt;
var a:array[1..100] of integer;
max,n,i:integer;
begin
writeln('nhap do dai cua day');readln(n);
for i:=1 to n do begin writeln('A[',i,']=;);readln(a[i]); end;
max:=a[1];
for i:=2 to n do if a[i] > max then begin
max:=a[i]; writeln('vi tri cua max trong day la',i);end;
readln
end.
thật ra thì cũng không cần biến max nhưng mình làm vậy cho dễ hiểu hơn nhé!
*Thuật toán:
-Bước 1: Nhập n và nhập dãy số
-Bước 2: max←a[1]; i←1;
-Bước 3: i←i+1;
-Bước 4: Nếu max<a[i] thì max←a[i];
-Bước 5: Nếu i<=n thì quay lại bước 3
-Bước 6: Cho for chạy từ 1 đến n
Nếu a[i]=max thì xuất i
-Bước 7: Kết thúc
Viết 2 chương trình con tìm Max, Min và vị trí của Max, Min trong mảng. Áp dụng: Nhập vào 1 mảng có n phần tử là các số nguyên, tìm số nguyên lớn nhất, nhỏ nhất và vị trí của các số đó trong mảng. Các số có giá trị thuộc kiểu int.
Input: đọc từ file 0100.inp
Dòng 1 là số nguyên dương n
Dòng 2 là n số nguyên
Output: ghi ra file 0100.out gồm 2 dòng:
Dòng 1 chứa số lớn nhất trong mảng và các vị trí của số lớn nhất;
Dòng 2 chứa số nhỏ nhất trong mảng và các vị trí của số nhỏ nhất
TRONG C ++
1. Viết chương trình nhập vào mảng gồm n số nguyên, - Đếm và đưa ra số phần tử chẵn của mảng - đưa ra số chẵn lớn nhất trong mảng - Tính tổng các số ở vị trí lẻ trong mảng - Tìm giá trị phần tử lẻ nhỏ nhất
uses crt;
var a:array[1..100]of integer;
i,n,dem,max,t,min,dem1:integer;
begin
clrscr;
write('n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
dem:=0;
max:=-32000;
for i:=1 to n do
begin
if a[i] mod 2=0 then
begin
dem:=dem+1;
if max<a[i] then max:=a[i];
end;
if dem=0 then writeln('Trong day khong co so chan')
else begin
writeln('So so chan la: ',dem);
writeln('So chan lon nhat la: ',max);
end;
t:=0;
for i:=1 to n do
if i mod 2=1 then t:=t+a[i];
writeln('Tong cac so o vi tri le la: ',t);
min:=maxint;
dem1:=0;
for i:=1 to n do
if a[i] mod 2<>0 then
begin
inc(dem1);
if min>a[i] then min:=a[i];
end;
if dem1=0 then writeln('Trong day khong co so le')
else writeln('So le nho nhat la: ',min);
readln;
end.
Nhập vào một mảng gồm n phần tử với n được nhập vào từ bàn phím.
- Tìm giá trị lớn nhất trong mảng ở vị trí chẵn (0,2,4,6,...)
- Nếu viết thành 2 hàm: nhập mảng, tìm giá trị lớn nhất và có lời gọi hàm.
Ví dụ:
Đầu vào:
n = 5
4
2
5
6
9
Đầu ra:
Gia tri lon nhat o vi tri chan la: 9
Nhập vào 1 mảng có N số nguyên ( N< 1000 )
1) Tính tổng các số chia hết cho 3 trong mảng vừa nhập
2) Tìm giá trị nhỏ nhất trong mảng , đưa ra vị trí đó . Nếu có nhiều vị trí chứa giá trị nhỏ nhất thì đưa ra vị trí lớn nhất trong các vị trí đó .
uses crt;
var a:array[1..100]of integer;
i,n,t,min,vt:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
t:=0;
for i:=1 to n do
if a[i] mod 3=0 then t:=t+a[i];
writeln('Tong cac so chia het cho 3 la: ',t);
min:=a[1];
for i:=1 to n do
if min>a[i] then min:=a[i];
vt:=1;
for i:=1 to n do
if min=a[i] then
begin
if vt<i then vt:=i;
end;
writeln('Gia tri nho nhat la: ',min);
writeln('Vi tri la: ',vt);
readln;
end.
uses crt;
var a:array[1..100]of integer;
n,i,k,max,min,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);
max:=a[1];
for i:=1 to n do
if max<a[i] then max:=a[i];
writeln('Gia tri lon nhat la: ',max);
writeln('Vi tri la: ');
for i:=1 to n do
if max=a[i] then write(i:4);
writeln;
min:=a[1];
for i:=1 to n do
if min>a[i] then min:=a[i];
writeln('Gia tri nho nhat la: ',min);
writeln('Vi tri la: ');
for i:=1 to n do
if min=a[i] then write(i:4);
writeln;
t:=0;
for i:=1 to n do
if i mod 2=0 then t:=t+sqr(a[i]);
writeln('Tong binh phuong cac gia tri o vi tri chan la: ',t);
readln;
end.
Viết chương trình nhập vào mảng a gồm n phần tử (n <40) n nhập từ bàn phím
Hãy in ra màn hình vị trí của phần tử có giá trị lớn nhất trong mảng nếu mảng có phần tử có cùng giá trị lớn nhất thì in ra màn hình của phần tử lớn nhất ở vị trí lớn nhất
#include <bits/stdc++.h>
using namespace std;
long long a[40],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;
}