小团想要编写一个程序,希望可以统计在M和N之间(M<N,且包含M和N)有多少个六位数ABCDEF满足以下要求:
(1) ABCDEF这六个数字均不相同,即A、B、C、D、E和F表示六个不同的数字。
(2) AB+CD=EF。即将这个六位数拆成三个两位数,使得第1个和第2个两位数的和等于第3个两位数。
数据范围:
进阶:时间复杂度
,空间复杂度%5C)
小团想要编写一个程序,希望可以统计在M和N之间(M<N,且包含M和N)有多少个六位数ABCDEF满足以下要求:
(1) ABCDEF这六个数字均不相同,即A、B、C、D、E和F表示六个不同的数字。
(2) AB+CD=EF。即将这个六位数拆成三个两位数,使得第1个和第2个两位数的和等于第3个两位数。
单组输入。
输入两个六位正整数M和N(M
输出在M到N之间(包含M和N)满足要求的六位数的个数。
100000 110000
0
public static void main(String[] args) {
int size = 0;
Scanner scanner = new Scanner(System.in);
int small = scanner.nextInt();
int big = scanner.nextInt();
HashSet set = new HashSet();
for (int i = small; i <=big ; i++) {
int A = i/100000;
int B = (i % 100000)/10000;
int C = (i % 10000)/1000;
int D = (i % 1000)/100;
int E = (i % 100)/10;
int F = i % 10;
set.add(A);
set.add(B);
set.add(C);
set.add(D);
set.add(E);
set.add(F);
int AB = A*10 +B;
int CD= C*10 +D;
int EF = E*10 +F;
if (set.size() == 6 && (AB+CD) == EF){
size++;
}
set.clear();
}
System.out.println(size);
}
关键一点就是想到用 set,如果有重复的,那就长度小于6,长度等于6就可以了。最后清空set,再次判断。
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int a1 = m / 10000;
int a2 = n / 10000;
int c1 = m % 10000;
int c2 = n % 10000;
int res = 0;
res += cnt(a1, c1, 9999);
for (int i = a1 + 1; i <= a2; i++) {
res += cnt(i, 0, 9999);
}
res += cnt(a2, 0, c2);
System.out.println(res);
}
private static int cnt(int num, int m, int n) {
int a = num / 10;
int b = num % 10;
if (a == b || b == 0) {
return 0;
}
int res = 0;
boolean[] arr = new boolean[10];
arr[a] = true;
arr[b] = true;
for (int i = 1; i < 10; i++) { // 此处,i从0开始
if (arr[i]) {
continue;
}
arr[i] = true;
for (int j = 1; j < 10; j++) {
if (arr[j]) {
continue;
}
arr[j] = true;
int sum = num + i * 10 + j;
int total = i * 1000 + j * 100 + sum;
if (m <= total && n >= total && sum < 100 && !arr[sum / 10] && !arr[sum % 10]) {
res++;
}
arr[j] = false;
}
arr[i] = false;
}
return res;
}
} public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int M = in.nextInt();
int N = in.nextInt();
int res=0;
for(int i=M;i<=N;i++){
res+=handler(i);
}
System.out.println(res);
}
public static int handler(int num){
// System.out.println(num);
//六个数字均不相同
if(isReapt(num)){
return 0;
}
//先获取前两个数
int one = num/10000;
int two = num/100%100;
int three = num%100;
//System.out.println(one+" "+two+" "+three);
if(one+two==three){
return 1;
}
return 0;
}
public static boolean isReapt(int num){
HashSet<Character> set = new HashSet<>();
String str = num+"";
for(int i=0;i<str.length();i++){
if(!set.contains(str.charAt(i))){
set.add(str.charAt(i));
}else{
return true;
}
}
return false;
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int ans = 0;
int M = sc.nextInt();
int N = sc.nextInt();
while (M <= N) {
int tmp = M;
int help[] = new int[3];
int index = 2;
//讲6个数字分成三组
while (tmp != 0) {
int c = tmp % 100;
tmp /= 100;
help[index--] = c;
}
if (T(help) && (help[0] + help[1] == help[2])) {
ans++;
}
M++;
}
System.out.println(ans);
}
//判断有没有重复数字
public static boolean T(int []help) {
int []arr = new int[10];
arr[help[0] / 10]++;
arr[help[0] % 10]++;
arr[help[1] / 10]++;
arr[help[1] % 10]++;
arr[help[2] / 10]++;
arr[help[2] % 10]++;
for (int i = 0; i < 10; i++) {
if (arr[i] > 1) {
return false;
}
}
return true;
}
} const [n,m]=readline().split(' ').map(Number)
let count=0
for(let i=n;i<=m;i++){
let a = i%10;
let b = parseInt(i/10)%10;
let c = parseInt(i/100)%10;
let d = parseInt(i/1000)%10;
let e = parseInt(i/10000)%10;
let f = parseInt(i/100000);
if(a!=b&&a!=c&&a!=d&&a!=e&&a!=f&&b!=c&&b!=d&&b!=e&&b!=f&&c!=d&&c!=e&&c!=f&&d!=e&&d!=f&&e!=f){
let s=b+''+a
let t=d+''+c
let p=f+''+e
if(parseInt(t)+parseInt(p)==parseInt(s)){
count++
}
}
}
console.log(count) import java.util.HashSet;
import java.util.Scanner;
/**
* @Author: LI
* @Date: Created in 15:32 2022/8/26
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int M = sc.nextInt();
int N = sc.nextInt();
int count = 0;
for (int i = M; i <= N; i++) {
String num = i + "";
if (containSame(num)) continue;
int x = Integer.parseInt(num.substring(0, 2));
int y = Integer.parseInt(num.substring(2, 4));
int z = Integer.parseInt(num.substring(4, 6));
if (x + y == z) count++;
}
System.out.println(count);
}
private static boolean containSame(String num) {
char[] chars = num.toCharArray();
HashSet<Character> set = new HashSet<>();
for (char aChar : chars) {
if (!set.add(aChar)) {
return true;
}
}
return false;
}
}
import java.util.HashSet;
import java.util.Scanner;
/**
* @author Lei
* @create 2022-08-14 10:36
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int M = scanner.nextInt();
int N = scanner.nextInt();
int res = 0;
for (int i = M; i <= N; i++) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(i);
String string = stringBuilder.toString();
HashSet<Integer> integers = new HashSet<>();
int a = (string.charAt(0) - '0') * 10 + (string.charAt(1) - '0');
integers.add(string.charAt(0) - '0');
integers.add(string.charAt(1) - '0');
integers.add(string.charAt(2) - '0');
integers.add(string.charAt(3) - '0');
integers.add(string.charAt(4) - '0');
integers.add(string.charAt(5) - '0');
int b = (string.charAt(2) - '0') * 10 + (string.charAt(3) - '0');
int c = (string.charAt(4) - '0') * 10 + (string.charAt(5) - '0');
if (integers.size() == 6 && a + b == c) {
res++;
}
integers.clear();
}
System.out.println(res);
}
}
#include<bits/stdc++.h>
using namespace std;
int l,r;
int main(){
cin>>l>>r;
int count=0;
for(int i=l;i<=r;i++){
string str=to_string(i);
set<char>mp;
for(auto s : str){
mp.insert(s);
}
if(mp.size()==6){
if(i/10000 + ((i/1000%10)*10 + i/100%10) == i%100) count++;
}
}
cout<<count;
return 0;
}
//上述“i/10000 + ((i/1000%10)*10 + i/100%10) == i%100”借鉴的是牛友的精妙方法。 #include<iostream>
using namespace std;
#include<string>
#include<set>
int main()
{
int num1, num2;
cin >> num1 >> num2;
int n = 0;
for (int i = num1; i <= num2; i++)
{
set<int> s1;
int A = i / 100000;
int B = (i % 100000) / 10000;
int C = (i % 10000) / 1000;
int D = (i % 1000) / 100;
int E = (i % 100) / 10;
int F = i % 10;
s1.insert(A);
s1.insert(B);
s1.insert(C);
s1.insert(D);
s1.insert(E);
s1.insert(F);
int AB = A * 10 + B;
int CD = C * 10 + D;
int EF = E * 10 + F;
if ((s1.size() == 6) && (AB + CD) == EF)
{
n++;
}
s1.clear();
}
cout << n << endl;
system("pause");
return 0;
} #include<bits/stdc++.h>
using namespace std;
int fun1(int n)
{ //a b c d e f
int a = n%10;
int b = n/10%10;
int c = n/100%10;
int d = n/1000%10;
int e = n/10000%10;
int f = n/100000;
if(a!=b&&a!=c&&a!=d&&a!=e&&a!=f&&b!=c&&b!=d&&b!=e&&b!=f&&c!=d&&c!=e&&c!=f&&d!=e&&d!=f&&e!=f) return 1;
else return 0;
}
int fun2(int n)
{
if(n/10000 + ((n/1000%10)*10 + n/100%10) == n%100) return 1;
else return 0;
}
int main()
{
int a,b;
while (cin >> a >> b)
{
int c = 0;
for (int i=a;i<=b;i++)
{
if(fun1(i) == 1 && fun2(i) == 1) c++;
}
cout << c << endl;
}
return 0;
} package main
import (
"fmt"
"strconv"
)
func main() {
var M,N,cnt int
fmt.Scan(&M,&N)
for i:=M;i<=N;i++{
str:=strconv.Itoa(i)
a, _ := strconv.Atoi(str[0:1])
b, _ := strconv.Atoi(str[1:2])
c, _ := strconv.Atoi(str[2:3])
d, _ := strconv.Atoi(str[3:4])
e, _ := strconv.Atoi(str[4:5])
f, _ := strconv.Atoi(str[5:6])
if a!=b&&a!=c&&a!=d&&a!=e&&a!=f&&
b!=c&&b!=d&&b!=e&&b!=f&&c!=d&&c!=e&&c!=f&&d!=e&&d!=f&&e!=f{
if a*10+b+c*10+d==e*10+f{
cnt++
}
}
}
fmt.Println(cnt)
} import java.util.*;
//排列组合,我觉得很香!
public class Main{
public static int res = 0;
public static int M;
public static int N;
public static void main(String[] args){
Scanner input = new Scanner(System.in);
M = input.nextInt();
N = input.nextInt();
int[] dataList = new int[]{0,1,2,3,4,5,6,7,8,9};
aSelect(dataList,new int[6],0);
System.out.println(res);
}
public static void aSelect(int[] dataList,int[] resultList,int resultIndex){
if(resultIndex == 6){
int ab = resultList[0]*10+resultList[1];
int cd = resultList[2]*10+resultList[3];
int ef = resultList[4]*10+resultList[5];
int now = ab*10000+cd*100+ef;
if( now >= M && now <= N && ab + cd == ef){
res++;
}
return;
}
for(int i = 0;i<dataList.length;i++){
boolean exist = false;
for(int j = 0;j < resultIndex;j++){
if(dataList[i] == resultList[j]){
exist=true;
break;
}
}
if(!exist){
resultList[resultIndex] = dataList[i];
aSelect(dataList,resultList,resultIndex+1);
}
}
}
} inin = list(map(int, input('').split()))
lower = inin[0]
higher = inin[1]
resp = 0
for i in range(lower, higher + 1):
char = str(i)
test = []
for j in char:
if j not in test:
test.append(j)
if len(test) == 6:
if char[0] != '0' and char[2] != '0' and char[4] != '0':
if int(char[0:2]) + int(char[2:4]) == int(char[4:6]):
resp += 1
print(resp)