Bạn chưa đăng nhập. Vui lòng đăng nhập để hỏi bài

Những câu hỏi liên quan
Treallagx
Xem chi tiết
Nguyễn Lê Phước Thịnh
10 tháng 8 2023 lúc 13:59

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]<<" ";

}

Treallagx
Xem chi tiết
Phía sau một cô gái
10 tháng 8 2023 lúc 8:52

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;

}

Phía sau một cô gái
10 tháng 8 2023 lúc 9:00

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;

}

Phía sau một cô gái
10 tháng 8 2023 lúc 9:04

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;

}

Lê Thành Long
Xem chi tiết
Minh Lệ
15 tháng 11 2021 lúc 14:52

VD: N=5, K=3, A1=2, A2=3, A3=3, A4=5, A5=6

i12345
ai23356
dem01222

 

 

 

APOK FF
Xem chi tiết
nguyentienlam
Xem chi tiết
nguyentienlam
4 tháng 4 2023 lúc 15:21

cứu m vs

 

Cuong Ly
Xem chi tiết
Nguyễn Tuấn Anh
4 tháng 5 2021 lúc 9:36

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;

Nguyễn Lê Phước Thịnh
4 tháng 5 2021 lúc 11:48

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.

Không Tên
Xem chi tiết
Nguyễn Lê Phước Thịnh
5 tháng 1 2021 lúc 20:39

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.

Không Tên
5 tháng 1 2021 lúc 9:18

Mọi người làm nhanh hộ ạ, em đang cần gấp ạ, phiền mọi người ạ

 

Không Tên
Xem chi tiết
Nguyễn Lê Phước Thịnh
5 tháng 1 2021 lúc 20:39

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.

Đuc Lee
Xem chi tiết
Minh Lệ
12 tháng 5 2023 lúc 16:08

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.