nhập vào số nguyên a,b,c ktra và in ra số lớn nhất max in ra số nhỏ nhất min
Hãy nhập câu hỏi của bạn vào đây, nếu là tài khoản VIP, bạn sẽ được ưu tiên trả lời.
Bạn cần giúp với Ngôn ngữ lập trình gì nhỉ? Mình sẽ hướng dẫn bạn thuật toán nhé. Nếu chưa rõ bạn có thể hỏi lại.
*Thuật toán:
- Bước 1: Nhập a, b, c
- Bước 2: Min ← a; Max ← a;
- Bước 3:
+ Nếu Min < b thì Min ← b;
+ Nếu Max > b thì Max ← b;
- Bước 4:
+ Nếu Min < c thì Min ← c;
+ Nếu Max > c thì Max ← c;
- Bước 5: In ra Max; Min và kết thúc thuật toán.
câu 1
Program Nguyen_to;
Var n,i:integer;
Function NT(n:integer):Boolean;
Var ok: Boolean;
i: integer;
Begin ok:=true;
for i:=2 to n-1 do if (n mod i)= 0 then ok:=ok and false;
if n < 2 then NT:=false else NT:=ok;
End;
Begin Write('Nhap n: ');
Readln(n); i:=n;
Repeat i:=i+1;
Until NT(i);
Write('So nguyen to nho nhat lon hon ',n, 'la: ',i);
Readln End.
câu 2
uses crt;
const so: set of char=['0','1','2','3','4','5','6','7','8','9'];
var a:array[1..100] of integer;
st,b:string;
c,l,i,n,j:integer;
s, Max: integer;
begin clrscr;
write('Nhap xau:');
readln(st);
l:=length(st);
i:=1;
n:=0;
repeat if (st[i] in so) then begin b:='';
repeat b:=b+st[i];
inc(i);
until (not(st[i] in so)) or (i>l);
inc(n);
val(b,a[n],c);
end;
inc(i);
until i>l;
Max:=a[1];
for i:=2 to n do If Max<A[i] Then Max:=A[i];
Writeln('Phan tu lon nhat cua mang:', Max);
readln;
end.
Bài 1
Var a:array:[1..1000] of integer;
i,n:integer;
s:longint;
Begin
Write('n = ');readln(n);
For i:=1 to n do
Begin
Write('Nhap so thu ',i,' = ');readln(a[i]);
s:=s+a[i];
End;
Write('Cac so vua nhap la: ');
For i:=1 to n do
Write(a[i]:8);
writeln;
write('Tong cac so la ',s);
Readln
End.
program SoMinMaxTrongMang;
var
A: array [1..50] of Integer;
n, i, min, max: Integer;
begin
Write('Nhap so phan tu cua mang: ');
Readln(n);
for i := 1 to n do
begin
Write('A[', i, ']= ');
Readln(A[i]);
end;
min := A[1];
max := A[1];
for i := 2 to n do
begin
if A[i] < min then
min := A[i];
if A[i] > max then
max := A[i];
end;
WriteLn('So lon nhat trong mang la: ', max);
WriteLn('So nho nhat trong mang la: ', min);
end.
#include <bits/stdc++.h>
using namespace std;
long long a,b,c,d;
int main()
{
cin>>a>>b>>c>>d;
cout<<max(a,max(b,max(c,d)));
return 0;
}
Lời giải :
Câu 1 :
var ,j,dem,min,max : integer ;
a : array[1..32000] of integer;
begin
write('n='); readln(n);
writeln('Nhap gia tri cua cac phan tu');
for i:= 1 to n do
begin
write('a[',i,']='); readln(a[i]);
end;
{cau a};
for i:= 1 to n-1 do
for j:= 1 to n do
if a[i] > a[j] then
begin
tg:=a[i];
a[i]:=a[j];
a[j]:=tg ;
end;
write('Mang sap xep theo thu tu tang dan la');
for i:= 1 to n do write(a[i]:3);
{cau b va cau c};
max :=a[1]; min:= a[1] ; dem:=0;
for i:= 2 to n do
begin
if max < a[i] then max:=a[i];
if min > a[i] then min:=a[i];
if a[i]=min then dem:=dem+1;
end;
write('Gia tri lon nhat la:',max);
write('Gia tri nho nhat la:',min);
write('So luong phan tu nho nhat la',dem);
readln
end.
Câu 2 : tương tự câu 1 , bạn tự vận dụng nha
program TimMinMaxTrungBinh;
var
N, i, max, min, sum: integer;
a: array[1..50] of integer;
TB, minTB, maxTB: real;
begin
writeln('Nhap so nguyen duong N:');
readln(N);
sum := 0;
for i := 1 to N do
begin
write('Nhap so thu ', i, ': ');
readln(a[i]);
sum := sum + a[i];
end;
max := a[1];
min := a[1];
for i := 2 to N do
begin
if a[i] > max then
max := a[i];
if a[i] < min then
min := a[i];
end;
TB := sum / N;
minTB := TB;
maxTB := TB;
for i := 1 to N do
begin
if (a[i] < TB) and (a[i] < minTB) then
minTB := a[i];
if (a[i] > TB) and (a[i] > maxTB) then
maxTB := a[i];
end;
writeln('Gia tri lon nhat la: ', max);
writeln('Gia tri nho nhat la: ', min);
writeln('Gia tri trung binh la: ', TB:2:2);
if minTB = TB then
writeln('Khong co gia tri nao nho hon TB')
else
writeln('Gia tri nho nhat < TB la: ', minTB);
if maxTB = TB then
writeln('Khong co gia tri nao lon hon TB')
else
writeln('Gia tri lon nhat > TB la: ', maxTB);
readln;
end.
uses crt;
var a,b,c,min,max:integer;
begin
clrscr;
write('Nhap a='); readln(a);
write('Nhap b='); readln(b);
write('Nhap c='); readln(c);
max:=a;
if max<b then max:=b;
if max<c then max:=c;
min:=a;
if min>b then min:=b;
if min>c then min:=c;
writeln('Gia tri lon nhat la: ',max);
writeln('Gia tri nho nhat la: ',min);
readln;
end.