ระบุตัวเลขสามตัว R, G และ B และตัวอักษร 'R', 'G' และ 'B' เท่านั้น เป้าหมายคือการหาจำนวนสตริงที่เป็นไปได้ที่สามารถทำได้โดยใช้ R Rs เป็นอย่างน้อย, อย่างน้อย G Gs และอย่างน้อย B Bs ในนั้น ตัวเลข R, G และ B มีผลรวมน้อยกว่าหรือเท่ากับความยาวของสตริงที่เป็นไปได้
ตัวอย่าง
อินพุต
R = 1, G = 1, B = 1 length=3
ผลลัพธ์
Count of number of strings (made of R, G and B) using given combination are − 6
คำอธิบาย
The possible strings will be : “RGB”, “RBG”, “BRG”, “BGR”, “GRB”, “GBR”. That is permutations of RGB.
อินพุต
R = 2, G = 0, B = 2 length=4
ผลลัพธ์
Count of number of strings (made of R, G and B) using given combination are − 6
คำอธิบาย
The possible strings will be : “RRBB”, “BBRR”, “RBRB”, “BRBR”, “RBBR”, “BRRB”.
แนวทางที่ใช้ในโปรแกรมด้านล่างมีดังนี้ −
ในแนวทางนี้ เราจะนำตัวอักษรสำหรับ R, B และ G จำนวนครั้งก่อน ตอนนี้ตรวจสอบความยาวที่เหลือ - ตัวอักษร (R+G+B) ทำการเรียงสับเปลี่ยนและรวมกันทั้งหมดและเพิ่มเพื่อนับ
-
นำค่าจำนวนเต็ม R, G, B เป็นอินพุต
-
ใช้ขนาดความยาวของสายที่จะทำ
-
การรวมฟังก์ชัน (int R, int G, int B, ขนาด int) รับอินพุตทั้งหมดและส่งกลับจำนวนสตริง (ทำจาก R, G และ B) โดยใช้ชุดค่าผสมที่กำหนด
-
นับเริ่มต้นเป็น 0
-
นับที่เหลือเป็น temp=size − (R + G + B)
-
ใช้อาร์เรย์ arr ของขนาดความยาว+1 เพื่อนำค่าของการเรียงสับเปลี่ยน
-
เริ่มแรกเก็บแฟกทอเรียลของ i ใน arr[i] ใช้สำหรับวนรอบจาก i=0 ถึง i=size Setarr[i]=arr[i−1]*i.
-
สำหรับการคำนวณชุดค่าผสมให้ข้าม arr[] อีกครั้งโดยใช้สองลูป
-
สำหรับ i=0 ถึง temp และ j=0 ถึง temp−i ให้คำนวณ temp_2 =temp − (i + j)
-
ใช้ temp_3 =arr[i + R] * arr[j + B] * arr[temp_2 + G].
-
ตอนนี้เพิ่ม arr[size] / temp_3 เพื่อนับ
-
ในตอนท้ายของการวนซ้ำทั้งหมด เราจะนับเป็นจำนวนสตริงที่ต้องการได้
-
ผลตอบแทนนับเป็นผลลัพธ์
ตัวอย่าง
#include<bits/stdc++.h>
using namespace std;
int combination(int R, int G, int B, int size){
int count = 0;
int temp = size − (R + G + B);
int arr[size+1];
arr[0] = 1;
for (int i = 1; i <= size; i++){
arr[i] = arr[i − 1] * i;
}
for (int i = 0; i <= temp; i++){
for (int j = 0; j <= temp−i; j++){
int temp_2 = temp − (i + j);
int temp_3 = arr[i + R] * arr[j + B] * arr[temp_2 + G];
count += arr[size] / temp_3;
}
}
return count;
}
int main(){
int R = 2, G = 1, B = 1;
int size = 4;
cout<<"Count of number of strings (made of R, G and B) using given combination are:
"<<combination(R, G, B, size);
return 0;
} ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น มันจะสร้างผลลัพธ์ต่อไปนี้ -
Count of number of strings (made of R, G and B) using given combination are: 12