曲径通幽论坛
标题:
百度知道中的题目一
[打印本页]
作者:
beyes
时间:
2008-11-16 22:42
标题:
百度知道中的题目一
如输入一个数字串:100 200 400 100 500
然后反向输出: 500 100 400 200 100
最后求头两个和尾两个数的平均值。
我的解法:(编译环境为linux gcc,gcc 编译带有数学函数库时,需加参数 -lm )
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAXSIZE 100
#define NUM 10
char
string[MAXSIZE];
int
len;
int
main
()
{
char
*
ptr;
int
temp
=
0
;
int
i
=
0
;
int
k
=
0
;
int
num[NUM];
printf(
"Please enter: "
);
if
( fgets(string, MAXSIZE, stdin)
==
NULL
){
printf(
"Read error.
\n
"
);
exit(
1
);
}
len
=
strlen(string);
ptr
=
&
string[len
-
2
];
len
=
len
-
2
;
while
( len
>=
0
)
{
while
(
*
ptr
!=
' '
&&
len
>=
0
)
{
if
( i
==
0
){
temp
=
*
ptr
-
48
;
i
=
1
;
ptr
--
;
len
--
;
continue
;
}
else
{
temp
+=
(
*
ptr
-
48
)
*
(
int
)pow(
10
,i);
ptr
--
;
i
++
;
len
--
;
continue
;
}
}
ptr
--
;
i
=
0
;
//reset i for next number
num[k]
=
temp;
k
++
;
printf(
"%d "
, temp);
temp
=
0
;
len
--
;
}
printf(
"
\n
"
);
printf(
"avg_1 = %d
\n
"
,(num[
0
]
+
num[
1
])
/
2
);
printf(
"avg_2 = %d
\n
"
,(num[k
-
1
]
+
num[k
-
2
])
/
2
);
return
0
;
}
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/)
Powered by Discuz! X3.2