This repository has been archived on 2023-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
MaxMultiplication/main.cpp
2023-06-15 22:46:24 +08:00

42 lines
1.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <cstdio>
// 给你一个整数数组 nums请你选择数组的两个不同下标 i 和 j使 (nums[i]-1)*(nums[j]-1) 取得最大值。
// 请你计算并返回该式的最大值。
int maxProduct(int nums[], int numsSize) {
int MaxNumber = 0;
for (int i = 0; i < numsSize; ++i) {
for (int j = 0; j < numsSize; ++j) {
if (i == j) continue;
if ((nums[i] - 1) * (nums[j] - 1) > MaxNumber) {
MaxNumber = (nums[i] - 1) * (nums[j] - 1);
}
}
}
return MaxNumber;
}
int main() {
while (true) {
int numsSize;
printf("[INFO] 请输入数组大小: ");
scanf("%d", &numsSize);
if (numsSize == -1) break;
if (numsSize <= 1) {
printf("[WARNING] 无法完成计算最少需要2位数\n\n");
continue;
}
getchar();
printf("[INFO] 请输入数组内容(中间使用逗号隔开): ");
int nums[numsSize];
for (int i = 0; i < numsSize - 1; ++i) {
scanf("%d,", &nums[i]);
}
scanf("%d", &nums[numsSize - 1]);
getchar();
// upload data to maxProduct function
printf("[OUTPUT] 输出结果: %d\n", maxProduct(nums, numsSize));
printf("\n");
}
printf("[SYSTEM] 已退出\n");
return 0;
}