谢谢 试编写算法,计算i!*2i的值并存入数组a
答案:2 悬赏:70 手机版
解决时间 2021-03-23 06:34
- 提问者网友:浪荡绅士
- 2021-03-22 20:11
谢谢 试编写算法,计算i!*2i的值并存入数组a
最佳答案
- 五星知识达人网友:走死在岁月里
- 2021-03-22 21:03
不用那么复杂。我的程序:
#include
#define arrsize 10
#define maxint 10000
main () {
int i, j;
int result;
int tmp;
int a[arrsize];
tmp = 1;
for (i=1; i<=arrsize; i++) {
tmp *= i;
result = tmp * 2 * i;
if (result >= maxint)
break;
else
a[i-1] = result;
}
for (j=0; j printf ("a[%2d] = %5d\n", j, a[j]);
getch();
}
#include
#define arrsize 10
#define maxint 10000
main () {
int i, j;
int result;
int tmp;
int a[arrsize];
tmp = 1;
for (i=1; i<=arrsize; i++) {
tmp *= i;
result = tmp * 2 * i;
if (result >= maxint)
break;
else
a[i-1] = result;
}
for (j=0; j
getch();
}
全部回答
- 1楼网友:骨子里都是戏
- 2021-03-22 22:36
#include
unsigned int _maxint = -1;
#define _N 10000
unsigned int a[_N];
int factorial(unsigned int n, unsigned int *r) {
unsigned int i;
unsigned int critical;
if ( !r ) {
return -1;
}
*r = 1;
for ( i = n;i > 1;i-- ) {
critical = _maxint / i;
if ( *r > critical ) {
return -1;
}
*r *= i;
}
return 0;
}
int arithmetic(unsigned int i, unsigned int *r) {
unsigned int critical;
if ( 0 != factorial(i, r) ) {
return -1;
}
critical = _maxint / *r;
if ( 2 * i > critical ) {
return -1;
}
*r *= 2 * i;
return 0;
}
int main() {
unsigned int i, j, r;
for ( i = 1;i < _N;i++ ) {
if ( 0 == arithmetic(i, &r) ) {
a[i - 1] = r;
}
else {
break;
}
}
// for dump
for ( j = 0;j < i - 1;j++ ) {
printf("%03d: %lu\n", j + 1, a[j]);
}
return 0;
}
unsigned int _maxint = -1;
#define _N 10000
unsigned int a[_N];
int factorial(unsigned int n, unsigned int *r) {
unsigned int i;
unsigned int critical;
if ( !r ) {
return -1;
}
*r = 1;
for ( i = n;i > 1;i-- ) {
critical = _maxint / i;
if ( *r > critical ) {
return -1;
}
*r *= i;
}
return 0;
}
int arithmetic(unsigned int i, unsigned int *r) {
unsigned int critical;
if ( 0 != factorial(i, r) ) {
return -1;
}
critical = _maxint / *r;
if ( 2 * i > critical ) {
return -1;
}
*r *= 2 * i;
return 0;
}
int main() {
unsigned int i, j, r;
for ( i = 1;i < _N;i++ ) {
if ( 0 == arithmetic(i, &r) ) {
a[i - 1] = r;
}
else {
break;
}
}
// for dump
for ( j = 0;j < i - 1;j++ ) {
printf("%03d: %lu\n", j + 1, a[j]);
}
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯