Nhập 1 số nguyên dương N, sau đó nhập tiếp 1 dãy gồm N số nguyên. Nhập 2 số nguyên dương…
- User
Nhập 1 số nguyên dương N, sau đó nhập tiếp 1 dãy gồm N số nguyên. Nhập 2 số nguyên dương V và K. Thêm số V vào vị trí K trong dãy. In ra dãy đó. Code java(n<=10^6)
- Assistant
import java.util.Scanner;
public class InsertNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
// Nhập số nguyên dương N System.out.print("Nhập số nguyên dương N: "); int N = scanner.nextInt(); // Khởi tạo mảng với số phần tử là N int[] array = new int[N]; // Nhập dãy gồm N số nguyên for (int i = 0; i < N; i++) { System.out.print("Nhập số nguyên thứ " + (i+1) + ": "); array[i] = scanner.nextInt(); } // Nhập số nguyên dương V System.out.print("Nhập số nguyên dương V: "); int V = scanner.nextInt(); // Nhập số nguyên dương K System.out.print("Nhập số nguyên dương K: "); int K = scanner.nextInt(); // Thêm số V vào vị trí K trong mảng int[] newArray = new int[N+1]; for (int i = 0; i < K-1; i++) { newArray[i] = array[i]; } newArray[K-1] = V; for (int i = K; i <= N; i++) { newArray[i] = array[i-1]; } // In ra dãy mới System.out.print("Dãy mới sau khi thêm số " + V + " vào vị trí " + K + ": "); for (int i = 0; i < N+1; i++) { System.out.print(newArray[i] + " "); } } - User
Vasya has recently learned to type and log on to the Internet. He immediately entered a chat room and decided to say hello to everybody. Vasya typed the word s. It is considered that Vasya managed to say hello if several letters can be deleted from the typed word so that it resulted in the word "hello". For example, if Vasya types the word "ahhellllloou", it will be considered that he said hello, and if he types "hlelo", it will be considered that Vasya got misunderstood and he didn't manage to say hello. Determine whether Vasya managed to say hello by the given word s.
Code java for this problem
- Assistant
import java.util.Scanner;
public class SayHello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
// Nhập từ cần kiểm tra System.out.print("Nhập từ cần kiểm tra: "); String word = scanner.nextLine(); // Kiểm tra xem từ có chứa "hello" hay không boolean isPossible = isHelloPossible(word); // In kết quả if (isPossible) { System.out.println("Vasya managed to say hello."); } else { System.out.println("Vasya didn't manage to say hello."); } } // Hàm kiểm tra xem từ có chứa "hello" hay không public static boolean isHelloPossible(String word) { int index = 0; // Vị trí bắt đầu tìm kiếm char[] hello = {'h', 'e', 'l', 'l', 'o'}; // Duyệt từng kí tự trong từ for (char c : word.toCharArray()) { // Nếu kí tự hiện tại trùng với kí tự trong hello // tăng vị trí tìm kiếm lên 1 if (c == hello[index]) { index++; } // Nếu đã tìm thấy tất cả kí tự trong hello // tức là từ có chứa "hello" if (index == 5) { return true; } } // Nếu duyệt hết các kí tự trong từ mà không tìm thấy "hello" // tức là từ không chứa "hello" return false; } - User
Vasya has recently learned to type and log on to the Internet. He immediately entered a chat room and decided to say hello to everybody. Vasya typed the word s. It is considered that Vasya managed to say hello if several letters can be deleted from the typed word so that it resulted in the word "hello". For example, if Vasya types the word "ahhellllloou", it will be considered that he said hello, and if he types "hlelo", it will be considered that Vasya got misunderstood and he didn't manage to say hello. Determine whether Vasya managed to say hello by the given word s.
Input The first and only line contains the word s, which Vasya typed. This word consisits of small Latin letters, its length is no less that 1 and no more than 100 letters.
Output If Vasya managed to say hello, print "YES", otherwise print "NO". Code java
- Assistant
import java.util.Scanner;
public class SayHello { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
// Nhập từ cần kiểm tra System.out.print("Nhập từ cần kiểm tra: "); String word = scanner.nextLine(); // Kiểm tra xem từ có chứa "hello" hay không boolean isPossible = isHelloPossible(word); // In kết quả if (isPossible) { System.out.println("YES"); } else { System.out.println("NO"); } } // Hàm kiểm tra xem từ có chứa "hello" hay không public static boolean isHelloPossible(String word) { int index = 0; // Vị trí bắt đầu tìm kiếm char[] hello = {'h', 'e', 'l', 'l', 'o'}; // Duyệt từng kí tự trong từ for (char c : word.toCharArray()) { // Nếu kí tự hiện tại trùng với kí tự trong hello // tăng vị trí tìm kiếm lên 1 if (c == hello[index]) { index++; } // Nếu đã tìm thấy tất cả kí tự trong hello // tức là từ có chứa "hello" if (index == 5) { return true; } } // Nếu duyệt hết các kí tự trong từ mà không tìm thấy "hello" // tức là từ không chứa "hello" return false; }