Topic: 编程时涉及到的算法问题,请高手指点 |
Print this page |
1.编程时涉及到的算法问题,请高手指点 | Copy to clipboard |
Posted by: youngplayer Posted on: 2004-07-28 15:58 int x1,x2,y1,y2,k; x1 - k%5 = x2; // ---(1) y1 - k/5 = y2; // ---(2) 如何求 k??? 开始我想当然的认为 k = k%5 + (k/5)*5 = (x1-x2) + (y1-y2)*5 可运行时发现有问题 当 k%5 和 k/5 符号相反(即一正一负)时,k的值再代入等式(1) (2)中等式不成立了 请问大虾 究竟应该如何求k??? 俺想了好几天 也没想出来 若得相助 感激涕零 |
2.Re:编程时涉及到的算法问题,请高手指点 [Re: youngplayer] | Copy to clipboard |
Posted by: floater Posted on: 2004-07-29 10:20 When does this true? k%5 和 k/5 符号相反? |
3.Re:编程时涉及到的算法问题,请高手指点 [Re: youngplayer] | Copy to clipboard |
Posted by: kam Posted on: 2004-07-29 11:12 int x1,x2,y1,y2,k; x1 - k%5 = x2; // ---(1) y1 - k/5 = y2; // ---(2) ---------------------------------------- 這兩條式本身就很奇怪... 一個"未知數"只需要一條式... 現在x1, x2, y1, y2 都知道了... 求k, 其實(2) 一條式就可以做到... 即 (y1+y2)*5 必定 = k 而k亦沒有其他數可以變.... 我不太明白你的問題.....^.^ |
4.Re:编程时涉及到的算法问题,请高手指点 [Re: youngplayer] | Copy to clipboard |
Posted by: kiffen Posted on: 2004-07-29 15:42 k = ( (x1-x2) < 0 ? (x1-x2)+5 :( x1 - x2 ) )+ (y1-y2)*5 |
5.Re:编程时涉及到的算法问题,请高手指点 [Re: youngplayer] | Copy to clipboard |
Posted by: youngplayer Posted on: 2004-07-30 10:24 kam兄 可能我没说明白 这是编程时候遇到的问题 也就是说 k/5 只会产生5的倍数 比如 k=13时 则k/5=2 你明白了吗? k%5 应该不用解释了吧。 其实 x1,y1和x2,y2对应的是2个点的坐标。当x1<x2时,k%5则为负数了嘛;而此时y1>y2,则k/5为正数啊,从而导致了k%5和k/5符号相反啊 |
6.Re:编程时涉及到的算法问题,请高手指点 [Re: youngplayer] | Copy to clipboard |
Posted by: youngplayer Posted on: 2004-07-30 10:32 kiffen兄,你的这个算法好象可以解决x1-x2<0 && y1-y2>0的情况,可当x1-x2>0 && y1--y2<0 时,又改怎么办呢? |
7.Re:编程时涉及到的算法问题,请高手指点 [Re: youngplayer] | Copy to clipboard |
Posted by: youngplayer Posted on: 2004-07-30 10:39 顺便补充一点:x2,y2可以对m取模 然后位置不变 |
8.Re:编程时涉及到的算法问题,请高手指点 [Re: youngplayer] | Copy to clipboard |
Posted by: kiffen Posted on: 2004-08-02 19:02 k = ( y1-y2>0? ( (x1-x2) < 0 ? (x1-x2)+5 x1 - x2 ) )+ (y1-y2)*5 ) : ( (x1-x2) > 0 ? (y1-y2)*5 +5 - ( x1-x2) : (y1- y2)*5 + x1 - x2 ) ) |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |