leetcode:504. Base 7

xiaoxiao2021-02-28  127

原题:

Given an integer, return its base 7 string representation.

Example 1:

Input: 100 Output: "202"

Example 2:

Input: -7 Output: "-10"

Note: The input will be in range of [-1e7, 1e7].

代码:

char* convertToBase7(int num) { int test = abs(num); char* result; result=(char*)malloc(sizeof(char)*20); *(result+19)='\0'; int n; for(n=18;test!=0;n--) { *(result+n)=(char)(test%7+48); test=test/7; } if(num==0) { *(result+n)='0'; return result+n; } if(num<0) { *(result+n)='-'; return result+n; } return result+n+1; }

转载请注明原文地址: https://www.6miu.com/read-50756.html

最新回复(0)