题目很简单,完成函数reverse,要求实现把给定的一个整数取其相反数的功能,举两个例子如下: x = 123, return 321 x = -123, return -321
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_LEN 32
int reverse(int x) {
//write your code here
int i = 0;
int j = 0;
int iNum = x;
static char strNum[MAX_STR_LEN] = "";
if ( iNum <0 )
{
strNum[i] = '-';
i++;
iNum = -iNum;
}
do
{
strNum[i++] = iNum % 10 + '0';
iNum = iNum / 10;
}while(iNum > 0);
printf("strNum = %s\n", strNum);
return atoi(strNum);
}
//start 提示:自动阅卷起始唯一标识,请勿删除或增加。
int main()
{
int i = 0;
int iNum[10] = {123, 456,789,741, 852,-963,-951,-623,-847, -95123};
int iN = 0;
//printf("put a int number :\n");
//scanf("%d", &iNumber);
for (i = 0; i < 10; i++ )
{
printf("iNum[%d]: %d\n", i, iNum[i]);
iN = reverse(iNum[i]);
printf("reverse %d\n", iN);
}
return 0;
}
//end