檢查每個單詞的字元是否可以重新排列成等差數列 (AP)
在本文中,我們將討論如何檢查給定字串中每個單詞的字元是否可以重新排列成等差數列 (AP)。我們還將用 C++ 實現解決方案,並提供一個示例來說明程式碼的工作原理。
等差數列 (AP)
等差數列 (AP) 是一串數字,其中除第一個數字外,每個數字都是透過將一個常數 d 加到前一個數字得到的。常數 d 稱為公差。
例如,數列 1, 3, 5, 7, 9 是一個公差為 2 的等差數列。
方法
要檢查給定字串中每個單詞的字元是否可以重新排列成等差數列,我們將遵循以下方法:
我們將給定字串分割成單個單詞。
對於每個單詞,我們將按字母順序排列字元。
我們將計算排序後的單詞中每對相鄰字元之間的公差。
如果所有相鄰字元對的公差都相同,則該單詞的字元可以重新排列成等差數列。
我們將對給定字串中的所有單詞重複步驟 2-4。
如果所有單詞都可以重新排列成等差數列,則返回 true。否則,返回 false。
示例
以下是上述方法的程式:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
bool canFormAP(char *s) {
int len = strlen(s);
char words[100][100]; // Assuming a maximum of 100 words with 100 characters each
int wordCount = 0;
int wordLen = 0;
for (int i = 0; i <= len; i++) {
if (s[i] == ' ' || s[i] == '\0') {
words[wordCount][wordLen] = '\0'; // Null-terminate the word
wordCount++;
wordLen = 0;
} else {
words[wordCount][wordLen] = s[i];
wordLen++;
}
}
for (int i = 0; i < wordCount; i++) {
int n = strlen(words[i]);
if (n <= 2) {
continue;
}
char d = words[i][1] - words[i][0];
for (int j = 2; j < n; j++) {
if (words[i][j] - words[i][j - 1] != d) {
return false;
}
}
}
return true;
}
int main() {
char s[] = "abcdefgh";
if (canFormAP(s)) {
printf("Characters of each word can be rearranged to form an Arithmetic Progression\n");
} else {
printf("Characters of each word cannot be rearranged to form an Arithmetic Progression\n");
}
return 0;
}
輸出
Characters of each word can be rearranged to form an Arithmetic Progression
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// Function to check if characters of each word can be rearranged to form an Arithmetic Progression
bool canFormAP(string s) {
vector<string> words;
string word = "";
// Split the input string into words using spaces
for (char c : s) {
if (c == ' ') {
words.push_back(word); // Store the word in the vector
word = ""; // Reset the word
} else {
word += c; // Build the word character by character
}
}
words.push_back(word); // Store the last word
// Check if characters within each word can form an Arithmetic Progression
for (string w : words) {
sort(w.begin(), w.end());
int n = w.length();
if (n <= 2) {
continue;
}
int d = w[1] - w[0]; // Calculate the common difference
for (int i = 2; i < n; i++) {
if (w[i] - w[i-1] != d) {
return false; // Not an Arithmetic Progression
}
}
}
return true; // All words satisfy the condition
}
int main() {
string s = "abcdefgh";
// Check if characters of each word can be rearranged to form an Arithmetic Progression
if (canFormAP(s)) {
cout << "Characters of each word can be rearranged to form an Arithmetic Progression\n";
} else {
cout << "Characters of each word cannot be rearranged to form an Arithmetic Progression\n";
}
return 0;
}
輸出
Characters of each word can be rearranged to form an Arithmetic Progression
import java.util.Arrays; // Import the Arrays class
public class Main {
public static boolean canFormAP(String s) {
String[] words = s.split(" "); // Split the input string into words
for (String w : words) {
char[] arr = w.toCharArray();
Arrays.sort(arr); // Sort characters of the word
int n = arr.length;
if (n <= 2) {
continue;
}
int d = arr[1] - arr[0];
for (int i = 2; i < n; i++) {
if (arr[i] - arr[i - 1] != d) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
String s = "abcdefgh";
if (canFormAP(s)) {
System.out.println("Characters of each word can be rearranged to form an Arithmetic Progression");
} else {
System.out.println("Characters of each word cannot be rearranged to form an Arithmetic Progression");
}
}
}
輸出
Characters of each word can be rearranged to form an Arithmetic Progression
def can_form_ap(s):
words = s.split() # Split the input string into words
for w in words:
w = ''.join(sorted(w)) # Sort characters of the word
n = len(w)
if n <= 2:
continue
d = ord(w[1]) - ord(w[0])
for i in range(2, n):
if ord(w[i]) - ord(w[i - 1]) != d:
return False
return True
s = "abcdefgh"
if can_form_ap(s):
print("Characters of each word can be rearranged to form an Arithmetic Progression")
else:
print("Characters of each word cannot be rearranged to form an Arithmetic Progression")
輸出
Characters of each word can be rearranged to form an Arithmetic Progression
在這個示例中,給定的字串是“the quick brown fox jumps over the lazy dog”。字串中的每個單詞都可以重新排列成等差數列。例如,單詞“quick”可以重新排列成序列“cikqu”,這是一個公差為 2 的等差數列。正如我們所討論的,單詞“lazy”可以重新排列成序列“alzy”,這是一個公差為 11 的等差數列。
因此,在這個示例中,給定字串中每個單詞的字元都可以重新排列成等差數列,程式碼的輸出是“每個單詞的字元都可以重新排列成等差數列”。
結論
在本文中,我們討論瞭如何檢查給定字串中每個單詞的字元是否可以重新排列成等差數列 (AP)。我們遵循了一個簡單的方法,包括排序每個單詞的字元並檢查每對相鄰字元之間的公差是否相同。我們還提供了 C++ 解決方案的實現,並用示例測試用例進行了說明。
這個問題有各種實際應用。例如,在密碼學中,重新排列字串的字元可以用來加密原始訊息,檢查字元是否可以重新排列成 AP 可以用作解密過程中的驗證步驟。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP