題目(截圖)

Untitled

程式區塊(C)

#include<stdio.h>

struct customer { 
	char lastName[15];
	char firstName[15];
	double payment = 0;
	unsigned int customerNumber = 0;
};

int main(void){
	struct customer people;
	struct customer highest;
	
	while(scanf("%s", &people.firstName) != EOF){
		scanf("%s", &people.lastName);
		scanf("%d", &people.payment);
		scanf("%d", &people.customerNumber);
		if(people.payment > highest.payment){
			highest = people;
		}
	}
    
    printf("%s %s %d\\n", highest.firstName, highest.lastName, highest.customerNumber);
} 

程式區塊(C++)

// 當你需要儲存每個不同顧客的數據時,再使用以下的程式碼
#include<bits/stdc++.h>
using namespace std;

struct customer { 
	char lastName[15];
	char firstName[15];
	double payment;
	unsigned int customerNumber;
};

int main(void){
	struct customer people[100];
	int i = 0;
	while(cin >> people[i].firstName && !cin.eof()){
		cin >> people[i].lastName;
		cin >> people[i].payment;
		cin >> people[i].customerNumber;
		i++;
	}
	
	int maxIndex = 0;
	
	for (i = 1; i < 101; ++i) {
        if (people[i].payment > people[maxIndex].payment) {
            maxIndex = i;
        }
    }
    
    cout << people[maxIndex].firstName << ' ' << people[maxIndex].lastName << ' ' << people[maxIndex].customerNumber;
} 

解題思路

需要學習如何撰寫 struct 結構的基本語法

因為測資不會重複,所以可以在輸入時就作判斷,誰是最高消費額的人