将 1,2,…,9 共 9 个数分成 3 组,分别组成 3 个三位数,且使这 3 个三位数构成 1 : 2 : 3的比例,试求出所有满足条件的 3位数

 这道题的本质是1-9中的数字不重复,因此只要将每一个仨位数的个十百位的数字分离出来即可

1.

#include<stdio.h>
int i, j, v; int a[10];//ai表示第i个数已经用过了
int main()
{
	for (i = 192; i <= 327; i++)//第一个数最小192,最大327。其实不知道的情况下简单来说是从123-329的但是算出来是最值就稍微改了下下
	{
		memset(a, 0, sizeof(a)); v = 0;//清零
		a[i % 10] = a[i / 10 % 10] = a[i / 100] = a[i * 2 % 10] = a[i * 2 / 10 % 10] = a[i * 2 / 100] = a[i * 3 % 10] = a[i * 3 / 10 % 10] = a[i * 3 / 100] = 1;//统计数字
		for (j = 1; j <= 9; j++) v += a[j];//v表示1-9这些数字是否全部齐了
		if (v == 9) printf("%d %d %d\n", i, i * 2, i * 3);//如果齐了就输出
	}
	return 0;
}

 a[i % 10] = a[i / 10 % 10] = a[i / 100] = a[i * 2 % 10] = a[i * 2 / 10 % 10] = a[i * 2 / 100] = a[i * 3 % 10] = a[i * 3 / 10 % 10] = a[i * 3 / 100] = 1,为拿出个十百位当索引

2. 以下为错误做法

#include<stdio.h>
int main()
{
	for (int a = 123;a<333;a++)
	{
		int b = a * 2;
		int c = a * 3;
		if (a % 10 != a / 10 % 10 != a / 100 != b % 10 != b / 10 % 10 != b / 100 != c % 10 != c / 10 % 10 != c / 100)//3个3位数的个什百位不相同
 {
			printf("%-5d%-5d%-5d", a, b, c);
		}

	}
	return 0;
}

a % 10 != a / 10 % 10 != a / 100 != b % 10 != b / 10 % 10 != b / 100 != c % 10 != c / 10 % 10 != c / 100 这个表达式是从左往右进行而C语言中条件判断的结果只返回1(真)或0(假),因此这个表达式很大几率为1(真)与我们的本意所有的都不相等相违背,且注意到没有b和c中可能有0,而这个并没有排除。

上面的改正:

#include<stdio.h>
int main()
{
	for (int a = 123;a<333;a++)
	{
		int arr[9]={0};
		int b = a * 2;
		int c = a * 3;
		arr[0] = a % 10;
		arr[1] = a / 10 % 10;
		arr[2] = a / 100;
		arr[3] = b % 10;
		arr[4] = b / 10 % 10;
		arr[5] = b / 100;
		arr[6] = c % 10; 
		arr[7] = c / 10 % 10;
		arr[8] = c / 100;
		int falg = 1;
		for (int i = 0; i < 9; i++) {
			for (int j = i + 1; j < 9; j++) {
				if (arr[i] == arr[j]) {
					falg = 0;
				}
			}
			if (arr[i] == 0) {
				falg = 0;
			}
		}
		if (falg) {
			printf("%-5d%-5d%-5d\n", a, b, c);
		}

	}
	return 0;
}

 

3. 以下为java做法,要用到string类中的int类型转字符串类型中的对象:string   .valueOf(int i)和检查序列的单个字符:string .contains(charsequence s)(CharSequence 是 char 值的一个可读序列,就是读取char类型)

public class Main {

  /**
   * 判断是否含有九个数字
   */
  public static boolean legal(int a, int b, int c) {
    String str = String.valueOf(a) + String.valueOf(b) + String.valueOf(c);//字符串拼接
    if (str.contains("1") && str.contains("2") && str.contains("3") && str.contains("4")
        && str.contains("5") && str.contains("6") && str.contains("7") && str.contains("8")
        && str.contains("9"))
      return true;
    else
      return false;
  }

  public static void main(String[] args) {
    int b, c;
    for (int a = 123; a <= 329; a++) {
      b = 2 * a;
      c = 3 * a;
      if (legal(a, b, c))
        System.out.println(a + " " + b + " " + c);
    }
  }
}

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值