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.
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 <bits/stdc++.h>
using namespace std;
long long m,n;
//chuongtrinhcon
long long ucln(long long m,long long n)
{
if (n==0) return(m);
else return(ucln(n,m%n));
}
//chuongtrinhchinh
int main()
{
cin>>n>>m;
cout<<ucln(n,m);
return 0;
}
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;
}
Bài 1:
uses crt;
var n,d,t,x,y,j,kt,i,t1,dem:integer;
st:string;
a:array[1..255]of integer;
begin
clrscr;
write('n='); readln(n);
if (0<n) and (n<1000) then
begin
{------------------------dong-1---------------------------}
str(n,st);
d:=length(st);
t:=0;
for i:=1 to d do
begin
val(st[i],x,y);
t:=t+x;
end;
writeln('tong cac chu so cua ',n,' la: ',t);
{----------------------dong-2----------------------------}
writeln('cac so nguyen to nho hon tong cac chu so cua ',n,' la: ');
dem:=0;
for i:=2 to t-1 do
begin
kt:=0;
for j:=2 to i-1 do
if i mod j=0 then kt:=1;
if kt=0 then
begin
write(i:4);
dem:=dem+1;
a[dem]:=i;
end;
end;
{----------------dong-3---------------------------}
writeln;
t1:=0;
for i:=1 to dem do
t1:=t1+a[i];
writeln('tong cac so nguyen to trong dong 2 la: ',t1);
end
else writeln('vui long nhap lai');
readln;
end.
Bài 2:
uses crt;
var a,b,i,dem,j,kt:integer;
c:array[1..100]of integer;
begin
clrscr;
write('a='); readln(a);
write('b='); readln(b);
if (20<=a) and (a<=50) and (30<=b) and (b<=75) then
begin
{------------------------dong-1--------------------------}
writeln('cac so co 2 chu so chia het cho 9 la:');
for i:=10 to 99 do
if i mod 9=0 then write(i:4);
{-----------------------dong-2--------------------------}
writeln;
writeln('cac so chinh phuong lon hon ',a,' va nho hon ',b,' la: ');
for i:=a+1 to b-1 do
if trunc(sqrt(i))=sqrt(i) then write(i:4);
{-----------------------dong-3---------------------------}
writeln;
dem:=0;
for i:=2 to b do
begin
kt:=0;
for j:=2 to i-1 do
if i mod j=0 then kt:=1;
if kt=0 then
begin
inc(dem);
c[dem]:=i;
end;
end;
writeln('so nguyen to lon nhat nho hon ',b,' la: ',c[dem]);
end
else writeln('vui long nhap lai');
readln;
end.
Bài 3:
uses crt;
var n,d,t,i,x,y:integer;
st:string;
begin
clrscr;
write('n='); readln(n);
if (0<n) and (n<105) then
begin
{---------------------------dong-1---------------------------}
str(n,st);
d:=length(st);
writeln('so luong cac chu so cua ',n,' la: ',d);
{---------------------------dong-2--------------------------}
t:=0;
for i:=1 to d do
begin
val(st[i],x,y);
t:=t+x;
end;
writeln('tong cac chu so cua ',n,' la: ',t);
{---------------------------dong-3------------------------}
writeln('phan so toi gian cua ',n,' la: 1/',n);
write('so dao nguoc cua ',n,' la: ');
for i:=d downto 1 do
write(st[i]);
end
else writeln('vui long nhap lai');
readln;
end.
Câu 1:
Program hotrotinhoc;
var i,tog,n: integer;
function tong(x: integer): integer;
var k,t: longint;
begin
t:=0;
while x<>0 do
begin
k:=x mod 10;
t:=t+k;
x:=x div 10;
end;
tong:=t;
end;
function nt(a: integer): boolean;
var j: integer;
begin
nt:=true;
if (a=2) or (a=3) then exit;
nt:=false;
if (a=1) or (a mod 2=0) or (a mod 3=0) then exit;
j:=5;
while (j<=trunc(sqrt(a))) do
begin
if (a mod j=0) or (a mod (j+2)=0) then exit;
j:=j+6;
end;
nt:=true;
end;
begin
write('Nhap so nguyen N :'); readln(n);
writeln('Tong cac chu so cua N la :',tong(n));
write('Cac so nguyen to nho hon tong cac chu so cua N la :');
for i:=1 to tong(n) do if nt(i) then
begin
write(i,' ');
tog:=tog+i;
end;
writeln;
write('Tong cac so nguyen to trong dong 2 la :',tog);
readln
end.
Để 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;
}