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.
#include <iostream>
#include <fstream>
using namespace std;
long int x[4],n,a[5001],kt[5001],ktvt[5001],MAXtong,dem=0;
int TRY(int i)
{
for(int j=x[i-1]+1;j<=n;j++)
if(kt[a[j]]==0)
{
x[i]=j;
kt[a[j]]=1;
if(i==3)
{
if(a[x[3]]==(float)(a[x[2]]+a[x[1]])/2||a[x[2]]==(float)(a[x[3]]+a[x[1]])/2||a[x[1]]==(float)(a[x[2]]+a[x[3]])/2)
{
dem++;
if(a[x[1]]+a[x[2]]+a[x[3]]>MAXtong)
{
MAXtong=a[x[1]]+a[x[2]]+a[x[3]];
}
}
}
else
TRY(i+1);
kt[a[j]]=0;
}
}
int main()
{
ifstream f("boba.inp");
f>>n;
for(int i=1;i<=n;i++)
{
f>>a[i];
}
x[0]=0;
MAXtong=-1000000000;
fill_n(kt,1001,0);
TRY(1);
cout<<dem<<endl;
if(dem>0)
{
cout<<MAXtong;
}
return 0;
}
Mình mới đạt tới trình độ quy hoạch động nên bạn thông cảm
Xin lỗi bạn, mình không hỗ trợ C. mình chỉ biết pascal thôi
const fi='tamhop.inp';
fo='tamhop.out';
var f1,f2:text;
a:array[1..100]of integer;
n,i,j,k,dem,max,t:integer;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,n);
for i:=1 to n do
read(f1,a[i]);
{--------------------------------xu-ly--------------------------------}
dem:=0; max:=0;
for i:=1 to n-2 do
begin
for j:=i+1 to n-1 do
begin
for k:=j+1 to n do
begin
if (a[i]=(a[j]+a[k])/2) or (a[j]=(a[i]+a[k])/2) or (a[k]=(a[i]+a[j])/2) then
begin
inc(dem);
t:=a[i]+a[j]+a[k];
if max<=t then max:=t;
end;
end;
end;
end;
writeln(f2,dem);
writeln(f2,max);
close(f1);
close(f2);
end.
Program Vidu;
Uses crt;
Type Mang1C = array[1..10] of Integer; {khai báo tên mảng – cách 2}
Var A:Mang1C; {Đặt tên mảng là A}
i:integer;
Begin
clrscr;
Writeln('Nhap gia tri cho mang A');
For i:=1 to 10 do
Begin
Write('A[',i,'] = ');
Readln(A[i]); { đọc vào giá trị cho A thứ i}
End;
Write('Danh sach cac phan tu trong mang A: ');
For i:=1 to 10 do
Write(A[i]:5);
Readln;
End.
Bài 2:
uses crt;
var a:array[1..199]of integer;
i,n:integer;
begin
clrscr;
write('n='); readln(n);
for i:=1 to n do
begin
write('a[',i,']='); readln(a[i]);
end;
{----------------------------xuat-------------------------------}
for i:=1 to n do write(a[i]:4);
readln;
end.
Bài 3:
uses crt;
var a:array[1..199]of integer;
i,n,x,dem:integer;
begin
clrscr;
write('n='); readln(n);
for i:=1 to n do
begin
write('a[',i,']='); readln(a[i]);
end;
{----------------------------xu-ly-------------------------------}
write('x='); readln(x);
dem:=0;
for i:=1 to n do
if a[i]=x then inc(dem);
writeln('trong day co ',dem,' gia tri ',x);
readln;
end.
Cho mình hỏi nếu đếm các phần tử có giá trị lẻ trong xâu thì làm như thế nào vậy
uses crt;
var a:array[1..50]of integer;
t,i,n:integer;
begin
clrscr;
repeat
write('Nhap n='); readln(n);
until (0<n) and (n<=50);
for i:=1 to n do
begin
repeat
write('A[',i,']='); readln(a[i]);
until (0<=a[i]) and (a[i]<=32767);
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=1 then t:=t+a[i];
writeln('Tong cac so le la: ',t);
writeln('Cac so le trong day A la: ');
for i:=1 to n do
if a[i] mod 2=1 then write(a[i]:4);
readln;
end.
uses crt;
var a:array[1..199]of integer;
i,n,min:integer;
begin
clrscr;
write('n='); readln(n);
for i:=1 to n do
begin
write('a[',i,']='); readln(a[i]);
end;
{----------------------------xu-ly-------------------------------}
min:=a[1];
for i:=1 to n do
if min>a[i] then min:=a[i];
writeln(min);
readln;
end.
Câu 1:
uses crt;
var a:array[1..100]of integer;
n,i,tl,tc:integer;
begin
clrscr;
repeat
write('nhap n='); readln(n);
if (n<=0) or (n>100) then writeln('ban da nhap sai, yeu cau ban nhap lai');
until (0<n) and (n<=100);
for i:=1 to n do
begin
repeat
write('a[',i,']='); readln(a[i]);
if abs(a[i])>=1000 then writeln('vui long nhap so co gia tri tuyet doi nho hon 1000');
until abs(a[i])<1000;
end;
{--------------------------xu-ly-----------------------------}
tc:=0;
tl:=0;
for i:=1 to n do
begin
if a[i] mod 2=0 then tc:=tc+a[i];
if a[i] mod 2=1 then tl:=tl+a[i];
end;
writeln('tong so nguyen chan cua day la: ',tc);
writeln('tong so nguyen le cua day la: ',tl);
readln;
end.
Câu 2:
uses crt;
var a:array[1..100]of integer;
n,i:integer;
begin
clrscr;
repeat
write('nhap n='); readln(n);
if (n<=0) or (n>100) then writeln('ban da nhap sai, yeu cau ban nhap lai');
until (0<n) and (n<=100);
for i:=1 to n do
begin
write('a[',i,']='); readln(a[i]);
end;
{--------------------------xu-ly-----------------------------}
writeln('cac so nguyen am co trong day la: ');
for i:=1 to n do
if a[i]<0 then write(a[i]:4);
readln;
end.
Câu 3:
uses crt;
var a:array[1..100]of integer;
n,i,t:integer;
begin
clrscr;
repeat
write('nhap n='); readln(n);
if (n<=0) or (n>100) then writeln('ban da nhap sai, yeu cau ban nhap lai');
until (0<n) and (n<=100);
for i:=1 to n do
begin
write('a[',i,']='); readln(a[i]);
end;
{--------------------------xu-ly-----------------------------}
t:=0;
for i:=1 to n do
if a[i] mod 3=0 then t:=t+a[i];
writeln('tong cac phan tu chia het cho 3 la: ',t);
readln;
end.
Câu 4:
uses crt;
var a,vt:array[1..100]of integer;
n,i,t,dem,j:integer;
min:int64;
begin
clrscr;
repeat
write('nhap n='); readln(n);
if (n<=0) or (n>100) then writeln('ban da nhap sai, yeu cau ban nhap lai');
until (0<n) and (n<=100);
for i:=1 to n do
begin
repeat
write('a[',i,']='); readln(a[i]);
if abs(a[i])>=1000 then writeln('vui long nhap so co gia tri tuyet doi nho hon 1000');
until abs(a[i])<1000;
end;
{--------------------------xu-ly-----------------------------}
min:=maxint;
for i:=1 to n do
if min>a[i] then min:=a[i];
dem:=0;
for i:=1 to n do
if a[i]=min then
begin
inc(dem);
vt[dem]:=i;
end;
writeln('so nho nhat la: ',min);
writeln('vi tri cua no trong day lan luot la: ');
for j:=1 to dem do
write(vt[j]:4);
readln;
end.
Câu 5:
uses crt;
var a,vt:array[1..100]of integer;
n,i,j,tam:integer;
begin
clrscr;
repeat
write('nhap n='); readln(n);
if (n<=0) or (n>100) then writeln('ban da nhap sai, yeu cau ban nhap lai');
until (0<n) and (n<=100);
for i:=1 to n do
begin
repeat
write('a[',i,']='); readln(a[i]);
if abs(a[i])>=1000 then writeln('vui long nhap so co gia tri tuyet doi nho hon 1000');
until abs(a[i])<1000;
end;
{--------------------------xu-ly-----------------------------}
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 so giam dan la: ');
for i:=1 to n do
write(a[i]:4);
readln;
end.
uses crt;
var n,i:integer;
a,b,c:array[1..100]of integer;
begin
clrscr;
write('n='); readln(n);
for i:=1 to n do
begin
write('a[',i,']='); readln(a[i]);
end;
for i:=1 to n do
begin
write('b[',i,']='); readln(b[i]);
end;
for i:=1 to n do
c[i]:=a[i]+b[i];
for i:=1 to n do
write(c[i]:4);
readln;
end.
uses crt;
var n,i:integer;
a,b,c: array [1..100] of integer;
begin
clrscr;
write('So phan tu cua day: ');readln(n);
for i:= 1 to n do
begin
write('Phan tu ',i,' cua day a: ');
readln(a[i]);
end;
writeln;
for i:= 1 to n do
begin
write('Phan tu ',i,' cua day b: ');
readln(b[i]);
end;
for i:= 1 to n do
c[i]:= a[i] + b[i];
writeln('Mang C:');
for i:= 1 to n do
write(c[i],' ');
readln
end.