首页 > 试题广场 >

六位数

[编程题]六位数
  • 热度指数:2815 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

小团想要编写一个程序,希望可以统计在MN之间(M<N,且包含MN)有多少个六位数ABCDEF满足以下要求:

(1) ABCDEF这六个数字均不相同,即ABCDEF表示六个不同的数字。

(2) AB+CD=EF。即将这个六位数拆成三个两位数,使得第1个和第2个两位数的和等于第3个两位数。


数据范围:
进阶:时间复杂度,空间复杂度

输入描述:

单组输入。

输入两个六位正整数M和N(M



输出描述:

输出在M到N之间(包含M和N)满足要求的六位数的个数。

示例1

输入

100000 110000

输出

0
(注意:ABCDEF都必须是正常的两位数,因此ACE都不能等于0。)
 
红字这里应该删掉
发表于 2021-02-25 11:19:27 回复(3)
这题真是绝了,做着做着怀疑人生了。
发表于 2021-02-28 21:20:33 回复(1)
卡在20%,怀疑人生!!!
发表于 2021-03-08 20:25:41 回复(3)
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,再次判断。
发表于 2022-03-22 14:34:30 回复(0)
牛客的题目质量怎么和狗屎一样
发表于 2021-08-27 16:08:37 回复(0)
测试case里面,C是可以为0的。题目有问题。代码注释处改一下
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;
    }
}

发表于 2021-03-08 19:16:48 回复(0)
什么鬼?A、C、E哪里不能为0了
M, N = map(int, input().split())
count = 0
for num in range(M, N + 1):
    str_num = str(num)
    if len(set(list(str_num))) != 6:
        continue
    if int(str_num[:2]) + int(str_num[2:4]) == int(str_num[-2:]):
        count += 1
print(count)


发表于 2021-03-05 12:51:44 回复(5)
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;
    }
}

发表于 2023-03-08 20:07:10 回复(0)
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;


    }
}

发表于 2023-02-26 22:27:05 回复(0)
js暴力求解
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)


发表于 2022-09-20 10:07:10 回复(0)
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;
    }
}

发表于 2022-08-26 19:45:28 回复(0)
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);
    }
}

发表于 2022-08-14 11:06:09 回复(0)
用set来判断是否有重复数字。
#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”借鉴的是牛友的精妙方法。

发表于 2022-08-12 21:36:38 回复(0)
#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;
}

编辑于 2022-08-12 21:26:06 回复(0)
#include <stdio.h>
int main()
{
    int M,N,ab,cd,ef;
    scanf("%d",&M);
    scanf("%d",&N);
    int count=0;
    for(ab=M/10000;ab<=N/10000;ab++)
    {
        if(ab/10==ab%10)continue;
        for(cd=0;cd<=98;cd++)
        {
            if(cd/10==cd%10||cd/10==ab/10||cd/10==ab%10||cd%10==ab/10||cd%10==ab%10)continue;
            for(ef=10;ef<=98;ef++)
            {
                if(ef/10==ef%10||ef/10==ab%10||ef/10==cd%10||ef/10==ab/10||ef/10==cd/10||ef%10==ab%10||ef%10==cd%10||ef%10==ab/10||ef%10==cd/10)continue;
                if((ab+cd)==ef)
                {
                    int temp=ab*10000+cd*100+ef;
                    if(temp>=M&&temp<=N)
                    {
                        count++;
                    }
                }
            }
        }
    }
    printf("%d\n",count);
    return 0;
}
//for循环暴力解题
//0的话在六位数中是00,就不符合题意了。
编辑于 2022-06-27 14:25:01 回复(1)
直接暴力就完事了,秒杀一切牛鬼蛇神数据
#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;
}



发表于 2022-06-26 11:30:02 回复(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)

}


编辑于 2022-05-26 00:02:40 回复(0)
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);
            }
        }
    }
}

发表于 2022-03-18 21:17:44 回复(0)

/*
题目有问题 此处不限制为0
*/
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        int sum = 0;

        for(int i=m;i<=n;i++){
            String[] str = String.valueOf(i).split("");
           
            int temp = 0;
            Set<String> set = new HashSet<>();
            for(int j=0;j<6;j++){
                set.add(str[j]);
            }
            if(set.size()!=6)
                continue;
            if((Integer.valueOf(str[0]+str[1])+Integer.valueOf(str[2]+str[3]))==
                    Integer.valueOf(str[4]+str[5])){
                sum++;
            }
        }

        System.out.println(sum);
    }
}



发表于 2021-09-24 10:48:57 回复(0)
对于这种NC题目实在不想多说什么...,就硬卡20%,看了评论才直到怎么回事。
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)

发表于 2021-09-10 06:19:30 回复(0)