我们有两个由字符a-z组成的无限重复消息。每个角色需要不同的时间单位进行传输; a = 1,b = 2,c = 4,d = 8,e = 16 ......,字符|告诉我们当前在消息中的位置。我们的工作是说同步这两条消息需要多少时间。同步意味着两个消息同时开始。
示例:
我们有消息1:ea | babab
和消息2:d | abaca
因此,我们知道消息1是bababea,消息2是:abacad。消息将以42个单位时间同步:
EA | bababea | = 42
d | abacad | abacad | = 42
示例2: message1:| acabbaaa消息2:| dcbabaaaa。
解决方案:0,因为它们已经同步。
我们想要一个算法来计算第一次同步发生的时间。
我在C中写这个。除了算法本身,我基本上完成了所有工作。
我认为这可以使用扩展的欧几里德算法来完成。
答案 0 :(得分:1)
我已回答了不同的question,我认为解决这个问题的方法完全相同。你需要求解方程 m1Offset +(m1Len * intN1)= m2Offset +(m2Len * intN2)
#include <stdio.h>
void findVal(unsigned int m1Offset, unsigned int m1Len, unsigned int m2Offset, unsigned int m2Len) ;
unsigned int getGCD(unsigned int n1, unsigned int n2);
int main()
{
findVal(17, 25, 8, 17);
return 0;
}
void findVal(unsigned int m1Offset, unsigned int m1Len, unsigned int m2Offset, unsigned int m2Len) {
unsigned int n1 = 0;
unsigned int n2 = 0;
unsigned char foundVal = 1;
unsigned int m1Time = m1Offset;
unsigned int m2Time = m2Offset;
//No need to find n1 and n2 if msgs are starting from beginning.
if(((m1Offset == m1Len) && (m2Offset == m2Len)) || ((0 == m1Offset) && (0 == m2Offset)))
{
m1Time = 0;
m2Time = 0;
}
//No need to find n1 and n2 if offset times are same.
else if(m1Offset != m2Offset)
{
//Offset times are not same.
foundVal = 0;
//Find GCD of m1Len and m2Len.
unsigned int gcd = getGCD(m1Len, m2Len);
//There is a solution only if the difference of offsets is divisible by gcd.
if(0 == (m2Offset-m1Offset) % gcd)
{
for(n2=1; n2<(unsigned int)-1; n2++)
{
unsigned int temp1 = (m2Len*n2)+(m2Offset-m1Offset);
if(0 == temp1 % m1Len)
{
n1 = temp1/m1Len;
m1Time = m1Offset + n1*m1Len;
m2Time = m2Offset + n2*m2Len;
foundVal = 1;
break;
}
}
}
}
if(1 == foundVal)
{
printf("Found n1[%u] n2[%u] m1Time[%u] m2Time[%u]\n", n1, n2, m1Time, m2Time);
}
else
{
printf("Could not find n1, n2, m1Time, m2Time\n");
}
}
unsigned int getGCD(unsigned int n1, unsigned int n2)
{
while(n1!=n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
printf("GCD = %u\n",n1);
return n1;
}
我们需要找到满足上述等式的intN1和intN2。 当且仅当m1Len和m2Len的GCD除以(m2Offset - m1Offset)时才会有解决方案。
在下面的代码中,
m1Offset和m2Offset:初始偏移量。例如:在消息&#34; ea | babab&#34;,&#34; ea&#34;是偏移量,它等于17.类似地在&#34; d | abaca&#34;,&#34; d&#34;是偏移量,等于8.
m1Time和m2Time应该相等,这是第一个同步时间。
这是我的代码。
Found n1[1] n2[2] m1Time[42] m2Time[42]
输出:
function emailStatusReport() {
// Set variables.
var x = 13;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetName = ss.getSheetByName('data');
var template = HtmlService.createTemplateFromFile("report.html");
var message;
var subject;
var emailQuotaRemaining = MailApp.getRemainingDailyQuota();
var theRange = ss.getDataRange();
var completeMessage = "";
//Logger.log(theRang.getA1Notation());
var theManualRange = ss.getRange("A1:C7");
// var dateTimeStamp = now();
var vals = theManualRange.getValues();
for(i=0; i<vals.length; i++){
var itemNumber = vals[i][0];
var title = vals[i][1];
var description = vals[i][2];
// Logger.log(itemNumber + " " + title + " " + description + " ");
completeMessage = completeMessage + ("<BR>" + itemNumber + " " + title + " " + description + "</BR>");
}
Logger.log(completeMessage);
// Logger.log (x);
// Logger.log (ss);
// Logger.log (sheetName);
// Logger.log ("Remaining email quota: " + emailQuotaRemaining);
// Logger.log (vals[i][0]);
subject = "Daily Status Report TEST..."
message = "<HTML><BODY>" +
"<Font Face='verdana' color='green' size='2'>" +
"<P><P><B>Daily Status Report TEST</B>" +
"<P>Please review the report listed below. " +
// "<P>The value of x is " + x + "." +
"<P>" + completeMessage +
"<P><P>" +
"For the most up-to-date information, please visit: <BR>" +
"https://docs.google.com/spreadsheets/d/1jA4XQa7MgQI7OOLwCcmGv7BcUKSjdIPyh6B0CeQI-PI/edit#gid=0" +
"<P>" +
"<P><B>Make it an amazing day!</B>" +
// "<P>Report generated at: " + dateTimeStamp + ". " +
"<P>Remaining daily email quota: " + emailQuotaRemaining + ". " +
// "<P>This report runs between ___ AM and ___ AM." +
"</HTML></BODY>";
MailApp.sendEmail("greg@gregbartonmba.com", subject, "", {htmlBody: message});
// Logger.log("Subject is :" + subject);
// Logger.log("Message is: " + message);
}