DAYTANG
Cho dãy gồm N số ai với (1<n,ai<10^5)
Lập trình giải quyết các yêu cầu sau:
1/ Dem trong day trên có bao nhiêu dãy con tăng liên tục
2/ In ra độ dài dãy con tăng dài nhất
File inp và out theo tên đề bài
Daytang.inp
Daytang.out
[Free Pascal]
Help me ( ngôn ngữ lập trình c++)
Câu 1. Tính số fibonaci thứ N. biết f(1)= 1; f(2) = 1; f(N)=f(N-2)+F(N-1) Câu 7: Cho dãy a gồm m số nguyên (|ai| <=10), dãy b gồm n số nguyên (bị <=10). 2 dãy này đã được sắp xếp không giảm. Hãy in ra một dãy c có các phần tử gồm 2 dãy số trên cũng được sắp xếp không giảm.
Câu 2. Cho dãy số gồm có N phần tử. Hãy đổi vị trí của phần tử lớn nhất và nhỏ nhất cho nhau. Nếu có nhiều phần tử lớn nhất và nhỏ nhất thì đổi chỗ phần tử lớn nhất có vị trí lớn nhất cho phần tử nhỏ nhất có vị trí nhỏ nhất chonhau.
2:
#include <bits/stdc++.h>
using namespace std;
int A[100],ln,nn,vt1,vt2,n;
int main()
{
cin>>n;
for(int i=1; i<=n; i++) cin>>A[i];
ln=A[1];
for (int i=1; i<=n; i++)
ln=max(ln,A[i]);
nn=A[1];
for (int i=1; i<=n; i++)
nn=min(nn,A[i]);
vt1=1; vt2=n;
for (int i=1; i<=n; i++)
if (ln==A[i] && vt1<=i) vt1=i;
for (int i=n; i>=1; i--)
if (nn==A[i] && vt2>=i) vt2=i;
swap(A[vt1],A[vt2]);
for (int i=1; i<=n; i++)
cout<<A[i]<<" ";
}
Help me ( ngôn ngữ lập trình c++)
Help me ( ngôn ngữ lập trình c++)
Câu 1. Tính số fibonaci thứ N. biết f(1)= 1; f(2) = 1; f(N)=f(N-2)+F(N-1)
Câu 2: Cho dãy a gồm m số nguyên (|ai| <=10), dãy b gồm n số nguyên (bị <=10). 2 dãy này đã được sắp xếp không giảm. Hãy in ra một dãy c có các phần tử gồm 2 dãy số trên cũng được sắp xếp không giảm.
Câu 3. Cho dãy số gồm có N phần tử. Hãy đổi vị trí của phần tử lớn nhất và nhỏ nhất cho nhau. Nếu có nhiều phần tử lớn nhất và nhỏ nhất thì đổi chỗ phần tử lớn nhất có vị trí lớn nhất cho phần tử nhỏ nhất có vị trí nhỏ nhất chonhau.
Câu 1: Tính số fibonaci thứ N. biết f(1)= 1; f(2) = 1; f(N)=f(N-2)+F(N-1)
#include <iostream>
int fibonacci(int n) {
if (n <= 2) {
return 1;
}
int prev = 1;
int current = 1;
int fib;
for (int i = 3; i <= n; i++) {
fib = prev + current;
prev = current;
current = fib;
}
return fib;
}
int main() {
int N;
std::cin >> N;
int result = fibonacci(N);
std::cout << "Số Fibonacci thứ " << N << " là: " << result << std::endl;
return 0;
}
Câu 2: Cho dãy a gồm m số nguyên (|ai| <=10), dãy b gồm n số nguyên (bị <=10). 2 dãy này đã được sắp xếp không giảm. Hãy in ra một dãy c có các phần tử gồm 2 dãy số trên cũng được sắp xếp không giảm.
#include <iostream>
#include <vector>
std::vector<int> mergeArrays(const std::vector<int>& a, const std::vector<int>& b) {
std::vector<int> c;
int i = 0;
int j = 0;
while (i < a.size() && j < b.size()) {
if (a[i] <= b[j]) {
c.push_back(a[i]);
i++;
} else {
c.push_back(b[j]);
j++;
}
}
while (i < a.size()) {
c.push_back(a[i]);
i++;
}
while (j < b.size()) {
c.push_back(b[j]);
j++;
}
return c;
}
int main() {
int m, n;
std::cin >> m >> n;
std::vector<int> a(m);
std::vector<int> b(n);
for (int i = 0; i < m; i++) {
std::cin >> a[i];
}
for (int i = 0; i < n; i++) {
std::cin >> b[i];
}
std::vector<int> c = mergeArrays(a, b);
std::cout << "Dãy c sau khi sắp xếp không giảm là:" << std::endl;
for (int i = 0; i < c.size(); i++) {
std::cout << c[i] << " ";
}
std::cout << std::endl;
return 0;
}
Câu 3: Cho dãy số gồm có N phần tử. Hãy đổi vị trí của phần tử lớn nhất và nhỏ nhất cho nhau. Nếu có nhiều phần tử lớn nhất và nhỏ nhất thì đổi chỗ phần tử lớn nhất có vị trí lớn nhất cho phần tử nhỏ nhất có vị trí nhỏ nhất cho nhau.
#include <iostream>
#include <vector>
#include <algorithm>
void swapMinMax(std::vector<int>& arr) {
int minIndex = std::min_element(arr.begin(), arr.end()) - arr.begin();
int maxIndex = std::max_element(arr.begin(), arr.end()) - arr.begin();
std::swap(arr[minIndex], arr[maxIndex]);
}
int main() {
int N;
std::cin >> N;
std::vector<int> arr(N);
for (int i = 0; i < N; i++) {
std::cin >> arr[i];
}
swapMinMax(arr);
std::cout << "Dãy số sau khi đổi vị trí của phần tử lớn nhất và nhỏ nhất cho nhau là:" << std::endl;
for (int i = 0; i < N; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}
Xây dựng thuật toán cho bài toán sau: Cho dãy A gồm N số nguyên a1,a2,...,aN và 1 số nguyên k cho trước. Đếm và đưa ra số lượng các ai = k. Mô phỏng thuật toán với 1 dãy số cụ thể.
VD: N=5, K=3, A1=2, A2=3, A3=3, A4=5, A5=6
i | 1 | 2 | 3 | 4 | 5 |
ai | 2 | 3 | 3 | 5 | 6 |
dem | 0 | 1 | 2 | 2 | 2 |
TimSoM
Cho dãy gồm N số nguyên dương Ai(0<N,Ai <106
).
Lập trình tìm xem sốM có trong dãy không. Nếu có ghi ra YES, không ghi ra NO
- File bài làm: TimSoM.Pas
- File input: TimSoM.inp
o Gồm 2 dòng:
Dòng 1 ghi số N, M
Dòng tiếp theo ghi N số Ai
- File Output: TimSoM.out
o Gồm 1 dòng:
Dòng 1: Ghi Yes hoặc No
Free Pascal nha mn.....!
Nhập vào số nguyên dương N (N ≤ 100) và dãy A gồm N số nguyên dương A1, A2, …, AN (Ai ≤2.1019). In ra màn hình số chính phương lớn nhất trong dãy A. Nếu trong dãy không có số chính phương nào thì in ra giá trị là -1
viết ct nhập vào 1 dãy gồm n số (với n được nhập từ bàn phím).tìm giá trị lớn nhất trong dãy số vừa nhập.
Var z:array[1..1000] of longint;
n, i, max:longint;
Begin
Readln(n);
For i:=1 to n do begin writeln('s',i,'=');
Readln(z[i]);
max:= z[1];
End;
uses crt;
var a:array[1..100]of integer;
i,n,max,dem1,dem2:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
max:=a[1];
for i:=1 to n do
if max<a[i] then max:=a[i];
writeln('So lon nhat la: ',max);
readln;
end.
uses crt;
var a:array[1..100]of integer;
i,n,min,dem:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
min:=a[1];
for i:=1 to n do
if min>a[i] then min:=a[i];
dem:=0;
for i:=1 to n do
if min=a[i] then inc(dem);
writeln('So phan tu co gia tri nho nhat trong day la: ',dem);
readln;
end.
Mọi người làm nhanh hộ ạ, em đang cần gấp ạ, phiền mọi người ạ
uses crt;
var a:array[1..100]of integer;
i,n,min,dem:integer;
begin
clrscr;
write('Nhap n='); readln(n);
for i:=1 to n do
begin
write('A[',i,']='); readln(a[i]);
end;
min:=a[1];
for i:=1 to n do
if min>a[i] then min:=a[i];
dem:=0;
for i:=1 to n do
if min=a[i] then inc(dem);
writeln('So phan tu co gia tri nho nhat trong day la: ',dem);
readln;
end.
Viết chương trình nhập 1 dãy số gồm n số ( n nhập từ bàn phím) tìm giá trị lớn nhất trong dãy số vừa nhập...
--giúp với ạ
Program HOC24;
var i,n,max: integer;
a: array[1..1000] of integer;
begin
write('Nhap N: '); readln(n);
for i:=1 to n do
begin
write('Nhap so thu ',i,': '); readln(a[i]);
end;
max:=a[1];
for i:=1 to n do if a[i]>max then max:=a[i];
write('So lon nhat la: ',max);
readln
end.