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.
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.
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;
}
Để tích của các số hạng là lớn nhất, ta nên chọn các số hạng nhỏ nhất có thể, bắt đầu từ 2, 3, 4,...
Ta sẽ chọn các số hạng liên tiếp bắt đầu từ 2, cho đến khi tổng của chúng gần bằng nn𝑛.
Giả sử ta chọn các số hạng là 2,3,4,...,k2 comma 3 comma 4 comma point point point comma k2,3,4,...,𝑘. Tổng của chúng là (2+k)(k−1)2the fraction with numerator open paren 2 plus k close paren open paren k minus 1 close paren and denominator 2 end-fraction(2+𝑘)(𝑘−1)2.
Nếu n−∑i=2ki=0n minus sum under-script i equals 2 over-script k end-scripts i equals 0𝑛−𝑘𝑖=2𝑖=0, thì ta đã tìm được cách biểu diễn.
Nếu n−∑i=2ki>0n minus sum under-script i equals 2 over-script k end-scripts i is greater than 0𝑛−𝑘𝑖=2𝑖>0, ta sẽ phân bổ phần dư này cho các số hạng đã chọn, bắt đầu từ số hạng lớn nhất. Step 2: Thuật toán
- Khởi tạo danh sách các số hạng
termsrỗng. - Khởi tạo
sum_terms = 0vàcurrent_num = 2. - Lặp lại cho đến khi
sum_terms + current_num > n:- Thêm
current_numvàoterms. - Cộng
current_numvàosum_terms. - Tăng
current_numlên 1.
- Thêm
- Tính phần dư
remainder = n - sum_terms. - Phân bổ phần dư
remaindercho các số hạng trongterms:- Lặp lại từ cuối danh sách
termsvề đầu: - Nếu
remainder > 0: - Tăng số hạng hiện tại lên 1.
- Giảm
remainderđi 1.
- Nếu
remainder == 0, dừng vòng lặp.
- Nếu
- Lặp lại từ cuối danh sách
- Sắp xếp các số hạng trong
termstheo thứ tự tăng dần.
- Khởi tạo
terms = [],sum_terms = 0,current_num = 2. -
sum_terms + 2 = 2 <= 10.terms = [2],sum_terms = 2.current_num = 3. -
sum_terms + 3 = 5 <= 10.terms = [2, 3],sum_terms = 5.current_num = 4. -
sum_terms + 4 = 9 <= 10.terms = [2, 3, 4],sum_terms = 9.current_num = 5. -
sum_terms + 5 = 14 > 10. Dừng lại. - Phần dư
remainder = 10 - 9 = 1. - Phân bổ phần dư:
- Số hạng cuối cùng là 4. Tăng lên 1 thành 5.
remainder = 0. - Danh sách số hạng mới là
[2, 3, 5].
- Số hạng cuối cùng là 4. Tăng lên 1 thành 5.
- Kiểm tra:
2 + 3 + 5 = 10. Tích là 2×3×5=302 cross 3 cross 5 equals 302×3×5=30.
Để giải quyết bài toán này, ta sẽ sử dụng thuật toán tham lam (greedy algorithm).
Ta sẽ chọn các số hạng là các số nguyên dương liên tiếp bắt đầu từ 2, cho đến khi tổng của chúng gần bằng nn𝑛. Phần dư còn lại sẽ được phân bổ cho các số hạng đã chọn, bắt đầu từ số hạng lớn nhất.
Sau khi thực hiện thuật toán, ta sẽ có danh sách các số hạng cần tìm.
code : #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
freopen("DANALYSE.INP","r",stdin);
freopen("DANALYSE.OUT","w",stdout);
long long n;
cin>>n;
long long num, a[1000009], m, i;
num=2;
while (num<=n){
m++;
a[m]=num;
n=n-num;
++num;}
i=m;
while (n > 0){
a[i]++;
i--;
if(!i)i=m;
n--;}
cout<<m<<endl;
for (i=1;i<=m;i++)
cout<<a[i]<<endl;
return 0;
}
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.
const fi='calc.inp';
fo='calc.out';
var f1,f2:text;
max,a,b,c,d1,t1,i,x,y,d2,t2,d,e,d3,t3,f,g:integer;
st1,st2,st3:string;
begin
assign(f1,fi); reset(f1);
assign(f2,fo); rewrite(f2);
readln(f1,a,b,c);
str(a,st1);
str(b,st2);
str(c,st3);
d1:=length(st1);
t1:=1;
for i:=1 to d1 do
begin
val(st1[i],x,y);
t1:=t1*x;
end;
d2:=length(st2);
t2:=1;
for i:=1 to d2 do
begin
val(st2[i],d,e);
t2:=t2*d;
end;
d3:=length(st3);
t3:=1;
for i:=1 to d3 do
begin
val(st3[i],f,g);
t3:=t3*f;
end;
max:=t1;
if max<t2 then max:=t2;
if max<t3 then max:=t3;
writeln(f2,max);
close(f1);
close(f2);
end.
uses crt;
var n,i,o,d:integer;
function ktnt(n:integer): integer;
var i,d:integer;
begin
d:=0;
for i:=1 to sqrt(n) do
if (n mod i=0) then d:=d+1;
if d=2 then ktnt=0
else ktnt=1;
end;
begin
readln(n);
writeln(' so nguyen to be hon hoac bang n la'); {a}
for i:=1 to n do
if ktnt(i)=0 then writeln(i);
writeln('so nguyen to nho nhat khong be hon n');
o:=n;
while o>0 do
begin
if ktnt(o)=0 then
begin
write(o);
break;
end;
o:=o+1;
end;
writeln('cặp số nguyên tố là hai số nguyên lẻ liên tiếp nhỏ hơn hoặc bằng n');
o:=0;
o:=1;
d:=0;
for i:=o+2 to n do
begin
if ktnt(i)=0 then
begin
d:=d+1;
write(i,' ');
if d<2 then continue;
end;
d:=0;
writeln;
end;
readln;
end.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
bool isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
int main() {
// Mở file đầu vào
ifstream fin("TIMNT.INP");
// Mở file đầu ra
ofstream fout("TIMNT.OUT");
int n;
fin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
fin >> a[i];
}
for (int i = 0; i < n; i++) {
if (isPrime(a[i])) {
fout << a[i] << " ";
}
}
// Đóng file
fin.close();
fout.close();
return 0;
}