我在查找具有最大偶数的元素时遇到问题 在一组数组中。我写的代码只带正数 考虑。所有负值都被完全忽略。
int main() {
int ary1[] = { 123, 456, -7890, 12 };
int ary2[] = {-123, 654, 78, 15, 189};
int ary3[] = {9, 9};
int ary4[] = {123, 123, 0, 12};
int ary5[] = {24, 45, -789, 24, 1};
int ary6[] = {-749, -241, 1, 45};
int ary7[] = {1, 3, 5};
digitExtract(ary1, 4);
}
void digitExtract(int Array[], int array_size) {
int max = Array[0];
for (int i = 1; i < array_size; i++)
{
if (Array[i] > max)
max = Array[i];
}
printf("\nThe largest element is: %d\n", max);
}
我有一个基本的想法,就是遍历每个元素 并将其转换为正数(获取绝对值以便对其进行处理),但我不确定如何处理 实现代码。
任何提示或想法?
答案 0 :(得分:0)
我认为这段代码应该可以满足您的需求。
#include <stdio.h>
void digitExtract(int Array[], int array_size);
int main() {
int ary1[] = { 123, 456, -7890, 12 };
int ary2[] = { -123, 654, 78, 15, 189 };
int ary3[] = { 9, 9 };
int ary4[] = { 123, 123, 0, 12 };
int ary5[] = { 24, 45, -789, 24, 1 };
int ary6[] = { -749, -241, 1, 45 };
int ary7[] = { 1, 3, 5 };
digitExtract(ary1, 4);
digitExtract(ary2, 5);
digitExtract(ary3, 2);
digitExtract(ary4, 4);
digitExtract(ary5, 5);
digitExtract(ary6, 4);
digitExtract(ary7, 3);
}
void digitExtract(int Array[], int array_size) {
int maxDigitNumber;
int maxDigitFound = -1;
int tmp;
for (int i = 0; i < array_size; i++) {
tmp = Array[i] < 0 ? -Array[i]: Array[i];
do {
if (tmp % 2 == 0 && maxDigitFound < (tmp % 10)) {
maxDigitFound = tmp % 10;
maxDigitNumber = Array[i];
}
tmp /= 10;
} while (tmp > 0)
}
if (maxDigitFound > -1)
printf("\nThe largest digit is: %d and the element is: %d\n", maxDigitFound, maxDigitNumber);
else
printf("\nThere is not an even number in the array\n");
}
希望这有帮助
答案 1 :(得分:-1)
如果您要求如何将整数数组中的所有值转换为正值,则可以通过循环执行此操作:
void digitExtract(int Array[], int array_size) {
for(int i = 0; i < array_size; i++)
Array[i] = abs(Array[i]);
int max = Array[0];
for (int i = 1; i < array_size; i++)
{
if (Array[i] > max)
max = Array[i];
}
printf("\nThe largest element is: %d\n", max);
}
实际上,您不需要这样做,因为您可以比较绝对值,而不是在比较之前将所有内容转换为正值。这也有效:
void digitExtract(int Array[], int array_size) {
int max = abs(Array[0]);
for (int i = 1; i < array_size; i++)
{
if (abs(Array[i]) > max)
max = Array[i];
}
printf("\nThe largest element is: %d\n", max);
}
无论哪种方式,您都需要
#include <stdlib.h>`
其中abs(int n)是。
答案 2 :(得分:-1)
这可能会有所帮助......
我使用数字0-9的ASCII值进行比较
数字0到9的ASCII值从48到57
先转换每个数字,例如456到一个字符串,即&#34; 456&#34;。
然后一次考虑1个字符,即&#39; 4&#39;,然后&#39; 5&#39;,然后&#39; 6&#39;。
所以我们比较&#39; 4&#39;的ASCII值。对于&#39; 5&#39;
我们检查以确保ASCII值是偶数&#39;号。
如果它甚至我们认为它是为了比较,否则我们不这样做。
如果2个元素具有相同的偶数,例如,应该怎么办? -749&amp; -241?他们都有4.
对于只有奇数的数组应该怎么办? ary3 []&amp; ary7 []?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void myFunc(int [], int);
int main()
{
int ary1[] = { 123, 456, -7890, 12 };
int ary2[] = { -123, 654, 78, 15, 189 };
int ary3[] = { 9, 9 };
int ary4[] = { 123, 123, 0, 12 };
int ary5[] = { 24, 45, -789, 24, 1 };
int ary6[] = { -749, -241, 1, 45 };
int ary7[] = { 1, 3, 5 };
myFunc(ary1, 4);
myFunc(ary2, 5);
myFunc(ary3, 2);
myFunc(ary4, 4);
myFunc(ary5, 5);
myFunc(ary6, 4);
myFunc(ary7, 3);
return 0;
}
void myFunc(int Array[], int size)
{
int iLen = 0, myIndx = 0;
int iMax = 0, iOverallMax = 0;
char myArray[50] = "";
int i, j = 0;
for(i=0; i<size; i++)
{
sprintf(myArray, "%d", abs(Array[i])); // Convert int to string.
iLen = strlen(myArray); // Get length e.g. len of "456" = 3
if((myArray[j] % 2) == 0) // We are only interested in even digits ...
iMax = myArray[j]; // Set iMax = ASCII value of myArray[0] i.e. ASCII value of '4'
else
iMax = 0;
j++;
while(j < iLen) // Have to loop through digits in "456".
{
// We are only interested in even digits per element ...
if((myArray[j] > iMax) && ((myArray[j] % 2) == 0))
iMax = myArray[j]; // Assigns ASCII value of myArray[j] e.g. '6' to iMax.
j++;
}
// Save the Array Index of the element with the max even digit found so far.
if(iMax > iOverallMax)
{
iOverallMax = iMax; // Save the ASCII value of the max even digit so far.
myIndx = i; // Save the Array Index.
}
// Reset variables ... and repeat the process for the next element in the Array.
j = 0;
strcpy(myArray, "");
}
printf("\nThe Array element with the largest even digit is: %d\n", Array[myIndx]);
}