1:
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
unsigned n, i;
double * x, sum, avg, out, test;
scanf("%u", &n);
x = (double *)calloc(n, sizeof(double));
for(i = 0; i < n; i++) {
scanf("%lf", x + i);
sum += x[i];
}
avg = sum / n;
out = x[0];
for(i = 1; i < n; i++)
if(abs(out - avg) < abs(x[i] - avg))
out = x[i];
printf("%.2f\n", out);
return 0;
}
2:
程序代码:
void swap(char * p1, char * p2)
{
char tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
char * reverse(char * buf, int n)
{
int i, j;
for(i = 0, j = n - 1; i < j; i++, j--)
swap(buf + i, buf + j);
return buf;
}
3:
程序代码:
#include <stdio.h>
#include <math.h>
double convert(double, unsigned);
int main(void)
{
double dTestNo;
unsigned iBase;
scanf("%lf", &dTestNo);
dTestNo -= floor(dTestNo);
for(iBase = 2; iBase < 10; iBase++)
printf("十进制正小数 %lf 转换成 %u 进制数为:%.8lf\n", dTestNo, iBase, convert(dTestNo, iBase));
return 0;
}
double convert(double dTestNo, unsigned iBase)
{
double num = 0, tmp1 = dTestNo, tmp2;
unsigned i, j;
for(i = 0, j = 10; i < 8; i++, j *= 10) {
tmp2 = floor(tmp1 *= iBase);
num += tmp2 / j;
tmp1 -= tmp2;
}
return num;
}
4:
程序代码:
#include <stdlib.h>
#include <string.h>
char * move(char * szBuf, int iLen, int n) {
int i, j;
char * szTmp = (char *)calloc(iLen, sizeof(char));
for(i = 0; i < n; i++) {
for(j = 1; j < iLen; j++)
szTmp[j] = szBuf[j - 1];
szTmp[0] = szBuf[iLen - 1];
memcpy(szBuf, szTmp, iLen);
}
free(szTmp);
return szBuf;
}
5:
程序代码:
#include <stdio.h>
#include <string.h>
int prize(const char *, const char *, int);
int main(void)
{
char * a = "12345678";
char * b = "42347856";
printf("%d\n", prize(a, b, 8));
return 0;
}
int prize(const char * a, const char * b, int iLen)
{
int i, a1, a2, b1, b2, count, maxCount = 0;
for(i = 1; i < iLen * 2; i++) {
if(iLen - i >= 0) {
a1 = iLen - i;
a2 = iLen;
b1 = 0;
b2 = i;
} else {
a1 = 0;
a2 = iLen - i + iLen;
b1 = i - iLen;
b2 = iLen;
}
count = 0;
while(a1 < a2 && b1 < b2) {
if(a[a1] == b[b1])
count++;
else {
if(maxCount < count)
maxCount = count;
count = 0;
}
a1++; b1++;
}
}
return maxCount;
}
最大遗憾是我突然发现此贴没分。