如何将数组中的两位数转换为一个整数?
示例:1245678933658到[12,45,67,89,33,65,80]如果它是奇数,则将0加到最后。
我的尝试是:
new_array[i]=digits[i]*10+digits[i++]*10/10;
答案 0 :(得分:0)
是否必须是"单行",还是允许更可读的代码?请尝试以下方法:
const char* digits = "12456789336581";
int digitIndex=0;
int numbers[100];
int currentNumberIndex = 0;
int currentNumber = 0;
while (digits[digitIndex]) {
currentNumber *=10;
currentNumber += digits[digitIndex] - '0';
digitIndex++;
if ( (digitIndex)%2 == 0) // two digits handled?
{
numbers[currentNumberIndex++] = currentNumber;
currentNumber = 0;
}
}
// handle the case that last number contained just one digit:
if (digitIndex%2) {
currentNumber *=10;
numbers[currentNumberIndex++] = currentNumber;
}
for (int i=0; i < currentNumberIndex; i++)
printf("number[%d]: %d\n", i, numbers[i]);