鶴壁網(wǎng)站建設(shè)公司佛山網(wǎng)站建設(shè)模板
不與最大數(shù)相同的數(shù)字之和
- C語言代碼
- C++ 語言代碼
- Java語言代碼
- Python語言代碼
💐The Begin💐點點關(guān)注,收藏不迷路💐 |
輸出一個整數(shù)數(shù)列中不與最大數(shù)相同的數(shù)字之和。
輸入
輸入分為兩行:
第一行為N(N為接下來數(shù)的個數(shù),N <= 100);
第二行為N個整數(shù),數(shù)與數(shù)之間以一個空格分開,每個整數(shù)的范圍是-1000,000到1000,000。
輸出
輸出為N個數(shù)中除去最大數(shù)其余數(shù)字之和。
樣例輸入
3
1 2 3
樣例輸出
3
先讀取輸入的整數(shù)個數(shù)以及對應(yīng)的整數(shù)數(shù)列,接著找出數(shù)列中的最大數(shù),然后遍歷數(shù)列,將不等于最大數(shù)的所有數(shù)字進行累加,最后輸出累加的結(jié)果,即不與最大數(shù)相同的數(shù)字之和。
C語言代碼
#include <stdio.h>
int main() {
????int n;
????scanf("
%d"
, &n); // 讀取輸入的整數(shù)個數(shù)n
????int nums[n]; // 定義數(shù)組,用于存儲輸入的整數(shù)數(shù)列
????for (int i = 0; i < n; i++) {
????????scanf("
%d"
, &nums[i]); // 循環(huán)讀取n個整數(shù),存入數(shù)組
????}
????int max_num = nums[0]; // 先假設(shè)數(shù)組中的第一個數(shù)為最大數(shù),初始化最大數(shù)
????for (int i = 1; i < n; i++) { // 從第二個數(shù)開始遍歷數(shù)組,尋找真正的最大數(shù)
????????if (nums[i] > max_num) { // 如果當(dāng)前數(shù)大于已記錄的最大數(shù)
????????????max_num = nums[i]; // 更新最大數(shù)
????????}
????}
????int sum = 0; // 用于累加不與最大數(shù)相同的數(shù)字之和,初始化為0
????for (int i = 0; i < n; i++) {
????????if (nums[i]!= max_num) { // 判斷當(dāng)前數(shù)是否不等于最大數(shù)
????????????sum += nums[i]; // 如果不等于,累加到總和中
????????}
????}
????printf("
%d\n"
, sum); // 輸出不與最大數(shù)相同的數(shù)字之和
????return 0;
}
C++ 語言代碼
#include
using namespace std;
int main() {
????int n;
????cin >> n; // 輸入整數(shù)的個數(shù)n
????int nums[n]; // 創(chuàng)建數(shù)組來存儲整數(shù)數(shù)列
????for (int i = 0; i < n; i++) {
????????cin >> nums[i]; // 依次輸入n個整數(shù)到數(shù)組中
????}
????int max_num = nums[0]; // 初始把第一個數(shù)當(dāng)作最大數(shù)
????for (int i = 1; i < n; i++) { // 從第二個數(shù)開始遍歷數(shù)組,找出真正的最大數(shù)
????????if (nums[i] > max_num) { // 若當(dāng)前數(shù)大于已記錄的最大數(shù)
????????????max_num = nums[i]; // 更新最大數(shù)
????????}
????}
????int sum = 0; // 初始化用于累加的變量為0,用來計算不與最大數(shù)相同的數(shù)字之和
????for (int i = 0; i < n; i++) {
????????if (nums[i]!= max_num) { // 檢查當(dāng)前數(shù)是否不等于最大數(shù)
????????????sum += nums[i]; // 不等于則累加到總和中
????????}
????}
????cout << sum << endl; // 輸出不與最大數(shù)相同的數(shù)字之和
????return 0;
}
Java語言代碼
import java.util.Scanner;
public class Main {
????Scanner scanner = new Scanner(System.in);
????int n = scanner.nextInt(); // 獲取輸入的整數(shù)個數(shù)n
????int[] nums = new int[n]; // 定義數(shù)組存儲整數(shù)數(shù)列
????for (int i = 0; i < n; i++) {
????????nums[i] = scanner.nextInt(); // 循環(huán)讀取n個整數(shù)并存入數(shù)組
????}
????int max_num = nums[0]; // 初始假設(shè)數(shù)組中第一個數(shù)是最大數(shù)
????for (int i = 1; i < n; i++) { // 從第二個元素開始遍歷數(shù)組,確定真正的最大數(shù)
????????if (nums[i] > max_num) { // 如果當(dāng)前元素大于已記錄的最大數(shù)
????????????max_num = nums[i]; // 更新最大數(shù)
????????}
????}
????int sum = 0; // 用于累加不與最大數(shù)相同的數(shù)字之和,初始化為0
????for (int i = 0; i < n; i++) {
????????if (nums[i]!= max_num) { // 判斷當(dāng)前元素是否不等于最大數(shù)
????????????sum += nums[i]; // 不等于則累加到總和中
????????}
????}
????System.out.println(sum); // 輸出不與最大數(shù)相同的數(shù)字之和
}
Python語言代碼
n = int(input()) // 獲取輸入的整數(shù)個數(shù)n
nums = list(map(int, input().split())) // 獲取輸入的整數(shù)數(shù)列,轉(zhuǎn)換為列表
max_num = nums[0] // 先假設(shè)列表中的第一個數(shù)是最大數(shù)
for num in nums[1:]: // 從列表的第二個數(shù)開始遍歷,尋找真正的最大數(shù)
????if num > max_num: // 如果當(dāng)前數(shù)大于已記錄的最大數(shù)
????????max_num = num // 更新最大數(shù)
sum_ = 0 // 初始化用于累加的變量為0,用來計算不與最大數(shù)相同的數(shù)字之和
for num in nums:
????if num!= max_num: // 判斷當(dāng)前數(shù)是否不等于最大數(shù)
????????sum_ += num // 不等于則累加到總和中
print(sum_) // 輸出不與最大數(shù)相同的數(shù)字之和
💐The End💐點點關(guān)注,收藏不迷路💐 |