恶心的c语言作业 大虾帮忙

来源:百度知道 编辑:UC知道 时间:2024/09/22 18:40:53
Function f(n) is recursively defined as:

f(n) = f(n-1) + f(n-3), n > 3
f(n) = n, n <= 3
Write a program to calculate f(n) modulo m.

n <= 10000, 2 <= m <= 10000.

Input

There are multiple test cases. Each test case consists of two integers: n and m. n = 0 and m = 0 denotes the end of input, and you should not process this case.

Output

For each test case, print f(n) modulo m in a single line.

Sample Input

1 2
10000 999
0 0
Sample Output

1
433

用递归函数就行了。
程序代码如下:

#include "stdio.h"

int t,result;

int f(int n) reentrant //这里的reentrant是可重入函数表示,有了这个标记后,在你的C编译器下可能通不过(我这里是Keil_C编译器),如果你那里编译通不过的话,把reentrant去掉
{
int m;
if(n<=3)
m=n;
else
{
m=f(n-3);
}
return(m);
}

void main(void) //主函数名
{
do
{
scanf("%d\n",&t);
if(t==0)
break;
else
{
result=f(t); //计算结果
printf("%d\n",result); //打印结果
}
}while(t!=0);
getchar();
exit();
}

自己不做,拿网上让你帮你做,你更恶心

这不是一道完整的题,请问你全部的问题是什么?

先翻译一下再说吧