怎么写这道C语言的程序题目.A*B求a* b % m;Input题目包含多组测试数据,每行3个非负整数a ,b ,m,(a ,b不超过1000位,m < 10000),输入一组0结束.Output输出a * b % m的值;Sample Input1 2 30 0 0Sample Output2
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/19 21:14:55
怎么写这道C语言的程序题目.A*B求a* b % m;Input题目包含多组测试数据,每行3个非负整数a ,b ,m,(a ,b不超过1000位,m < 10000),输入一组0结束.Output输出a * b % m的值;Sample Input1 2 30 0 0Sample Output2
怎么写这道C语言的程序题目.
A*B
求a* b % m;
Input
题目包含多组测试数据,每行3个非负整数a ,b ,m,(a ,b不超过1000位,m < 10000),输入一组0结束.
Output
输出a * b % m的值;
Sample Input
1 2 3
0 0 0
Sample Output
2
怎么写这道C语言的程序题目.A*B求a* b % m;Input题目包含多组测试数据,每行3个非负整数a ,b ,m,(a ,b不超过1000位,m < 10000),输入一组0结束.Output输出a * b % m的值;Sample Input1 2 30 0 0Sample Output2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//
typedef unsigned int uint;
typedef unsigned short ushort;
static const uint kMaxCount = 256;
//
typedef struct st_ret {
uint ret[kMaxCount];
ushort count;
st_ret* next;
} st_ret;
//
void init_st_ret(st_ret* st)
{
st->count = 0;
st->next = 0;
}
//
st_ret* create_st_ret()
{
#ifdef __cplusplus
st_ret* p = (st_ret*)malloc(sizeof(st_ret));
#else
st_ret* p = malloc(sizeof(st_ret));
#endif
init_st_ret(p);
return p;
}
//
st_ret* fill_st_ret(st_ret* st, uint ret_value)
{
st_ret* p = st;
if (st->count >= kMaxCount)
p = st->next = create_st_ret();
p->ret[p->count++] = ret_value;
return p;
}
//
void free_st_ret(st_ret* st_head)
{
st_ret* p = st_head;
while (p)
{
st_ret* q = p;
p = p->next;
free(q);
}
}
//
void print_st_ret(st_ret* st)
{
uint i = 0;
for(; i < st->count; ++i)
printf("%u\n", st->ret[i]);
}
//
void print_st_rets(st_ret* st_head)
{
printf("\n");
st_ret* p = st_head;
while (p)
{
print_st_ret(p);
p = p->next;
}
}
//
static const int kMaxNumberLength = 1000;
int trim_big_number(const char* big_number, uint value_upper)
{
int len = strlen(big_number);
if (EOF == len || kMaxNumberLength < len)
return 0;
int digit_num = 0;
for (uint i = value_upper; i > 0; ++digit_num, i = i/10) ;
if (digit_num >= len)
return strtoul(big_number, NULL, 10);
else
{
const char* trim_number = big_number + len - digit_num;
return strtoul(trim_number, NULL, 10);
}
}
//
int main(int argc, char** argv)
{
st_ret* p = create_st_ret();
st_ret* q = p;
char szInputString[1024] = { 0 };
while (true)
{
int a, b, m;
scanf("%s", szInputString);
a = trim_big_number(szInputString, 10000);
scanf("%s", szInputString);
b = trim_big_number(szInputString, 10000);
scanf("%d", &m);
if (a == 0 && b == 0 && m == 0)
break;
if (m == 0)
q = fill_st_ret(q, 0);
else
q = fill_st_ret(q, a * b % m);
}
print_st_rets(p);
free_st_ret(p);
printf("\n");
system("pause");
return 0;
}
之前看错题目,以为a, b不大于1000.修改.