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 <vector>
using namespace std;
pair<int, int> findMaxSubarray(vector<int> nums) {
int n = nums.size();
int maxSum = nums[0];
int currentSum = nums[0];
int start = 0;
int end = 0;
for (int i = 1; i < n; i++) {
if (currentSum < 0) {
currentSum = nums[i];
start = i;
end = i;
} else {
currentSum += nums[i];
end = i;
}
if (currentSum > maxSum) {
maxSum = currentSum;
}
}
return make_pair(start, end);
}
int main() {
int numTests;
cin >> numTests;
for (int t = 0; t < numTests; t++) {
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
}
pair<int, int> maxSubarray = findMaxSubarray(nums);
cout << maxSubarray.first << " " << maxSubarray.second << endl;
}
return 0;
}
var A:
array[1..n] of int64;
i, n, count: integer;
begin
write('Nhap so phan tu cua mang: ');
readln(n);
for i := 1 to n do
begin
write('Nhap phan tu thu ', i, ': ');
readln(A[i]);
end;
count := 0;
for i := 1 to n do
if A[i] > 0 then
count := count + 1;
writeln('So luong cac so nguyen duong trong mang la: ', count); end.
uses crt;
var d,n,k,kt1,i,j,x,kt2,u,kt:longint;
st1,st2,st:string;
y:integer;
begin
clrscr;
write('nhap so k='); readln(k);
if k>1 then begin
d:=1;
n:=3;
repeat
repeat
kt:=0;
n:=n+2;
for i:=2 to trunc(sqrt(n)) do
if n mod i=0 then kt:=1;
if kt=0 then str(n,st1);
until kt=0;
{----------------------------------------}
repeat
kt:=0;
n:=n+2;
for j:=2 to trunc(sqrt(n)) do
if n mod j=0 then kt:=1;
if kt=0 then str(n,st2);
until kt=0;
{----------------------------------------}
kt:=0;
st:=st1+st2;
val(st,x,y);
for i:=2 to trunc(sqrt(x)) do
if x mod i=0 then kt:=1;
if kt=0 then d:=d+1;
until d=k;
end;
if k=1 then write('so nguyen to ghep thu ',k,' la 23')
else writeln('so nguyen to ghep thu ',k,' la: ',x);
readln;
end.
Ngôn ngữ: C++
#include <bits/stdc++.h>
using namespace std;
long long a[1000000];
long long n,m,d;
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++){
long long tmp;
cin>>tmp;
a[i]=a[i-1]+tmp;
}
for(int i=1;i<n;i++){
for(int j=i+1;j<=n;j++){
if(a[j]-a[i-1]<=m){d++;}
}
}
cout<<d;
return 0;
}
Bài 1:
uses crt;
var n,i,dem,maxd,vt,vt1,vt2,vt3,maxa,dem1:integer;
a,b,c,d,e,f,g:array[1..100]of integer;
mind,mina:int64;
begin
clrscr;
repeat
write('n='); readln(n);
if (n<=0) or (n>100) then writeln('nhap lai');
until (0<n) and (n<=100);
for i:=1 to n do
begin
write('a[',i,']='); readln(a[i]);
end;
{--------------------------so-hang-duong-----------------------}
dem:=0;
for i:=1 to n do
if a[i]>0 then
begin
inc(dem);
b[dem]:=a[i];
end;
maxd:=0;
for i:=1 to dem do
if maxd<b[i] then maxd:=b[i];
vt:=0;
for i:=1 to n do
if maxd=a[i] then
begin
vt:=vt+1;
c[vt]:=i;
end;
writeln('so hang duong lon nhat cua day la: ',maxd);
writeln('vi tri cua no lan luot la: ');
for i:=1 to vt do
write(c[i]:4);
writeln;
mind:=maxint;
for i:=1 to dem do
if mind>b[i] then mind:=b[i];
vt1:=0;
for i:=1 to n do
if mind=a[i] then
begin
inc(vt1);
d[vt1]:=i;
end;
writeln('so hang duong nho nhat cua day la: ',mind);
writeln('vi tri cua no lan luot la: ');
for i:=1 to vt1 do
write(d[i]:4);
{------------------------so-hang-am--------------------------}
writeln;
dem1:=0;
for i:=1 to n do
if a[i]<0 then
begin
inc(dem1);
e[dem1]:=a[i];
end;
maxa:=e[1];
for i:=1 to dem1 do
if maxa<e[i] then maxa:=e[i];
vt2:=0;
for i:=1 to n do
if maxa=a[i] then
begin
inc(vt2);
f[vt2]:=i;
end;
writeln('so hang am lon nhat cua day la: ',maxa);
writeln('vi tri cua no lan luot la: ');
for i:=1 to vt2 do
write(f[i]:4);
writeln;
mina:=maxint;
for i:=1 to dem1 do
if mina>e[i] then mina:=e[i];
vt3:=0;
for i:=1 to n do
if mina=a[i] then
begin
inc(vt3);
g[vt3]:=i;
end;
writeln('so hang am nho nhat cua day la: ',mina);
writeln('vi tri cua no lan luot la: ');
for i:=1 to vt3 do
write(g[i]:4);
readln;
end.
Bài 2:
uses crt;
var a,vt:array[1..10]of integer;
i,x,dem:integer;
begin
clrscr;
for i:=1 to 10 do
begin
write('a[',i,']='); readln(a[i]);
end;
write('nhap x='); readln(x);
dem:=0;
for i:=1 to 10 do
if a[i]=x then
begin
inc(dem);
vt[dem]:=i;
end;
if dem=0 then writeln('khong co gia tri ',x,' trong day')
else
begin
writeln('vi tri cua ',x,' trong day lan luot la:');
for i:=1 to dem do
write(vt[i]:4);
end;
readln;
end.
Bài 3:
uses crt;
var a:array[1..100,1..100]of integer;
n,i,j,t:integer;
begin
clrscr;
write('n='); readln(n);
for i:=1 to n do
for j:=1 to n do
begin
write('a[',i,',',j,']='); readln(a[i,j]);
end;
{----------------------------xuat-----------------------------}
writeln('ma tran ban vua nhap la:');
for i:=1 to n do
begin
for j:=1 to n do
write(a[i,j]:4);
writeln;
end;
{------------------tong-cac-phan-tu-tren-duong-cheo-chinh------------------}
t:=0;
for i:=1 to n do
for j:=1 to n do
if i=j then t:=t+a[i,j];
writeln('tong cac phan tu tren duong cheo chinh la: ',t);
readln;
end.
Ngôn ngữ: C++
#include <bits/stdc++.h>
using namespace std;
long long n;
long long minx=LLONG_MAX;
long long a[100000];
int main()
{
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=2;i<=n;i++){
minx=min(minx,a[i]-a[i-1]);
}
cout<<minx;
return 0;
}