9个数字的排列问题

Quebradawill 2015-12-13 08:15:17
加精
用1,2,3,……,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3,给出所有解。
...全文
2538 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
开心秋水 2018-08-07
  • 打赏
  • 举报
回复
穷举即可!~
lin5161678 2018-08-07
  • 打赏
  • 举报
回复
引用 5 楼 lm_whales 的回复:
第一个数 范围是 123~987/3=329
这个范围的数,一定是3位数
如果1倍,2倍,3倍 数字无重复且没有0 ,则是一组否则不是
来个最笨的穷举法:
int main(){
unsigned flag =0;
unsigned right =(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9);
int abc,def,ghi;
for(abc=123;abc<987/3;abc++)
{
flag =0;
def =abc*2;
ghl =abc*3;
flag|= (1<<(abc/100))|(1<<(abc/10%10))|=(1<<(abc%10));
flag|= (1<<(def/100))|(1<<(def/10%10))|=(1<<(def%10));
flag|= (1<<(ghi/100))|(1<<(ghi/10%10))|=(1<<(ghi%10));
if(flag ==right)
printf("%d:%d:%d",abc,def,ghi);
}
return 0;
}

flag|= (1<<(abc/100))|(1<<(abc/10%10))|=(1<<(abc%10));错了
小板栗子 2018-08-06
  • 打赏
  • 举报
回复
初学者 请勿介意
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>


#include<ctime>
using namespace std;
/*
用1,2,3,……,9组成3个三位数abc,def和ghi,每个数字恰好使用一次,要求abc:def:ghi=1:2:3,给出所有解。
*/
int main()
{
srand((unsigned int)time(NULL));
int one, two, three;
while (true)
{
int arr[9] = { 1,2,3,4,5,6,7,8,9 };
int arr1[9];
for (int i = 0; i < 9; i++)
{
int num = rand() % 9;
int pos = i;
for (int j = 0; j < i; j++)
{
if (num == arr1[j])
{
i--;
break;
}
}
if (pos == i)
{
arr1[i] = num;
}
}
/*for (int i = 0; i < 9; i++)
{
cout << arr1[i] << endl;
}*/
one = arr[arr1[0]] * 100 + arr[arr1[1]] * 10 + arr[arr1[2]];
two = arr[arr1[3]] * 100 + arr[arr1[4]] * 10 + arr[arr1[5]];
three = arr[arr1[6]] * 100 + arr[arr1[7]] * 10 + arr[arr1[8]];
if (two==one*2 && three == one*3)
{
for (int i = 0; i < 9; i++)
{
if (i % 3 == 0&&i!=0)
{
cout << endl;
}
cout << arr[arr1[i]];
}
break;
}
}
system("pause");
return EXIT_SUCCESS;
}
  • 打赏
  • 举报
回复
程序实现功能都很好,但是不够优化,如果超过20行(包括空行)都不能得分。
DogDeng 2016-09-21
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
#include <math.h>
bool isNumBitSame(int , int );
bool isNumHasSame(int);
void intToArray(int , int *);
int main()
{
	using namespace std;
	int num1, num2, num3;
	int num1Array[3], num2Array[3], num3Array[3];

	for (num1 = 100; num1 < 329; num1++)
	{
		num2 = num1 * 2;
		if (!isNumBitSame(num1, num2))
		{
			num3 = num1 * 3;
			if (!isNumBitSame(num1, num3) && !isNumBitSame(num2, num3))
			{
				if (!isNumHasSame(num1) && !isNumHasSame(num2) && !isNumHasSame(num3))
				{
					cout << num1 << "  " << num2 << "  " << num3 << "  " << endl;
				}
			}
		}

	}
	return 0;

}

bool isNumBitSame(int a, int b)      
{
	using namespace std;
	int ap[3], bp[3];
	intToArray(a, ap);
	intToArray(b, bp);
	int i, j;
	for (i = 0; i < 3; i++)
		for (j = 0; j < 3; j++)
			if (ap[i] == bp[j])
				return true;
	return false;
}

bool isNumHasSame(int a)
{
	int ap[3];
	intToArray(a, ap);
	int i, j;
	for (i = 0; i < 3; i++)
		for (j = i+1; j < 3; j++)
			if (ap[i] == ap[j])
				return true;
	for (i = 0; i < 3; i++)
		if (ap[i] == 0)
			return true;
	return false;
}

void intToArray(int a, int *aArray)      
{
	int i;
	for (i = 0; i < 3; i++)
	{
		aArray[i] = a / int(pow(10, 2 - i));
		a = a - aArray[i] * int(pow(10, 2 - i));
	}
}
//192 384 576 //219 438 657 //273 546 819 //327 654 981 新手代码,多多指教
li4c 2016-05-06
  • 打赏
  • 举报
回复
// fuck project main.go
package main

import (
	"fmt"
	"strconv"
	"strings"
)

type result struct {
	abc int
	def int
	ghi int
}

func (r result) String() string {
	return fmt.Sprintf("abc:%d,bcd:%d,ghi:%d", r.abc, r.def, r.ghi)
}

func fuck() []result {
	var buffer []result
	a := 123
	for {
		a3 := a * 3
		if a3 > 987 {
			break
		}
		stra := strconv.Itoa(a)
		strb := strconv.Itoa(a * 2)
		strc := strconv.Itoa(a3)
		if func(str string) bool {
			state := true
			for i := 1; i <= 9; i++ {
				if false == strings.Contains(str, strconv.Itoa(i)) {
					state = false
					break
				}
			}
			return state
		}(stra + strb + strc) {
			result := new(result)
			result.abc = a
			result.def = a * 2
			result.ghi = a3
			buffer = append(buffer, *result)
		}
		a++
	}
	return buffer
}
func main() {
	buffer := fuck()
	for _, data := range buffer {
		fmt.Println(data)
	}
}
凑热闹
qq_34802448 2016-05-05
  • 打赏
  • 举报
回复
这个题目so easy
qq_33443408 2015-12-20
  • 打赏
  • 举报
回复
学习了 学习了 学习了
tcmakebest 2015-12-19
  • 打赏
  • 举报
回复
这只是一个简单的穷举法,从 123 至 987/3 的整数,然后乘以 1002003, 得出的9位数结果中没有0,没有重复数字.
lm_whales 2015-12-17
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
纠正上帖:
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
int main() {
    vector<char> P(9);
    vector<char>::iterator b,e;
    b=P.begin();
    e=P.end();
    for (int i=0;i<9;i++) P[i]=i+1;
    do {
        double abc,def,ghi;
        abc=P[0]*100.0+P[1]*10.0+P[2];
        def=P[3]*100.0+P[4]*10.0+P[5];
        ghi=P[6]*100.0+P[7]*10.0+P[8];
        if (fabs(def/abc-2.0)<0.0001 && fabs(ghi/abc-3.0)<0.0001) printf("%lg %lg %lg\n",abc,def,ghi);
    } while (next_permutation(b,e));
    return 0;
}
//192 384 576
//219 438 657
//273 546 819
//327 654 981
//
赵老师,这个完全可以不用浮点数,修改了一下,一点问题都没有。
lm_whales 2015-12-17
  • 打赏
  • 举报
回复
第一个数 范围是 123~987/3=329 这个范围的数,一定是3位数 如果1倍,2倍,3倍 数字无重复且没有0 ,则是一组否则不是 来个最笨的穷举法: int main(){ unsigned flag =0; unsigned right =(1<<1)|(1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9); int abc,def,ghi; for(abc=123;abc<987/3;abc++) { flag =0; def =abc*2; ghl =abc*3; flag|= (1<<(abc/100))|(1<<(abc/10%10))|=(1<<(abc%10)); flag|= (1<<(def/100))|(1<<(def/10%10))|=(1<<(def%10)); flag|= (1<<(ghi/100))|(1<<(ghi/10%10))|=(1<<(ghi%10)); if(flag ==right) printf("%d:%d:%d",abc,def,ghi); } return 0; }
lm_whales 2015-12-17
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
纠正上帖:
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
int main() {
    vector<char> P(9);
    vector<char>::iterator b,e;
    b=P.begin();
    e=P.end();
    for (int i=0;i<9;i++) P[i]=i+1;
    do {
        double abc,def,ghi;
        abc=P[0]*100.0+P[1]*10.0+P[2];
        def=P[3]*100.0+P[4]*10.0+P[5];
        ghi=P[6]*100.0+P[7]*10.0+P[8];
        if (fabs(def/abc-2.0)<0.0001 && fabs(ghi/abc-3.0)<0.0001) printf("%lg %lg %lg\n",abc,def,ghi);
    } while (next_permutation(b,e));
    return 0;
}
//192 384 576
//219 438 657
//273 546 819
//327 654 981
//
不用这么恐怖吧 3位数的3倍不超过3000吧,你这是顺便计算5,6,7,8,9,10位数吗?
rickylin86 2015-12-17
  • 打赏
  • 举报
回复
下面代码可以获得全部答案: 192 219 273 327

import java.util.Arrays;

public class Test{
	public static void main(String[] args){
		int start = 123; //最小的数字
		int end = 987;//最大的数字
		int result = 0;
		for(result = start ; result <= end ; result ++){
			if(check(result)){
				System.out.println(result);
			}
		}

		
	}

	private static boolean check(int index){
		int second = 2 * index;
		int third = 3 * index;
		String content = index + "" + second + third;
		String[] strs = content.split("");//将字符串拆散成数组
		Arrays.sort(strs); //排序数组
		String result = "";
		for(String str : strs){
			result += str;
		}
		return result.equals("123456789");
	}
}
rickylin86 2015-12-17
  • 打赏
  • 举报
回复
这是java的实现.答案192

import java.util.Arrays;

public class Test{
	public static void main(String[] args){
		int start = 123; //最小的数字
		int end = 987;//最大的数字
		int result = 0;
		for(result = start ; result <= end ; result ++){
			if(check(result)){
				break;
			}
		}

		System.out.println(result);
	}

	private static boolean check(int index){
		int second = 2 * index;
		int third = 3 * index;
		String content = index + "" + second + third;
		String[] strs = content.split("");//将字符串拆散成数组
		Arrays.sort(strs); //排序数组
		String result = "";
		for(String str : strs){
			result += str;
		}
		return result.equals("123456789");
	}
}
赵4老师 2015-12-17
  • 打赏
  • 举报
回复
在14行前加一句
if (abc>333) break;
赵4老师 2015-12-17
  • 打赏
  • 举报
回复
不用浮点数:
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    vector<char> P(9);
    vector<char>::iterator b,e;
    b=P.begin();
    e=P.end();
    for (int i=0;i<9;i++) P[i]=i+1;
    do {
        int abc,def,ghi;
        abc=P[0]*100+P[1]*10+P[2];
        def=P[3]*100+P[4]*10+P[5];
        ghi=P[6]*100+P[7]*10+P[8];
        if (abc*2==def && abc*3==ghi) printf("%d %d %d\n",abc,def,ghi);
    } while (next_permutation(b,e));
    return 0;
}
//192 384 576
//219 438 657
//273 546 819
//327 654 981
//
fly_dragon_fly 2015-12-14
  • 打赏
  • 举报
回复
int ct[10];
bool rep(int n){
    if(n>999&&n<100) return true;
    while(n){
        int k=n%10;
        if(++ct[k]!=1) return true;
        n /=10;
    }
    return false;
}
void solve()
{
    for(int i=100;i<1000/3;i++){
        memset(ct,0,sizeof ct);
        if(rep(i)||rep(i*2)||rep(i*3)) continue;
        printf("%d+%d=%d\n",i,i*2,i*3);
    }
}
赵4老师 2015-12-14
  • 打赏
  • 举报
回复
纠正上帖:
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
int main() {
    vector<char> P(9);
    vector<char>::iterator b,e;
    b=P.begin();
    e=P.end();
    for (int i=0;i<9;i++) P[i]=i+1;
    do {
        double abc,def,ghi;
        abc=P[0]*100.0+P[1]*10.0+P[2];
        def=P[3]*100.0+P[4]*10.0+P[5];
        ghi=P[6]*100.0+P[7]*10.0+P[8];
        if (fabs(def/abc-2.0)<0.0001 && fabs(ghi/abc-3.0)<0.0001) printf("%lg %lg %lg\n",abc,def,ghi);
    } while (next_permutation(b,e));
    return 0;
}
//192 384 576
//219 438 657
//273 546 819
//327 654 981
//
赵4老师 2015-12-14
  • 打赏
  • 举报
回复
此题无解

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧