【例题1】面试题 16.07. 最大数值 - 力扣(LeetCode)
int maximum(int a, int b){
return a>b?a:b;
}
【例题2】2413. 最小偶倍数 - 力扣(LeetCode)
int smallestEvenMultiple(int n){
int a = n>2?n:2;
int b = n>2?2:n;
int mul = a * b;
while(b!=0){
if(a%b==0){
a = b;
break;
}else{
int temp = a;
a = b;
b = temp % b;
}
}
return mul/a;
}
【例题3】1486. 数组异或操作 - 力扣(LeetCode)
int xorOperation(int n, int start){
n--;
int Xor = 0;
while(n >= 0){
Xor^= start+2*(n--);
}
return Xor;
}
【例题4】1572. 矩阵对角线元素的和 - 力扣(LeetCode)
int diagonalSum(int** mat, int matSize, int* matColSize){
int i,sum=0;
for(i=0;i<matSize;i++){
sum+=mat[i][i]+mat[i][matSize-1-i];
}
if (matSize%2==0){
return sum;
}else{
return sum-mat[(matSize-1)/2][(matSize-1)/2];
}
}
【例题5】504. 七进制数 - 力扣(LeetCode)
char * convertToBase7(int num){
if (num == 0){
return "0";
}
char *str=(char*)malloc(sizeof(char)*32);
int arr[100] = {0};
int i=0,j=0,k=0,flag = 0;
if (num<0){
flag = 1;
num = num*(-1);
str[k++] = '-';
}
while(num>0){
arr[i++] = num % 7;
num /= 7;
}
for(j = i-1;j>=0;j--){
str[k++] = '0' + arr[j];
}
str[k]='\0';
return str;
}