search

Home  >  Q&A  >  body text

java - C语言的一道关于循环的练习题求解


做这道题目没什么思路?不知道该怎么下手,求大神讲解一下

ringa_leeringa_lee2805 days ago599

reply all(6)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:58:30

    http://blog.csdn.net/houyp520...

    reply
    0
  • 阿神

    阿神2017-04-18 10:58:30

    The simplest thing is to exhaustively enumerate the arrangements of 9 numbers.

    Optimization is to eliminate some impossible situations first. For example, the middle number is a multiple of 2, and the last number is a multiple of 3. And so on.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:58:30

    Just write recursion. . Or is it called iteration?

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:58:30

    public class Sidney {

    public static void main(String[] args) {
        int[] s = new int[9];
        for (int i = 300; i < 999; i+=3) {
            int flag = 1;
            Set<Integer> set = new HashSet<>();
            int a = i / 3;
            int b = (i / 3) * 2;
            s[0]=a%10;s[1]=a%100/10;s[2]=a/100;
            s[3]=b%10;s[4]=b%100/10;s[5]=b/100;
            s[6]=i%10;s[7]=i%100/10;s[8]=i/100;
            for (int i1 : s) {
                if (!set.add(i1) || i1 == 0) {
                    flag = 0;
                    break;
                }
            }
            if (flag == 1) {
                System.out.println(a + "\t" + b + "\t" + i);
            }
        }
    }

    }

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:58:30

    Liu Rujia’s question.

    Enumerate the first number, which is the number accounting for 1 in 1:2:3.

    The enumeration range is 123~345. Based on this number, calculate the other two numbers, and then determine whether exactly 9 numbers are used.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:58:30

    The idea is very simple. Since you want the ratio to be 1:2:3 and each number needs to be used once, then directly enlarge 1, 2, and 3 x times, and then determine whether each number appears only once. 1 requires less than 100 times of magnification to reach 3 digits, so just start from 123. code show as below.
    int testa()
    {

    int a=1,b=2,c =3;
    char szA[10]={0},szB[4]={0},szC[4]={0},cTag[10]={0};
    
    for(int i=123;i<=333;i++)
    {
        memset(szA,0x00,10);memset(szB,0x00,4);memset(szC,0x00,4);memset(cTag,0x00,10);
        sprintf_s(szA,"%d",a*i);sprintf_s(szB,"%d",b*i);sprintf_s(szC,"%d",c*i);
    
        strcat_s(szA,szB);strcat_s(szA,szC);
        int j=0;
        for(j=0;j<9;j++)
        {
            if(cTag[szA[j]-'1']!=0)
                break;
            cTag[szA[j]-'1']=1;
        }
    
        if(j==9)
            printf("%s\n",szA);
    }
    
    return 0;

    }

    reply
    0
  • Cancelreply