Algorithm in C - playing with numbers - number with 3 in units place

后端 未结 4 732
闹比i
闹比i 2020-12-24 08:46

I came across this question in an interview. Any number with 3 in its units position has at least one multiple containing all ones. For instance, a multiple of 3 is 111, a

4条回答
  •  没有蜡笔的小新
    2020-12-24 09:29

    The multiple of 23 is 1111111111111111111111

    #include 
    
    int
    main () {
            unsigned int ones = 1;
            double result, by = 23, dividend = 1;
            while (dividend) {
                result = dividend / by;
                    if (result < 1) {
                        dividend = dividend * 10 + 1;
                            ++ones;
                    } else {
                        dividend -= by * (int)result;
                    }
            }
            while (ones--) {
                printf("1");
            }
            printf("\n");
        return 0;
    }
    

提交回复
热议问题