如何编写以下问题的代码 [英] How do I write a code for the following problem

查看:75
本文介绍了如何编写以下问题的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一组九个单(不一定是不同的)数字,比如0,0,1,3,4,6,7,8,9,可以形成许多不同的时间以12小时的时间格式HH:MM:SS,例如10:36:40或01:39:46,只使用每个数字一次。目标是找到最大可能的有效时间(00:00:01到12:00:00),这些时间可以使用九个数字中的六个完全一次形成。在这种情况下,它是10:49:38。









我试图通过转换为秒来实现它,其中最大时间是12:59:59,即46799秒。请推荐一个更好的代码。



我尝试过:



#include< stdio.h>

int main()

{

int num [9];

int temp = 0;

int cnt = 0;

int A = 0,B = 0,C = 0,D = 0 ,E = 0,F = 0;

for(int a = 0; a< 9; a ++)

{for(int b = 0; b< 9; b ++)

{for(int c = 0; c< 9; c ++)

{for(int d = 0; d< 9; d ++)

{for(int e = 0; e< 9; e ++)

{for(int f = 0; f< 9; f ++)

{if ((10 * num [e] + num [f])< 60)

{int cal =(10 * num [a] + num [b])* 3600 +(10 * num [c] + num [d])* 60 +(10 * num [e] + num [f]);

scanf(%d%d%d%d%d%d% d%d%d,& A,& B,& C,& D,& E,& F);

if(cal <= 46799)

{cnt ++;

if(temp< cal)

{temp = cal; A = num [a]; B = num [b]; C = num [c]; D = num [d]; E = num [e]; F = num [f];

}

}

}

}

}

}

}

}

}

if(cnt == 0){printf(不可能); } else {printf(%d%d:%d%d:%d%d,A,B,C,D,E,F); }}

Given a set of nine single (not necessarily distinct) digits, say 0, 0, 1, 3, 4, 6, 7, 8, 9, it is possible to form many distinct times in a 12 hour time format HH:MM:SS, such as 10:36:40 or 01:39:46 by using each of the digits only once. The objective is to find the maximum possible valid time (00:00:01 to 12:00:00) that can be formed using some six of the nine digits exactly once. In this case, it is 10:49:38.




I've tried to do it by converting into seconds , where maximum time, which is 12:59:59, which is 46799 seconds. Please suggest a better code.

What I have tried:

#include <stdio.h>
int main()
{
int num[9];
int temp = 0;
int cnt=0;
int A=0,B=0,C=0,D=0,E=0,F=0;
for(int a=0;a<9; a++)
{ for(int b=0;b<9; b++)
{ for(int c=0;c<9; c++)
{ for(int d=0;d<9; d++)
{ for(int e=0;e<9;e++)
{ for(int f=0;f<9;f++)
{ if((10*num[e]+num[f])<60)
{ int cal = (10 * num[a] + num[b]) * 3600 + (10 * num[c] + num[d]) * 60 + (10 * num[e] + num[f]);
scanf("%d %d %d %d %d %d %d %d %d",&A,&B,&C,&D,&E,&F);
if (cal <= 46799)
{ cnt++;
if (temp < cal)
{ temp = cal; A = num[a]; B = num[b]; C = num[c]; D = num[d]; E = num[e]; F = num[f];
}
}
}
}
}
}
}
}
}
if(cnt==0){ printf("impossible"); }else { printf ("%d %d : %d %d : %d %d",A,B,C,D,E,F); } }

推荐答案

引用:

我试过通过转换来做到这一点到秒,最大时间,即12:59:59,即46799秒。请建议一个更好的代码。

I've tried to do it by converting into seconds , where maximum time, which is 12:59:59, which is 46799 seconds. Please suggest a better code.



错误的方法,只是简单的错误。



我会写一个程序,将组成每个可能的组合,检查哪些是有效时间并保持最佳答案。



我给出一个指导,这是你的HomeWork,你编写代码。


Wrong approach, just plain wrong.

I would write a program that will compose every possible combination, check which ones are valid times and keep best answer.

I give a guideline, it is your HomeWork, you write the code.


问题不在于秒:它是关于检查九个数字集的每个可能顺序,首先发现有效时间值,然后是最高12小时时间。所以看看有效的是什么:

The problem is not about seconds: it's about checking each possible order of the set of nine digits and first discovering the valid time values, and then the highest 12 hour time. So look at what is valid:
Hour:   01 to 12
Minute: 00 to 59
Second: 00 to 59

并开始将输入值组织到他们可以去的地方。

例如,小时的最重要数字必须是零或一 - 所以找到输入中的值那个合适。同样地,对于任何值,6到9(包括6和9)不能处于最重要的分钟或第二位置。

对这些进行排序,然后您可以开始向下工作:最高时刻,然后是最高时刻剩下的,然后是最高的第二个。

我建议首先对输入进行排序并从那里开始工作。



但这是你的作业,而不是我的作业 - 所以我不会给你任何代码!

And start organising the input values into where they can go.
For example, the most significant digit of the hour must be a zero or a one - so find the values you have in the input that fit. Similarly for any values 6 to 9 inclusive cannot be in the most significant minute or second position.
Sort out those, and you can then start working your way down: the highest hour, then the highest minute from what is left, then the highest second.
I'd suggest starting by sorting the input and working from there.

But this is your homework, not mine - so I'll give you no code!


这篇关于如何编写以下问题的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆