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;
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 tạo mảng gồm n bé hơn hoặc bằng 150
a) tính và đưa ra màn hình tích các số lẻ chia hết cho 3 trong mảng
b)đưa ra vị trí phần tử có giá trị nhỏ nhất trong mảng , nếu có nhiều giá trị nhỏ nhất thì đưa ra chỉ số lớn nhất
c)đưa ra màn hình sau khi được sắp sắp theo thứ tự tăng dần của các giá trị trong mảng
#include <bits/stdc++.h>
using namespace std;
long long a[150],i,s,n,nn;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
s=1;
for (i=1; i<=n; i++)
if (a[i] %2!=0 && a[i]%3==0) s=s*a[i];
cout<<s<<endl;
nn=a[1];
for (i=1; i<=n; i++) nn=min(nn,a[i]);
for (i=n; i>=1; i--)
if (nn==a[i])
{
cout<<i<<endl;
break;
}
sort(a+1,a+n+1);
for (i=1; i<=n; i++) cout<<a[i]<<" ";
return 0;
}
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.
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 ++
Viết chương trình C++ biết nhập vào một mảng số nguyên. Tìm các phần tử nhỏ nhất của mảng, đưa ra các vị trí tại đó đạt giá trị nhỏ nhất.
#include <bits/stdc++.h>
using namespace std;
long long a[1000],i,n,nn;
int main()
{
cin>>n;
for (i=1; i<=n; i++) cin>>a[i];
nn=a[1];
for (i=1; i<=n; i++) nn=min(nn,a[i]);
cout<<nn<<endl;
for (i=1; i<=n; i++) if (nn==a[i]) cout<<i<<" ";
return 0;
}
nhập vào một mảng gồm n(n nhỏ hơn hoặc bằng 250)số nguyeenvaf số nguyên k. tìm và đưa ra màn hình giá trị lớn nhất của mảng và vị trí lớn nhất đó.
uses crt;
var a:array[1..250]of integer;
i,n,max:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
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 cua no trong mang la: ');
for i:=1 to n do
if max=a[i] then write(i:4);
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;
}
Bài 1. Nhập vào một dãy A gồm N (N <= 250) phần tử và một số k. Cho biết số K có trong mảng hay không?. Nếu có đa ra vị trí của số hạng có giá trị bằng k trong dãy.
Bài 2Viết chương trình nhập vào 1 mảng cho đến khi gặp số 0 thì dừng lại. Đưa ra mảng đã nhập vào và kết quả của nó (While, repeat).
Bài 3. Viết chương trình nhập vào một mảng hai chiều có M dòng, N cột là các số nguyên dương, sau đó:
- In ra mảng vừa nhập
- Tính tổng các số chẵn của mảng
- Đếm xem trong mảng có bao nhiêu số bằng 10.
Bài 1:
#include <bits/stdc++.h>
using namespace std;
long long a[250],i,n,k;
int main()
{
cin>>n>>k;
for (i=1; i<=n; i++)
cin>>a[i];
for (i=1; i<=n; i++)
if (a[i]==k) cout<<i<<" ";
return 0;
}
Bài 1. Nhập vào một dãy A gồm N (N <= 250) phần tử và một số k. Cho biết số K có trong mảng hay không?. Nếu có đa ra vị trí của số hạng có giá trị bằng k trong dãy.
Bài 2Viết chương trình nhập vào 1 mảng cho đến khi gặp số 0 thì dừng lại. Đưa ra mảng đã nhập vào và kết quả của nó (While, repeat).
Bài 3. Viết chương trình nhập vào một mảng hai chiều có M dòng, N cột là các số nguyên dương, sau đó:
- In ra mảng vừa nhập
- Tính tổng các số chẵn của mảng
- Đếm xem trong mảng có bao nhiêu số bằng 10.
Bài 1:
#include <bits/stdc++.h>
using namespace std;
long long a[250],i,n,k;
int main()
{
cin>>n>>k;
for (i=1; i<=n; i++)
cin>>a[i];
for (i=1; i<=n; i++)
if (a[i]==k) cout<<i<<" ";
return 0;
}