C++ 5. Maximum profit

Insert picture description hereInsert picture description here
Hahahaha, do you need a translator? I didn't understand it anyway. . . But when we understand it, you will find that this question is still very kind

Sorting out the key information of the topic:

The company keeps records every five years, so that it can calculate (12-5+1=) 8 times in a year. These 8 times are lost every five years (deficit), but the year-end calculation seems to be It is profitable, and it is likely to be profitable. Here let us design the code, assuming that the annual surplus (s) or loss (d) is the same (the kindness of this question), let us judge how to arrange the monthly The profit and loss situation, so that the surplus in a year is the largest under the premise of meeting the conditions of the question, if it is impossible to make a surplus, output the string Deficit

Code and analysis

//最大盈利
//ssssdssssdss
//sssddsssddss
//ssdddssdddss
//sddddsddddsd
//dddddddddddd
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
    
    
	int s,d;
	while(scanf("%d%d",&s,&d) !=EOF){
    
    
		if(4*s-d<0 && 10*s-2*d>0){
    
    
			cout<<10*s-2*d<<endl;
		}
		else if(3*s-2*d<0 && 8*s-4*d>0){
    
    
			cout<<8*s-4*d<<endl;
		}
		else if(2*s-3*d<0 && 6*s-6*d>0){
    
    
			cout<<6*s-6*d<<endl;
		}
		else if(s-4*d<0 && 3*s-9*d>0){
    
    
			cout<<3*s-9*d<<endl;
		}
		else{
    
    
			cout<<"Deficit"<<endl;
		}	
	}
	return 0;
} 

As long as the idea is clear, it is very simple:
I give all the design conditions at the beginning of the code, and use the if-else statement to discuss. The judgment condition is five years of loss and full year profit. Why is this all the situation? It is conceivable that if we want to make the most profit in a year, and every five years is a cycle, then we try to put the loss month at the end of the cycle, that is to generate as much surplus as possible, so as to solve the problem. Isn't it kind enough?
PS:
Gradually I feel that it is much more convenient to use cin and cout, so use them as much as possible in the future!

Come on with ZDZ!
Be the master of time, the master of destiny, the helmsman of the soul!
--Roosevelt

Guess you like

Origin blog.csdn.net/interestingddd/article/details/113662493