算法求解(数学建模的问题)

最帅马老师 最帅马老师 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
刚看到有兄弟提问,没想出下文就结贴了,现重新开一贴征求算法:

原题:

有一条长阶梯,若每步跨2阶,则最最后剩一阶,若每步跨3 阶,则最后剩2阶,若每步跨5阶,则最后剩4阶,若每步跨6阶则最后剩5阶。只有每次跨7阶,最后才正好一阶不剩。请问这条阶梯共有多少阶?
C语言,使用如下程序:


#include <stdio.h>
int
main()
{
int i=0;

while (i+=7)
{
if ( (i-2)%3!=0 ) continue;
if ( (i-4)%5!=0 ) continue;
if ( (i-5)%6!=0 ) continue;

printf("the anwser is: %d\n", i);
break;
}

return 0;
}


输出为119。
那么,119是不是唯一的答案?不是,将程序更改为如下:

#include <stdio.h>

int
main()
{
int i=0;

while (i+=7)
{
if (i>10000)
{
break;
}

if ( (i-2)%3!=0 ) continue;
if ( (i-4)%5!=0 ) continue;
if ( (i-5)%6!=0 ) continue;

printf("the anwser is: %d\n", i);
}

return 0;
}

输出为:
the anwser is: 119
the anwser is: 329
the anwser is: 539
the anwser is: 749
the anwser is: 959
the anwser is: 1169
the anwser is: 1379
the anwser is: 1589

从这些输出可以观察到,从119开始,每个符合条件的答案之间相差210。
但为什么会相差210?第一个数为119,可不可以推算出来,而不用遍历?
如果用函数f(x)表示,则此函数有f(x+T)=f(x),周期T为210,这个函数如何建模?
...全文

相关推荐

评论 22
收藏 已收藏
分享
返回顶部
最帅马老师
C语言 70,039 成员 243,237 内容