给定一个只包含大写英文字母的字符串S
,要求你给出对S重新排列的所有不相同的排列数。
如:S
为ABA
,则不同的排列有ABA
、AAB
、BAA
三种。
输入一个长度不超过10
的字符串S
,我们确保都是大写的。
输出S
重新排列的所有不相同的排列数(包含自己本身)。
ABA
3
ABCDEFGHHA
907200
#include <stdio.h>
#include <string.h>
#define MAX 11
long long factorial(int n) {
long long fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
int main() {
char s[MAX];
while(scanf("%s", s) != EOF) {
int freq[26] = {0};
for (int i = 0; i < strlen(s); i++) {
freq[s[i] - 'A']++;
}
long long res = factorial(strlen(s));
for (int i = 0; i < 26; i++) {
if (freq[i] > 1) {
res /= factorial(freq[i]);
}
}
printf("%lld\n", res);
}
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
long long factorial(int n) {
long long fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
int main() {
string s;
while(getline(cin, s)) {
vector<int> freq(26, 0);
for (char c : s) {
freq[c - 'A']++;
}
long long res = factorial(s.length());
for (int f : freq) {
if (f > 1) {
res /= factorial(f);
}
}
cout << res << endl;
}
return 0;
}
package com.amoscloud.nowcoder.refactor.t0241_0250;
import java.util.*;
import java.math.*;
/**
* Created with IntelliJ IDEA.
*
* @Author: Amos
* @E-mail: amos@amoscloud.com
* @Date: 2023/7/22
* @Time: 18:04
* @Description:
*/
public class Main0249 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
String s = scanner.nextLine();
int[] freq = new int[26];
for (char c : s.toCharArray()) {
freq[c - 'A']++;
}
BigInteger res = factorial(s.length());
for (int f : freq) {
if (f > 1) {
res = res.divide(factorial(f));
}
}
System.out.println(res);
}
}
public static BigInteger factorial(int n) {
BigInteger fact = BigInteger.ONE;
for (int i = 2; i <= n; i++) {
fact = fact.multiply(BigInteger.valueOf(i));
}
return fact;
}
}
import math
while True:
try:
s = input().strip()
freq = [0] * 26
for c in s:
freq[ord(c) - ord('A')] += 1
res = math.factorial(len(s))
for f in freq:
if f > 1:
res //= math.factorial(f)
print(res)
except EOFError:
break
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function factorial(n) {
let fact = BigInt(1);
for (let i = 2; i <= n; i++) {
fact *= BigInt(i);
}
return fact;
}
rl.on('line', (line) => {
const s = line.trim();
const freq = new Array(26).fill(0);
for (let i = 0; i < s.length; i++) {
freq[s.charCodeAt(i) - 'A'.charCodeAt(0)]++;
}
let res = factorial(s.length);
for (const f of freq) {
if (f > 1) {
res /= factorial(f);
}
}
console.log(res.toString());
});
package main
import (
"fmt"
"math/big"
"bufio"
"os"
"strings"
)
func factorial(n int64) *big.Int {
result := big.NewInt(1)
for i := int64(2); i <= n; i++ {
result.Mul(result, big.NewInt(i))
}
return result
}
func main() {
reader := bufio.NewReader(os.Stdin)
for {
s, _ := reader.ReadString('\n')
s = strings.TrimSpace(s)
freq := [26]int64{}
for _, c := range s {
freq[c-'A']++
}
res := factorial(int64(len(s)))
for _, f := range freq {
if f > 1 {
res.Div(res, factorial(f))
}
}
fmt.Println(res)
}
}