注册 登录
编程论坛 C++教室

算法问题(数字游戏)

fl8962 发布于 2014-09-23 05:14, 772 次点击
135×2.6=351;270270×2.6=702702;这两个乘法有个共同点,135乘以2.6后,结果等于把开头第一个数字放到最后。135×2.6=351,135中的1移位到最后一位就变成了351,也就是正确的结果。
题目是,输入一个1《X《1000的数字(该数字为X),X最多保留小数点后四位。然后找出所有满足上述条件的数字y, 使得y*x= 满足上述条件。比如当x=2.6时候,当y=135,270,135135,270270;时候满足上述条件。
求最快的算法。
7 回复
#2
rjsp2014-09-23 09:31
没想出什么特别好的算法
程序代码:
#include <stdio.h>

void foo( double x )
{
    for( unsigned base=1; base<100000000; base*=10 )
    {
        for( unsigned a=1; a<10; ++a )
        {
            // {(a*base + b)*x == b*10 + a}  ==> {b/a = (base*x-1)/(10-x)}
            unsigned b = (unsigned)(a*(base*x-1)/(10-x));
            if( b<base && b*(10-x)==a*(base*x-1) )
                printf( "%u\n", a*base+b );
        }
    }
}

int main (void)
{
    foo( 2.6 );

    return 0;
}

#3
rladlgud2014-09-23 10:17
楼主的是 c++ 还是 c语言那
#4
wp2319572014-09-23 14:19
回复 3 楼 rladlgud
看这个
#include <stdio.h>
#5
fl89622014-09-23 21:21
回复 2 楼 rjsp
bi wo xie de kuai duo le ...
#6
stop12042014-09-25 03:26
[quote]以下是引用rjsp在2014-9-23 09:31:32的发言:
运行没结果.
#7
stop12042014-09-25 07:25
我还以为  123123 *2.6 = 231231

这样的话就 135 跟 270
#8
stop12042014-09-25 07:52
没的想.. 排除一些  1111  2222  123456  7654321 这样的规律数..  

3999999 这个就认为是包括小数吧.399.9999
为什么不要400以上.. 400 * 2.6 = 1040  三位数变成四位数了.
程序代码:

#include "iostream"
using namespace std;
int re(int i);
main()
{
    int i = 1;
    while (i++ < 3999999)      
        if ( i * 2.6 == re(i) )
            cout << "Matching: " << i << endl;
}
int re(int i)
{
    return i % 100 * 100 / 10 + i/100;
}

Matching: 135
Matching: 270
[Finished in 0.1s]

[ 本帖最后由 stop1204 于 2014-9-25 07:56 编辑 ]
1