Thử thách PHP - 1 [25 bài tập với giải pháp]
[Một trình soạn thảo có sẵn ở cuối trang để viết và thực thi các tập lệnh.]
1. Viết chương trình PHP để kiểm tra xem một số nguyên dương nhất định có phải là sức mạnh của hai. Chuyển đến EditorInput: 4 Đầu ra: 4 là sức mạnh 2 Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to check whether a given positive integer is a power of two.
Go to the editor
Input : 4
Output :4 is power of 2
Click me to see the sample solution
2. Viết chương trình PHP để kiểm tra xem một số nguyên dương nhất định có phải là sức mạnh của ba. Chuyển đến đầu vào của trình soạn thảo: 9 Đầu ra: 9 là sức mạnh của 3 Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to check whether a given positive integer is a power of three.
Go to the editor
Input : 9
Output : 9 is power of 3
Click me to see the sample solution
3. Viết chương trình PHP để kiểm tra xem một số nguyên dương nhất định có phải là sức mạnh của bốn. Truy cập EditorInput: 4 Đầu ra: 4 là sức mạnh của 4 Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to check whether a given positive integer is a power of
four. Go to the editor
Input : 4
Output : 4 is power of 4
Click me to see the sample solution
4. Viết chương trình PHP để kiểm tra xem số nguyên có phải là sức mạnh của số nguyên khác không. Chuyển đến đầu vào của trình soạn thảo: 16, 2 đầu ra: 16 là sức mạnh của 2 ví dụ: với x = 16 và y = 2 Câu trả lời là "đúng" và với x = 12 và y = 2 "Sai" Nhấp vào tôi để xem Giải pháp mẫu Write a PHP program to check whether an integer is the power of another integer.
Go to the editor
Input : 16, 2
Output : 16 is power of 2
Example: For x = 16 and y = 2 the answer is "true", and for x = 12 and y = 2 "false"
Click me to see the sample solution
5 5] Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to find a missing number[s] from an
array.Go to the editor
Input : 1,2,3,6,7,8
Output : Array
[
[3] => 4
[4] => 5
]
Click me to see the sample solution
6. Viết chương trình PHP để tìm ba số từ một mảng sao cho tổng của ba số liên tiếp bằng 0. [[0] => -1 + 0 + 1 = 0] Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to find three numbers from an array such that the sum of three consecutive numbers
equal to zero.Go to the editor
Input : [-1,0,1,2,-1,-4]
Output : Array
[
[0] => -1 + 0 + 1 = 0
]
Click me to see the sample solution
7. Viết chương trình PHP để tìm ba số từ một mảng sao cho tổng của ba số liên tiếp bằng với một số nhất định. Chuyển đến đầu vào của trình soạn thảo: [mảng [2, 7, 7, 1, 8, 2, 7, 8, 7], 16]] đầu ra: mảng [[0] => 2 + 7 + 7 = 16 [1] => 7 + 1 + 8 = 16] Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to find three numbers from an array such that the sum of three
consecutive numbers equal to a given number. Go to the editor
Input : [array[2, 7, 7, 1, 8, 2, 7, 8, 7], 16]]
Output : Array
[
[0] => 2 + 7 + 7 = 16
[1] => 7 + 1 + 8 = 16
]
Click me to see the sample solution
8. Viết chương trình PHP để tính toán và trả về căn bậc hai của một số đã cho. Chuyển đến đầu vào của trình soạn thảo: 16 Đầu ra: 4 Nhấp vào tôi để xem giải pháp mẫu Write a PHP
program to compute and return the square root of a given number. Go to the editor
Input : 16
Output : 4
Click me to see the sample solution
9. Viết chương trình PHP để tìm một số duy nhất trong một mảng không xảy ra hai lần. Chuyển đến đầu vào của trình soạn thảo: mảng [5, 3, 4, 3, 4] đầu ra: mảng [[0] => 5 [1] => 3 [2] => 4 [3] => 3 [4] = > 4] Số đơn: 5 Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to find a single number in an array that doesn't occur twice.
Go to the editor
Input : array[5, 3, 4, 3, 4]
Output : Array
[
[0] => 5
[1] => 3
[2] => 4
[3] => 3
[4] => 4
]
Single Number: 5
Click me to see the sample solution
10. Viết chương trình PHP để tìm phần tử duy nhất trong một mảng trong đó mọi phần tử xuất hiện ba lần ngoại trừ một. [0] => 5 [1] => 3 [2] => 4 [3] => 3 [4] => 5 [5] => 5 [6] => 3] Xem giải pháp mẫu Write a PHP program to
find the single element in an array where every element appears three times except for one.Go to the editor
Input : array[5, 3, 4, 3, 5, 5, 3]
Output : Array
[
[0] => 5
[1] => 3
[2] => 4
[3] => 3
[4] => 5
[5] => 5
[6] => 3
]
Single Number: 4
Click me to see the sample solution
11. Viết chương trình PHP để tìm phần tử duy nhất xuất hiện một lần trong một mảng trong đó mọi phần tử xuất hiện hai lần ngoại trừ một. Chuyển đến đầu vào của Trình chỉnh sửa: Array [5, 3, 0, 3, 0, 5, 7, 7, 9] Đầu ra: 9 Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to find the single element appears once in an array where every element appears twice except for one. Go to the editor
Input : array[5, 3, 0, 3, 0, 5, 7, 7, 9]
Output : 9
Click me to see the sample solution
12. Viết chương trình PHP để thêm các chữ số của một số nguyên dương liên tục cho đến khi kết quả có một chữ số. Ví dụ, số cho trình soạn thảo là 59, kết quả sẽ là 5. Đầu vào: 48 Đầu ra: 3 Bước 1: 5 + 9 = 14 Bước 1: 1 + 4 = 5 Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to add the digits of a positive integer repeatedly until the result has a single digit.Go to the editor
For example given number is 59, the result will be 5.
Input : 48
Output : 3
Step 1: 5 + 9 = 14
Step 1: 1 + 4 = 5
Click me to see the sample solution
13. Viết chương trình PHP để đảo ngược các chữ số của số nguyên.go vào mẫu biên tập Write a PHP program to reverse the digits of an integer.Go to the editor
Sample :
x = 234, return 432
x = -234, return -432
Click me to see the sample solution
14. Viết chương trình PHP để đảo ngược các bit của số nguyên [32 bit không dấu]. Chuyển đến biên tập viên Write a PHP program to reverse the bits of an integer [32 bits unsigned]. Go to the editor
Đầu vào: 1234 Đầu ra: 1260388352 Ví dụ, 1234 được biểu thị bằng nhị phân là 10011010010 và trả về 1260388352 đại diện cho nhị phân là 100101100100000000000000000. Nhấp vào tôi để xem giải pháp mẫu mẫu
Output: 1260388352
For example, 1234 represented in binary as 10011010010 and returns 1260388352
which represents in binary as 1001011001000000000000000000000.
Click me to see the sample solution
15. Viết chương trình PHP để kiểm tra xem một chuỗi các số có tiến triển số học hay không. Chuyển đến toán học biên tập, một tiến trình số học hoặc chuỗi số học là một chuỗi các số sao cho sự khác biệt giữa các thuật ngữ liên tiếp là không đổi. Đầu vào: Mảng [5, 7, 9, 11] Đầu ra: Một chuỗi số học chẳng hạn, chuỗi 5, 7, 9, 11, 13, 15 ... là một tiến trình số học với sự khác biệt chung là 2. Nhấp vào tôi để xem Giải pháp mẫu Write a PHP program to check whether a sequence of numbers is an arithmetic progression or not. Go to the editor
In mathematics, an arithmetic
progression or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant.
Input : array[5, 7, 9, 11]
Output : An arithmetic sequence
For example, the sequence 5, 7, 9, 11, 13, 15 ... is an arithmetic progression with common difference of 2.
Click me to see the sample solution
16. Viết chương trình PHP để kiểm tra xem một chuỗi các số có tiến triển hình học hay không. Chuyển đến đầu vào của Trình chỉnh sửa: Mảng [2, 6, 18, 54] đầu ra: Trình tự hình học trong toán học, tiến trình hình học hoặc trình tự hình học, là một chuỗi các số trong đó mỗi thuật ngữ sau khi tìm thấy bằng cách nhân số trước đó Đã sửa, số khác không được gọi là tỷ lệ chung. Ví dụ, trình tự 2, 6, 18, 54, ... là một tiến trình hình học với tỷ lệ chung 3. Tương tự, 10, 5, 2.5, 1.25, ... là một chuỗi hình học với tỷ lệ chung 1/2. Nhấp vào tôi để xem giải pháp mẫu Write a
PHP program to check whether a sequence of numbers is a geometric progression or not. Go to the editor
Input : array[2, 6, 18, 54]
Output : Geometric sequence
In mathematics, a geometric progression or geometric sequence, is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio. For example,
the sequence 2, 6, 18, 54, ... is a geometric progression with common ratio 3. Similarly, 10, 5, 2.5, 1.25, ... is a geometric sequence with common ratio 1/2.
Click me to see the sample solution
17. Viết chương trình PHP để tính tổng của hai số đảo ngược và hiển thị tổng trong biểu mẫu đảo ngược. Chuyển đến đầu vào của trình soạn thảo: 13, 14 Đầu ra: 72 Lưu ý: Kết quả sẽ không duy nhất cho mỗi số, ví dụ 31 là một dạng đảo ngược của một số số là 13, 130, 1300, do đó, tất cả các số không hàng đầu sẽ bị bỏ qua. Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to compute the sum of the two reversed numbers and display the sum in reversed form.
Go to the editor
Input : 13, 14
Output : 72
Note : The result will not be unique for every number for example 31 is a reversed form of several numbers of 13, 130, 1300 etc. Therefore all the leading zeros will be omitted.
Click me to see the sample solution
18. Cho đến khi bạn đạt được 1. Chuyển đến đầu vào trình chỉnh sửa: 12 Đầu ra: Mảng [[0] => 12 [1] => 6 [2] => 3 [3] => 10 [4] => 5 [5] = > 16 [6] => 8 [7] => 4 [8] => 2 [9] => 1] Theo Wikipedia, phỏng đoán Collatz là một phỏng đoán trong toán học được đặt theo tên của Lothar Collatz, người lần đầu tiên đề xuất nó vào năm 1937. Phản đoán còn được gọi là phỏng đoán 3N + 1. Các phỏng đoán có thể được tóm tắt như sau. Lấy bất kỳ số nguyên dương n. Nếu n là chẵn, chia nó cho 2 để nhận n / 2. Nếu n là lẻ, hãy nhân nó với 3 và thêm 1 để có được 3n + 1. Lặp lại quy trình [được gọi là "một nửa hoặc ba cộng với một"] . Phạm vi phỏng đoán là bất kể bạn bắt đầu số nào, cuối cùng bạn sẽ luôn đạt được 1. Ví dụ: ví dụ: bắt đầu bằng n = 12, người ta có trình tự 12, 6, 3, 10, 5, 16, 8, 4, 4, 2, 1. n = 19, ví dụ, mất nhiều thời gian hơn để đạt 1: 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 16 , 8, 4, 2, 1. Nhấp vào tôi để xem giải pháp mẫu
Write a PHP program where you take any positive integer n, if n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process until you reach 1. Go to the editor
Input : 12
Output : Array
[
[0] => 12
[1] => 6
[2] => 3
[3] => 10
[4] => 5
[5] => 16
[6] => 8
[7] => 4
[8] => 2
[9] => 1
]
According to Wikipedia the Collatz conjecture is a conjecture in mathematics named after Lothar Collatz, who first proposed it in 1937. The conjecture is also known as the 3n + 1 conjecture.
The conjecture can be summarized as follows. Take any positive integer n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process [which has been called "Half Or Triple Plus One"] indefinitely. The
conjecture is that no matter what number you start with, you will always eventually reach 1.
Example :
For instance, starting with n = 12, one gets the sequence 12, 6, 3, 10, 5, 16, 8, 4, 2, 1.
n = 19, for example, takes longer to reach 1: 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1.
Click me to see the sample solution
19. Viết chương trình PHP để kiểm tra xem một số nhất định có phải là số xấu xí không. Chuyển đến EditorInput: 12 Đầu ra: 12 là số xấu xí là số dương là các số dương có các yếu tố chính là 2, 3 hoặc 5. Trình tự 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 12, 12, , ... hiển thị 10 số xấu xí đầu tiên. Lưu ý: 1 thường được coi là một số xấu xí. Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to check whether a given number is an ugly number. Go to the editor
Input : 12
Output :12 is an Ugly number
Ugly numbers are positive numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...
shows the first 10 ugly numbers.
Note: 1 is typically treated as an ugly number.
Click me to see the sample solution
20. Chuyển đến đầu vào của trình soạn thảo: 1 Đầu ra: Số Hamming trong toán học, tiến trình số học hoặc trình tự số học là một chuỗi các số sao cho sự khác biệt giữa các thuật ngữ liên tiếp là không đổi. Ví dụ: trình tự 5, 7, 9, 11, 13, 15 ... là một tiến trình số học với sự khác biệt phổ biến là 2. Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to get the Hamming numbers upto a given numbers also check whether a given number is an Hamming number. Go to the editor
Input : 1
Output : Hamming Number
In mathematics, an arithmetic
progression or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant.
For example, the sequence 5, 7, 9, 11, 13, 15 ... is an arithmetic progression with common difference of 2.
Click me to see the sample solution
21. Viết chương trình PHP để kiểm tra xem một chuỗi nhất định có phải là một đối thủ của một chuỗi khác không. Chuyển đến đầu vào của Trình chỉnh sửa: ['Anagram', 'Nagaram'] đầu ra: Hai chuỗi này là ANAGram theo Wikipedia, một phương pháp là chuyển đổi từ trực tiếp hoặc phát từ, kết quả của việc sắp xếp lại các chữ cái hoặc cụm từ để tạo ra một từ từ hoặc cụm từ mới, sử dụng tất cả các chữ cái gốc chính xác một lần; Ví dụ, từ xin lỗi có thể được sắp xếp lại thành nag-a-ram. Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to check whether a given string is an anagram of
another given string. Go to the editor
Input : ['anagram','nagaram']
Output : These two strings are anagram
According to Wikipedia, an anagram is direct word switch or word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example, the word anagram can be rearranged into nag-a-ram.
Click me to see the sample solution
22. Viết một chương trình PHP để đẩy tất cả các số không đến cuối một mảng. Chuyển đến đầu vào của Trình chỉnh sửa: Mảng [0,2,3,4,6,7,10] Đầu ra: Mảng [[0] => 2 [1] => 3 [2] => 4 [3] => 6 [4] => 7 [5] => 10 [6] => 0] Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to push all zeros to the end of an array. Go to the editor
Input : array[0,2,3,4,6,7,10]
Output : Array
[
[0] => 2
[1] => 3
[2] => 4
[3] => 6
[4] =>
7
[5] => 10
[6] => 0
]
Click me to see the sample solution
23. Viết chương trình PHP để tìm phần tử đa số trong một mảng. Chuyển đến đầu vào của trình soạn thảo: mảng [1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 6] N là số lượng các phần tử trong mảng. Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to find majority element in an array. Go to the editor
Input : array[1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 6]
Output : 5
Note: The majority element
is the element that appears more than n/2 times where n is the number of elements in the array.
Click me to see the sample solution
24. Viết chương trình PHP để tìm độ dài của từ cuối cùng trong một chuỗi. Chuyển đến đầu vào của Trình chỉnh sửa: Đầu ra bài tập PHP: 9 Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to find the length of the last word in a string. Go to the editor
Input : PHP Exercises
Output : 9
Click me to see the sample solution
25. Viết chương trình PHP để tìm số duy nhất xảy ra số lần lẻ và các số khác xảy ra thậm chí số lần. Chuyển đến đầu vào của trình soạn thảo: 4, 5, 4, 5, 2, 2, 3, 3, 2, 4, 4 đầu ra: 2 Nhấp vào tôi để xem giải pháp mẫu Write a PHP program to find the single number which occurs odd number of times and other numbers occur even number of times. Go to the editor
Input : 4, 5, 4, 5, 2, 2, 3, 3, 2, 4, 4
Output :
2
Click me to see the sample solution
Trình chỉnh sửa mã PHP:
Nhiều hơn nữa sẽ đến!
Không gửi bất kỳ giải pháp nào cho các bài tập trên tại đây, nếu bạn muốn đóng góp vào trang tập thể dục phù hợp.