gpt4 book ai didi

c++ - 获取数组中以 0 结尾的最大元素

转载 作者:行者123 更新时间:2023-11-28 06:01:12 25 4
gpt4 key购买 nike

所以,我正在编写一个代码,它创建一个数组,其中包含一个元素和用户编写的元素数量,然后代码生成一个最大值,最后一位数字为零。所以,我已经完成了编写一个数组的步骤,该数组是用户输入的元素。最难的部分(对我来说)是完成一个生成最后一位为零的最大值的代码。所以是的,我需要一个建议来完成这段代码。谢谢。

例如:

一个数组 - 2 20 25 300 55555最后一位为零的最大数是300

是的,我需要一些建议来完成这段代码。这是我到目前为止所做的代码:

#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <time.h>

int GetAmount() {
int howmany;
printf("Enter amount of elements - ");
scanf_s("%i", &howmany);
return howmany;
}

void GetArray(int a[], int n) {
printf("Enter elements - \n");
for (int i = 0; i < n; i++)
{ printf("%i ->", i);
scanf_s("%i", &a[i]);

}
}

int LastDigitZero(int n[], int a) {
for (int i = 0; i < a; i++)
{
if (n[i] % 10 == 0) {
return 0;
}
}
}

int maxvalue(int a[], int n) {
int temp = 0;
for (int i = 0; i < n; i++)
{
if (a[i] > temp) {
temp = a[i];
}
}
return temp;
}



void main() {
int amount = GetAmount();
int array[100];
GetArray(array, amount);
int max = maxvalue(array, amount);
printf("Max Value is %i\n", max);
}

感谢您的关注,祝您有美好的一天! :)

最佳答案

有效!看起来就是这样!

#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <time.h>

int GetAmount() {
int howmany;
printf("Enter amount of elements - ");
scanf_s("%i", &howmany);
return howmany;
}

void GetArray(int a[], int n) {
printf("Enter elements - \n");
for (int i = 0; i < n; i++)
{ printf("%i ->", i);
scanf_s("%i", &a[i]);

}
}


int maxvalue(int a[], int n) {
int temp = 0;
for (int i = 0; i < n; i++)
{
if (a[i] % 10 == 0 && a[i] > temp) {
temp = a[i];
}
}
return temp;
}



void main() {
int amount = GetAmount();
int array[100];
GetArray(array, amount);
int max = maxvalue(array, amount);
printf("The biggest number which last digit is zero is %i\n ", max);
system("pause");

}

谢谢大家的回答!那太快了!!! :)

关于c++ - 获取数组中以 0 结尾的最大元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33260888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com