算法将数字分成两部分 [英] algorithm to split number into two pieces

查看:78
本文介绍了算法将数字分成两部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hey Guys,


我需要一个算法/公式来执行以下操作:


我有两个32位定时器级联到形成一个64位定时器,每个定时器最大值

(50秒)。这是他们的工作方式:


值| timer1 | timer2

5 50 3

当timer1倒计时到零时,timer2会倒数到两个。

Timer1会重新加载值50并启动倒计时,这继续

直到timer2达到零。


我需要能够最有效地加载它们。那么,伪代码看起来像这样?
这个?


if(value< max_value)

load timer1 = value;

load timer2 = 1;

if(value == even number)&& (值< max_value * 2)

load timer1 = value / 2;

load timer2 = 2;

查看不同值的列表:


值| timer1 | timer2

10 | 10 | 1

50 | 50 | 1(max_value)

60 | 30 | 2

80 | 40 | 2

100 | 50 | 2(max_value * 2)

110 | 11 | 10

现在好玩的开始,因为我无法加载超过

50的值的timer1,我必须拆分值110,以便timer1拥有

最大值可能。更多场景,

120 | 30 | 4

150 | 50 | 3

160 | 40 | 4

我无法看到实现算法的模式。任何帮助将非常感谢


Hey Guys,

I need an algorithm/formula to do the following:

I have two 32-bit timers cascaded to form a 64-bit timer, max value
per timer(50sec). This is the way they work:

value | timer1 | timer2
5 50 3
when timer1 counts down to zero, timer2 would count down to two.
Timer1 would reload the value 50 and start counting down, this goes on
till timer2 reaches zero.

I need to be able to load them most efficiently. So, pseudo-code looks
like this??

if (value < max_value)
load timer1 = value;
load timer2 = 1;
if (value == even number) && (value < max_value*2)
load timer1 = value/2;
load timer2 = 2;
For a list of different values:

value | timer1 | timer2
10 | 10 | 1
50 | 50 | 1 (max_value)
60 | 30 | 2
80 | 40 | 2
100 | 50 | 2 (max_value*2)
110 | 11 |10
Now the fun starts, since I cannot load timer1 with a value more than
50, I have to split the value 110 so that timer1 would have the
maximum value possible. Some more scenarios,
120 | 30 | 4
150 | 50 | 3
160 | 40 | 4
I just can''t see a pattern to implement an algorithm. Any help will be
greatly appreciated.

推荐答案

" Jay" < JA ***************** @ honeywell.com>在消息中写道

news:d0 ************************** @ posting.google.c om ...
"Jay" <ja*****************@honeywell.com> wrote in message
news:d0**************************@posting.google.c om...
嘿伙计们,

我需要一个算法/公式来执行以下操作:

我有两个32位定时器级联形成64位定时器,每个定时器的最大值
(50秒)。这是他们的工作方式:

价值| timer1 | timer2
5 50 3
当timer1倒计时到零时,timer2会倒计时到两个。
Timer1会重新加载值50并开始倒计时,这会持续到timer2达到零。

我需要能够最有效地加载它们。所以,伪代码看起来像这样?

if(value< max_value)
load timer1 = value;
load timer2 = 1;
if(value ==偶数)&& (值< max_value * 2)
load timer1 = value / 2;
load timer2 = 2;
对于不同值的列表:

value | timer1 | timer2
10 | 10 | 1
50 | 50 | 1(max_value)
60 | 30 | 2
80 | 40 | 2
100 | 50 | 2(max_value * 2)
110 | 11 | 10
现在开始有趣了,因为我无法加载超过
50的timer1,我必须将值110拆分,以便timer1具有可能的最大值。更多场景,
120 | 30 | 4
150 | 50 | 3
160 | 40 | 4
我只是看不到实现算法的模式。非常感谢任何帮助。
Hey Guys,

I need an algorithm/formula to do the following:

I have two 32-bit timers cascaded to form a 64-bit timer, max value
per timer(50sec). This is the way they work:

value | timer1 | timer2
5 50 3
when timer1 counts down to zero, timer2 would count down to two.
Timer1 would reload the value 50 and start counting down, this goes on
till timer2 reaches zero.

I need to be able to load them most efficiently. So, pseudo-code looks
like this??

if (value < max_value)
load timer1 = value;
load timer2 = 1;
if (value == even number) && (value < max_value*2)
load timer1 = value/2;
load timer2 = 2;
For a list of different values:

value | timer1 | timer2
10 | 10 | 1
50 | 50 | 1 (max_value)
60 | 30 | 2
80 | 40 | 2
100 | 50 | 2 (max_value*2)
110 | 11 |10
Now the fun starts, since I cannot load timer1 with a value more than
50, I have to split the value 110 so that timer1 would have the
maximum value possible. Some more scenarios,
120 | 30 | 4
150 | 50 | 3
160 | 40 | 4
I just can''t see a pattern to implement an algorithm. Any help will be
greatly appreciated.




这与C有什么关系?


看起来像你想要的模式是

a循环索引从50开始并倒计时

为1.循环的每次传递计算

除了值之外的余数。通过

循环索引。当你发现剩余的

为零时,循环索引是你的timer1值。

商是你的timer2。


在上面的示例中,值= 120将

产生timer1 = 40,timer2 = 3.


但是,由于你的timer1和timer2不能

超过50,理论上最高的值。是
50 * 50。我相信可能会有一些质数小于2500的b $ b将使这种算法失败。


-

--- -------------------------

Jeffrey D. Smith

Farsight Systems Corporation

24 BURLINGTON DRIVE

LONGMONT,CO 80501-6906
http://www.farsight-systems.com

z / Debug调试在IBM z / OS上运行的Systems / C程序!

ISV升级费用是否过高?检查我们的定制产品开发!



What does this have to do with C?

Looks like the pattern you want is to have
a loop index that starts at 50 and counts down
to 1. Each pass of the loop calculates the
remainder from dividing the "value" by the
loop index. When you find a remainder of
zero, the loop index is your timer1 value.
The quotient is your timer2.

In your example above, the value=120 would
yield timer1=40, timer2=3.

However, since your timer1 and timer2 cannot
exceed 50, the theoretical highest "value" is
50*50. I believe there may be some prime numbers
less than 2500 that will fail this algorithm.

--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS!
Are ISV upgrade fees too high? Check our custom product development!


2004年2月23日14:56:44 -0800, ja ***************** @ honeywell.com (周杰伦)

写道:
On 23 Feb 2004 14:56:44 -0800, ja*****************@honeywell.com (Jay)
wrote:
嘿伙计们,

我需要一个算法/公式来做到以下几点:

我有两个32-位定时器级联形成一个64位定时器,每个定时器最大值
(50秒)。这是他们的工作方式:

价值| timer1 | timer2
5 50 3
当timer1倒计时到零时,timer2会倒计时到两个。
Timer1会重新加载值50并开始倒计时,这会持续到timer2达到零。

我需要能够最有效地加载它们。所以,伪代码看起来像这样?

if(value< max_value)
load timer1 = value;
load timer2 = 1;
if(value ==偶数)&& (值< max_value * 2)
load timer1 = value / 2;
load timer2 = 2;
对于不同值的列表:

value | timer1 | timer2
10 | 10 | 1
50 | 50 | 1(max_value)
60 | 30 | 2
80 | 40 | 2
100 | 50 | 2(max_value * 2)
110 | 11 | 10
现在开始有趣了,因为我无法加载超过
50的timer1,我必须将值110拆分,以便timer1具有可能的最大值。更多场景,
120 | 30 | 4
150 | 50 | 3
160 | 40 | 4
我只是看不到实现算法的模式。非常感谢任何帮助。
Hey Guys,

I need an algorithm/formula to do the following:

I have two 32-bit timers cascaded to form a 64-bit timer, max value
per timer(50sec). This is the way they work:

value | timer1 | timer2
5 50 3
when timer1 counts down to zero, timer2 would count down to two.
Timer1 would reload the value 50 and start counting down, this goes on
till timer2 reaches zero.

I need to be able to load them most efficiently. So, pseudo-code looks
like this??

if (value < max_value)
load timer1 = value;
load timer2 = 1;
if (value == even number) && (value < max_value*2)
load timer1 = value/2;
load timer2 = 2;
For a list of different values:

value | timer1 | timer2
10 | 10 | 1
50 | 50 | 1 (max_value)
60 | 30 | 2
80 | 40 | 2
100 | 50 | 2 (max_value*2)
110 | 11 |10
Now the fun starts, since I cannot load timer1 with a value more than
50, I have to split the value 110 so that timer1 would have the
maximum value possible. Some more scenarios,
120 | 30 | 4
150 | 50 | 3
160 | 40 | 4
I just can''t see a pattern to implement an algorithm. Any help will be
greatly appreciated.




将timer1设置为最大值

可能之间是否存在某种相关性。并且最有效地加载两个计时器?如果是这样,我没有得到那个相关性,所以我将忽略最大值

b。 timer1的一部分。随着这个要求的消除,这里的'b
似乎能够为你的价值给定值完成工作

和max_value:


#include< stdio.h>


void setTimers(int value,int max_value,int * timer1p,int * timer2p)

{

* timer1p = value%max_value;

* timer2p = 1 + value /(max_value - 1);

if(* timer1p == 0)/ *可能有更优雅的方式* /

{/ *这样做,但我很懒* /

* timer1p = max_value;

- * timer2p;

}

}


int main(void )

{

int i,timer1,timer2;

int values [] = {10,50,60,80,100,110 ,120,150,160};

const int size = sizeof values / sizeof * values;

const int max_value = 50;


for(i = 0; i< size; i ++)

{

setTimers(values [i],max_value,& timer1,& timer2) ;

printf(" for value =%d(max_value =%d):"

" timer1 =%d,timer2 =%d \ n",

values [i],max_value,timer1,timer2);

}

返回0;

}


运行此计算结果:


表示值= 10(max_value = 50):timer1 = 10,timer2 = 1

表示值= 50(max_value = 50):timer1 = 50,timer2 = 1

for value = 60(max_value = 50):timer1 = 10,timer2 = 2

for value = 80(max_value = 50):timer1 = 30,timer2 = 2

for value = 100(max_value = 50):timer1 = 50,timer2 = 2

for value = 110(max_value = 50):timer1 = 10,timer2 = 3

for value = 120(max_value = 50):timer1 = 20,timer2 = 3

for value = 150(max_value = 50):timer1 = 50,timer2 = 3

for value = 160(max_value = 50):timer1 = 10,timer2 = 4



-leor


Leor Zolman

BD软件
le ** @ bdsoft.com
www.bdsoft。 com - C / C ++,Java,Perl& Cn的现场培训。 Unix

C ++用户:下载BD Software的免费STL错误消息

Decryptor at www.bdsoft.com/tools/stlfilt.html



Is there some correlation between setting timer1 to the "maximum value
possible" and loading both timers "most efficiently"? If so, I don''t get
what that correlation is, so I''m going to ignore the "maximum value
possible" part for timer1. With that requirement eliminated, here''s
something that seems to get the job done for your given values of "value"
and max_value:

#include <stdio.h>

void setTimers(int value, int max_value, int *timer1p, int *timer2p)
{
*timer1p = value % max_value;
*timer2p = 1 + value / (max_value - 1);
if (*timer1p == 0) /* there may be a more elegant way */
{ /* to do this, but I''m being lazy */
*timer1p = max_value;
--*timer2p;
}
}

int main(void)
{
int i, timer1, timer2;
int values[] = {10, 50, 60, 80, 100, 110, 120, 150, 160};
const int size = sizeof values / sizeof *values;
const int max_value = 50;

for (i = 0; i < size; i++)
{
setTimers(values[i], max_value, &timer1, &timer2);
printf("for value = %d (max_value = %d): "
"timer1 = %d, timer2 = %d\n",
values[i], max_value, timer1, timer2);
}
return 0;
}

Running this yields:

for value = 10 (max_value = 50): timer1 = 10, timer2 = 1
for value = 50 (max_value = 50): timer1 = 50, timer2 = 1
for value = 60 (max_value = 50): timer1 = 10, timer2 = 2
for value = 80 (max_value = 50): timer1 = 30, timer2 = 2
for value = 100 (max_value = 50): timer1 = 50, timer2 = 2
for value = 110 (max_value = 50): timer1 = 10, timer2 = 3
for value = 120 (max_value = 50): timer1 = 20, timer2 = 3
for value = 150 (max_value = 50): timer1 = 50, timer2 = 3
for value = 160 (max_value = 50): timer1 = 10, timer2 = 4

That help?
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software''s free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html


Jay写道:
Jay wrote:
嘿伙计们,

我需要一个算法/公式来执行以下操作:

我有两个32位定时器级联形成一个64位定时器,每个定时器最大值
(50秒)。这是他们的工作方式:

价值| timer1 | timer2
5 50 3
当timer1倒计时到零时,timer2会倒计时到两个。
Timer1会重新加载值50并开始倒计时,这会持续到timer2达到零。

我需要能够最有效地加载它们。所以,伪代码看起来像这样?

if(value< max_value)
load timer1 = value;
load timer2 = 1;
if(value ==偶数)&& (值< max_value * 2)
load timer1 = value / 2;
load timer2 = 2;
对于不同值的列表:

value | timer1 | timer2
10 | 10 | 1
50 | 50 | 1(max_value)
60 | 30 | 2
80 | 40 | 2
100 | 50 | 2(max_value * 2)
110 | 11 | 10
现在开始有趣了,因为我无法加载超过
50的timer1,我必须将值110拆分,以便timer1具有可能的最大值。更多场景,
120 | 30 | 4
150 | 50 | 3
160 | 40 | 4
我只是看不到实现算法的模式。非常感谢任何帮助。
Hey Guys,

I need an algorithm/formula to do the following:

I have two 32-bit timers cascaded to form a 64-bit timer, max value
per timer(50sec). This is the way they work:

value | timer1 | timer2
5 50 3
when timer1 counts down to zero, timer2 would count down to two.
Timer1 would reload the value 50 and start counting down, this goes on
till timer2 reaches zero.

I need to be able to load them most efficiently. So, pseudo-code looks
like this??

if (value < max_value)
load timer1 = value;
load timer2 = 1;
if (value == even number) && (value < max_value*2)
load timer1 = value/2;
load timer2 = 2;
For a list of different values:

value | timer1 | timer2
10 | 10 | 1
50 | 50 | 1 (max_value)
60 | 30 | 2
80 | 40 | 2
100 | 50 | 2 (max_value*2)
110 | 11 |10
Now the fun starts, since I cannot load timer1 with a value more than
50, I have to split the value 110 so that timer1 would have the
maximum value possible. Some more scenarios,
120 | 30 | 4
150 | 50 | 3
160 | 40 | 4
I just can''t see a pattern to implement an algorithm. Any help will be
greatly appreciated.




我不确定我是否理解正确,但如果我这样做,请注意你的两个

计数器在一起不能计算大于50的素数或者如果

素数因子不能分成两个产品< = 50(因为它们只能计算
计数一些素数的倍数< = 50)。一种方法是将
将你的输入值计入素数因子,然后将它们逐个乘以
直到你达到最接近50的值。这将是timer1

的值和剩余因子的乘积是timer2。这里的代码是

演示了这一点(从1..50硬编码了素数)。代码'

的排列使得value1获得最大可能值,value2是

其余因子相乘。如果一个数字不能代表

作为value1 * value2那么它返回-1。根据您的输入限制

(只有偶数等),您应该能够进一步优化:


#include< stdio.h>


#define MAX_VALUE1 50

#define MAX_VALUE2 50

静态短线素数[] =

{2,3,5,7,11,13,17,19,23,29,31,

37,41,43,47,};


int split_value(int value,int * value1,int * value2,int * factors){

int nfacts = 0;

int temp = value ;

int i;


if(temp == 1){

* value1 = 1;

* value2 = 1;

返回0;

}


i = 0;

while(i< sizeof primes / sizeof primes [0]&

temp!= 1){

while(temp!= 1&& temp%primes [i] == 0){

因子[nfacts ++] = primes [i];

temp / = primes [i];

}

++我;

}


if(temp!= 1)

返回-1; / *此值无法表示
两个计数器中的
< = 50 * /

* value1 = 1,* value2 = 1;

i = 0;


for(i = nfacts - 1; i> = 0; - i){

if(* value1 * factors [i ]< = MAX_VALUE1){

* value1 * = factors [i];

因子[i] = -1;

} < br $>
}


for(i = 0; i< nfacts; ++ i)

if(factors [i]> ; 1)

* value2 * =因素[i];


如果(* value2> MAX_VALUE1)

返回 - 1; / *此值无法表示

两个计数器< = 50 * /


返回0;

}


int main(无效){

int i,j;

int v1,v2;

int因子[sizeof primes / sizeof primes [0]];


printf(" \tValue \t | \tTimer1 \t | \tTimer2 \ n" );

for(i = 1; i< = 2500; ++ i){

#ifdef PRINT_ALL

printf(" \t%5d \t",i);

#endif

for(j = 0; j< sizeof factors / sizeof factors [0]; + + j)

因子[j] = -1;

if(split_value(i,& v1,& v2,factors)== 0){

#ifndef PRINT_ALL

printf(" \t%5d \t",i);

#endif

printf(" | \t%6d \ t | \t%6d \ n",v1,v2);

}

#ifdef PRINT_ALL

else {

printf(" Not Possible {");

for(j = 0; factors [j]!= -1; ++ j)

printf("%d,",factors [j]);

printf(" } \ n");

}

#endif

}


返回0;

}


-nrk。


-

删除电子邮件的devnull



I am not sure if I understood it correctly, but if I did, note that your two
counters together cannot count prime numbers greater than 50 or if the
prime factors cannot be broken into two products <= 50 (Since they can only
count a multiple of some prime number <= 50). One way to go about it is to
factor your input value into prime factors, then multiply them one by one
till you hit the value closest to 50. That would be the value of timer1
and the product of the remaining factors would be timer2. Here''s code that
demonstrates this (having hard-coded the primes from 1..50). The code''s
arranged so that value1 gets the maximum possible value and value2 is the
rest of the factors multiplied together. If a number cannot be represented
as value1 * value2 then it returns -1. Based on your input constraints
(only even numbers etc.) you should be able to optimize this further:

#include <stdio.h>

#define MAX_VALUE1 50
#define MAX_VALUE2 50

static short primes[] =
{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, };

int split_value(int value, int *value1, int *value2, int *factors) {
int nfacts = 0;
int temp = value;
int i;

if ( temp == 1 ) {
*value1 = 1;
*value2 = 1;
return 0;
}

i = 0;
while ( i < sizeof primes/sizeof primes[0] &&
temp != 1 ) {
while ( temp != 1 && temp % primes[i] == 0 ) {
factors[nfacts++] = primes[i];
temp /= primes[i];
}
++i;
}

if ( temp != 1 )
return -1; /* This value cannot be represented
in two counters <= 50 */
*value1 = 1, *value2 = 1;
i = 0;

for ( i = nfacts - 1; i >= 0; --i ) {
if ( *value1 * factors[i] <= MAX_VALUE1 ) {
*value1 *= factors[i];
factors[i] = -1;
}
}

for ( i = 0; i < nfacts; ++i )
if ( factors[i] > 1 )
*value2 *= factors[i];

if ( *value2 > MAX_VALUE1 )
return -1; /* This value cannot be represented
in two counters <= 50 */

return 0;
}

int main(void) {
int i, j;
int v1, v2;
int factors[sizeof primes/sizeof primes[0]];

printf("\tValue\t|\tTimer1\t|\tTimer2\n");
for ( i = 1; i <= 2500; ++i ) {
#ifdef PRINT_ALL
printf("\t%5d\t", i);
#endif
for ( j = 0; j < sizeof factors/sizeof factors[0]; ++j )
factors[j] = -1;
if ( split_value(i, &v1, &v2, factors) == 0 ) {
#ifndef PRINT_ALL
printf("\t%5d\t", i);
#endif
printf("|\t%6d\t|\t%6d\n", v1, v2);
}
#ifdef PRINT_ALL
else {
printf("Not Possible { ");
for ( j = 0; factors[j] != -1; ++j )
printf("%d, ", factors[j]);
printf(" }\n");
}
#endif
}

return 0;
}

-nrk.

--
Remove devnull for email


这篇关于算法将数字分成两部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆