找到员工的薪水 [英] Find Salary of employees
问题描述
几天前,我老师要求找到以下问题的解决方案。
三兄弟有一家商店。他们知道未来几天商店的总利润。兄弟俩同意他们将总时间分成三个连续的部分,不一定等于
持续时间,并且每个人都将在商店工作,并在其中一个部分收集相应的利润他代表。
但他们想公平地达成交易,这样就不会从其他人那里获得更多的利润。具体来说,他们希望最大限度地减少
利润将获得三者中最青睐的。
我们希望找到更受青睐的兄弟的利润。我们知道每天的日子和利润。输入文件中的第一个数字是K天数和跟随的K数,即每天的利润。
输入:8 5 6 1 4 9 3 1 2
例如输出为13.
第一个兄弟的利润是5 + 6 + 1 = 12。第二个兄弟的利润是
4 + 9 = 13。第三个兄弟的利润是3 + 1 + 2 = 6 ..
所以我们必须找到一个灵活的工作时间为每个兄弟和他的工资,然后写出最大的工资在一个文件中。
这是我到目前为止所做的:
问题
如何制作一个可以找到evry员工工作日的算法,因为我的工作效率不高且不是正确的。我只是花了很多时间思考它,我找不到解决方案。有人有什么想法吗?
以下是代码:
#include < stdio.h >
#include < stdlib.h >
void write_output(FILE * output, int 钱){
output = fopen( share.out, w +);
fprintf(输出, %d \ n,money);
fclose(输出);
}
int main(){
FILE * input,* output ;
int days = 0 ,sum = 0 跨度>;
input = fopen( share.in, r);
fscanf(输入, %d,& days);
int * salary =( int *)malloc(days * sizeof ( int ));
for ( int i = 0 ; i< days; i ++){
fscanf(输入, %d ,& salary [i]);
sum + = salary [i];
}
fclose(输入);
int average_salary =(sum / 3 );
/ * **************** ******************* /
for ( int i = 0 ; i< days; i ++){
printf( %d \ n,salary [i]);
}
/ * *************** ************************************* /
int first = 0 ,second = 0 ,third = 0 ,i = 0 ;
while (first< = average_salary){
first + = salary [i];
i ++;
}
int position_last_first = i;
while (second< = average_salary){
second + = salary [i];
i ++;
}
int position_last_second = i;
while (i <天){
third + = salary [i];
i ++;
}
/ * *********** **************************************** /
int big = 0 ;
int nfirst = first,nsecond = second,nthird = third;
if (first> second){
if (第一个>第三个){
big = first;
nfirst =第一个+工资[position_last_first];
nsecond = second - salary [position_last_first];
}
其他 {
big = third;
nthird = third - salary [position_last_second];
nfirst =第一个+工资[position_last_second];
}
}
其他 {
如果(第二个>第三个){
big = second;
nfirst =第一个+工资[position_last_first];
nsecond = second - salary [position_last_first];
}
else {
big = third;
nthird = third - salary [position_last_second];
nsecond = nsecond + salary [position_last_second];
}
}
if (nfirst> nsecond){
if (nfirst> nthird){
write_output(output,nfirst);
}
else {
write_output(output,nthird);
}
}
其他 {
如果( nsecond> nthird){
write_output(output,nsecond);
}
else {
write_output(output,nthird);
}
}
免费(工资);
return 0 ;
}
对于较小的数字,您可以计算所有排列并对具有最大份额的每个排列进行加权。根据您的标准,最小份额的最小份额(不一定只有一份)是最佳选择。
例如
A 次> 乙次> C 次> 重量次> < td> 5 6 1 4 9 3 1 2 20 5 6 1 4 9 3 1 2 19 5 6 1 4 9 3 1 2 15 5 6 1 4 9 3 1 2 20 5 6 1 4 9 3 1 2 23 5 6 1 4 9 3 1 2 24 5 6 1 4 9 3 1 2 19 5 6 1 4 9 3 1 2 15 5 6 1 4 9 3 1 2 14 5 6 1 4 9 3 1 2 17 5 6 1 4 9 3 1 2 18 5 6 1 4 9 3 1 2 15 5 6 1 4 9 3 1 2 13 5 6 1 4 9 3 1 2 16 5 6 1 4 9 3 1 2 17 5 6 1 4 9 3 1 2 16 5 6 1 4 9 3 1 2 16 5 6 1 4 9 3 1 2 16 5 6 1 4 9 3 1 2 25 5 6 1 4 9 3 1 2 25 5 6 1 4 9 3 1 2 28
这远非最佳,但会导致所有可能的解决方案,您可以选择(一)最小的解决方案。
排列的数量是:
- 给定:3兄弟,N个数字
- 排列:1 + 0.5 * N *(N-3)
- 例如N = 8 - >排列= 1 + 0.5 * 8 * 5 = 21
- 例如N = 20 - > permutations = 1 + 0.5 * 20 * 17 = 171
现在你需要定义一个创建排列的算法。
一种可能的算法为每个排列赋予唯一的id,例如1,1,6是A = 1,B = 1,C = 6的数字,即上表的第一个排列。在给定的情况下,3,2,3是最佳的。
例如保持最后一个最小解的粗算法可以实现如下N = ...
薪水[0 ... N-1] = ...
Min =总和(薪水[0 ... N-1])$ b $ b排列= [0,0,0]
,A = 1到N-2,循环
,B = 1到N-1-A do循环
ProfitA = Sum(薪水[0 ... A-1])$ b $ b ProfitB =总和(薪水[A ... A + B-1])$ b $ b ProfitC = Sum(薪水[A + B ... N-1])$ b $ b ProfitMax = Max(ProfitA,ProfitB,ProfitC)
如果Min大于ProfitMax则
Min = ProfitMax
排列= [A,B,C]
结束如果
结束循环
结束循环
打印最小值:排列只有这样你应该开始编写C代码。
我将此作为练习在C中实现。
干杯
Andi
A few days back, I asked by my teacher to find a solution in the following problem.
Three brothers have a shop.They know the total profit of the shop in the upcoming days. The brothers agreed that they would divide the total time in three successive parts, not necessarily equal
duration, and that each one will be working in the shop and during one of these parts to collect the corresponding profit on his behalf.
But they want to make a deal fairly, so that one not to received from more profit
from someone else. Specifically, they want to minimize the
profit will receive the most "favored" of the three.
We want to find the profit of the more "favored" brother. We know the days and the profit of every day. The first number in the input file is the number of K days and the follow K numbers which is the profit of every day.
Εg of input : 8 5 6 1 4 9 3 1 2
Eg of output is 13.
The first brother profit is 5+6+1=12. The second brother profit is
4+9=13. The third brother profit is 3+1+2=6..
So we must find a flexible work hour for each brother and his salary and then to write the biggest salary in a file.
Here what I did so far:
Question
How can I make an algorythm that can find the days of work of evry employee cause mine isn't very efficient and isn't the right one. I just pass so much time to think about it and I can't find a solution. Has someone any idea?
Here is the code :
#include <stdio.h>
#include <stdlib.h>
void write_output(FILE* output,int money) {
output = fopen("share.out","w+");
fprintf(output,"%d\n",money);
fclose(output);
}
int main() {
FILE *input,*output;
int days = 0, sum = 0;
input = fopen("share.in","r");
fscanf(input,"%d",&days);
int *salary = (int *)malloc(days * sizeof(int));
for (int i = 0; i < days; i++){
fscanf(input,"%d", &salary[i]);
sum += salary[i];
}
fclose(input);
int average_salary = (sum / 3);
/************************************/
for (int i = 0; i < days; i++){
printf("%d\n", salary[i]);
}
/*****************************************************/
int first = 0, second = 0, third = 0, i = 0;
while (first <= average_salary) {
first += salary[i];
i++;
}
int position_last_first = i ;
while (second <= average_salary) {
second += salary[i];
i++;
}
int position_last_second = i ;
while (i < days) {
third += salary[i];
i++;
}
/****************************************************/
int big = 0;
int nfirst = first, nsecond = second, nthird = third;
if (first > second) {
if(first > third) {
big = first;
nfirst = first + salary[position_last_first];
nsecond = second - salary[position_last_first];
}
else {
big = third;
nthird = third - salary[position_last_second];
nfirst = first + salary[position_last_second];
}
}
else {
if(second > third) {
big = second;
nfirst = first + salary[position_last_first];
nsecond = second - salary[position_last_first];
}
else {
big = third;
nthird = third - salary[position_last_second];
nsecond = nsecond + salary[position_last_second];
}
}
if (nfirst > nsecond) {
if(nfirst > nthird) {
write_output(output, nfirst);
}
else {
write_output(output, nthird);
}
}
else {
if(nsecond > nthird) {
write_output(output, nsecond);
}
else {
write_output(output, nthird);
}
}
free(salary);
return 0;
}
For small numbers, you might calculate all permutations and weight each permutation with the largest share. The minimum of the largest share (not necessarily only one) is the best choice according to your criterions.
E.g.
A B C weight 5 6 1 4 9 3 1 2 20 5 6 1 4 9 3 1 2 19 5 6 1 4 9 3 1 2 15 5 6 1 4 9 3 1 2 20 5 6 1 4 9 3 1 2 23 5 6 1 4 9 3 1 2 24 5 6 1 4 9 3 1 2 19 5 6 1 4 9 3 1 2 15 5 6 1 4 9 3 1 2 14 5 6 1 4 9 3 1 2 17 5 6 1 4 9 3 1 2 18 5 6 1 4 9 3 1 2 15 5 6 1 4 9 3 1 2 13 5 6 1 4 9 3 1 2 16 5 6 1 4 9 3 1 2 17 5 6 1 4 9 3 1 2 16 5 6 1 4 9 3 1 2 16 5 6 1 4 9 3 1 2 16 5 6 1 4 9 3 1 2 25 5 6 1 4 9 3 1 2 25 5 6 1 4 9 3 1 2 28
This is far from optimal, but it will result in all possible solutions and you can chose the (one) minimal one.
The number of permutations is:
- given: 3 brothers, N numbers
- permutations: 1+0.5*N*(N-3)
- E.g. N=8 --> permutations = 1+0.5*8*5 = 21
- E.g. N=20 --> permutations = 1+0.5*20*17=171
Now you need to define an algorithm that creates the permutations.
A possible algorithm gives to each permutation a unique id, e.g. 1,1,6 is A=1, B=1, C=6 numbers, i.e. the first permutation of the table above. In the given case, 3,2,3 is the optimum.
E.g. a crude algorithm that keeps the last minimal solution could be implemented as followsN = ... Salary[0...N-1] = ... Min = Sum(Salary[0...N-1]) Permutation = [0,0,0] for A=1 to N-2 do loop for B=1 to N-1-A do loop ProfitA = Sum(Salary[0...A-1]) ProfitB = Sum(Salary[A...A+B-1]) ProfitC = Sum(Salary[A+B...N-1]) ProfitMax = Max(ProfitA, ProfitB, ProfitC) if Min is greater than ProfitMax then Min = ProfitMax Permutation = [A,B,C] end if end loop end loop Print Min : PermutationOnly then you should start writing C code.
I leave this as exercise to implement in C.
Cheers
Andi
这篇关于找到员工的薪水的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!