2018年阿里在线编程题(c++) 50
嘉宾随机写出6个0~9之间的数字,然后用这六个数字组合成一个最早的时间和一个最晚的时间(24小时制),然后商家会在这两个时间点发红包,如果6个数字无法组合成有效的时间,则...
嘉宾随机写出6个0~9之间的数字,然后用这六个数字组合成一个最早的时间和一个最晚的时间(24小时制),然后商家会在这两个时间点发红包,如果6个数字无法组合成有效的时间,则告诉用户时间无效。
如:
用户输入123456,最早的时间是12:34:56 最晚的时间是23:56:41,输出123456,235641
用户输入 111111,则最早和最晚的时间都是11:11:11,输出111111
用书输入999999,则无效,输出N/A
求c++代码 展开
如:
用户输入123456,最早的时间是12:34:56 最晚的时间是23:56:41,输出123456,235641
用户输入 111111,则最早和最晚的时间都是11:11:11,输出111111
用书输入999999,则无效,输出N/A
求c++代码 展开
2个回答
展开全部
贴出求最小时间代码
int FindMinNum(int nums[6], int masks[6])
{
int min = 10;
int min_index = 0;
for (int i = 0; i < 6; i++)
{
if (masks[i] == 0)
{
if (nums[i] < min)
{
min = nums[i];
min_index = i;
}
}
}
masks[min_index] = 1;
return min;
}
void MinTimeTest(int nums[6])
{
int masks[6] = { 0 };
int h_h = FindMinNum(nums, masks);
if (h_h > 2)
{
printf("invalid\n");
return;
}
int h_l;
int m_h, m_l, s_h, s_l;
if (h_h == 2)
{
h_l = FindMinNum(nums, masks);
if (h_l >= 4)
{
printf("invalid\n");
return;
}
int data[4] = {0};
data[0] = FindMinNum(nums, masks);
data[1] = FindMinNum(nums, masks);
data[2] = FindMinNum(nums, masks);
data[3] = FindMinNum(nums, masks);
//4 data, case0:1,2,3,4 case1:1, 2, 3, 6 case2:1,2,6,7 case3:1,6,7,8
if (data[2] < 6)
{
m_h = data[0];
m_l = data[1];
s_h = data[2];
s_l = data[3];
}
else if (data[1] < 6)
{
m_h = data[0];
m_l = data[2];
s_h = data[1];
s_l = data[3];
}
else
{
printf("invalid\n");
return;
}
}
else
{
int data[5] = { 0 };
data[0] = FindMinNum(nums, masks);
data[1] = FindMinNum(nums, masks);
data[2] = FindMinNum(nums, masks);
data[3] = FindMinNum(nums, masks);
data[4] = FindMinNum(nums, masks);
//5 data, case0:1,2,3,4,5 case1:1,2,3,5,9 case2:1,2,3,8,9 case3:1,2,7,8,9 case4:1,6,7,8,9
if (data[3] < 6)
{
h_l = data[0];
m_h = data[1];
m_l = data[2];
s_h = data[3];
s_l = data[4];
}
else if (data[2] < 6)
{
h_l = data[0];
m_h = data[1];
m_l = data[3];
s_h = data[2];
s_l = data[4];
}
else if (data[1] < 6)
{
h_l = data[2];
m_h = data[0];
m_l = data[3];
s_h = data[1];
s_l = data[4];
}
else
{
printf("invalid\n");
return;
}
}
printf("%d%d:%d%d:%d%d\n", h_h, h_l, m_h, m_l, s_h, s_l);
}
int main()
{
int nums[] = { 0, 9, 1, 9, 2, 8 };
MinTimeTest(nums);
}
int FindMinNum(int nums[6], int masks[6])
{
int min = 10;
int min_index = 0;
for (int i = 0; i < 6; i++)
{
if (masks[i] == 0)
{
if (nums[i] < min)
{
min = nums[i];
min_index = i;
}
}
}
masks[min_index] = 1;
return min;
}
void MinTimeTest(int nums[6])
{
int masks[6] = { 0 };
int h_h = FindMinNum(nums, masks);
if (h_h > 2)
{
printf("invalid\n");
return;
}
int h_l;
int m_h, m_l, s_h, s_l;
if (h_h == 2)
{
h_l = FindMinNum(nums, masks);
if (h_l >= 4)
{
printf("invalid\n");
return;
}
int data[4] = {0};
data[0] = FindMinNum(nums, masks);
data[1] = FindMinNum(nums, masks);
data[2] = FindMinNum(nums, masks);
data[3] = FindMinNum(nums, masks);
//4 data, case0:1,2,3,4 case1:1, 2, 3, 6 case2:1,2,6,7 case3:1,6,7,8
if (data[2] < 6)
{
m_h = data[0];
m_l = data[1];
s_h = data[2];
s_l = data[3];
}
else if (data[1] < 6)
{
m_h = data[0];
m_l = data[2];
s_h = data[1];
s_l = data[3];
}
else
{
printf("invalid\n");
return;
}
}
else
{
int data[5] = { 0 };
data[0] = FindMinNum(nums, masks);
data[1] = FindMinNum(nums, masks);
data[2] = FindMinNum(nums, masks);
data[3] = FindMinNum(nums, masks);
data[4] = FindMinNum(nums, masks);
//5 data, case0:1,2,3,4,5 case1:1,2,3,5,9 case2:1,2,3,8,9 case3:1,2,7,8,9 case4:1,6,7,8,9
if (data[3] < 6)
{
h_l = data[0];
m_h = data[1];
m_l = data[2];
s_h = data[3];
s_l = data[4];
}
else if (data[2] < 6)
{
h_l = data[0];
m_h = data[1];
m_l = data[3];
s_h = data[2];
s_l = data[4];
}
else if (data[1] < 6)
{
h_l = data[2];
m_h = data[0];
m_l = data[3];
s_h = data[1];
s_l = data[4];
}
else
{
printf("invalid\n");
return;
}
}
printf("%d%d:%d%d:%d%d\n", h_h, h_l, m_h, m_l, s_h, s_l);
}
int main()
{
int nums[] = { 0, 9, 1, 9, 2, 8 };
MinTimeTest(nums);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接输入123456即可.不要加分隔符,你自己粘贴到IDE里面看吧,我这里不知道为什么不能转换为代码格式回答。
#include <string>
#include <iostream>
#include <stdio.h>
int main()
{
using namespace std;
string sacrifice;
int a[6];
while(1){
getline(cin,sacrifice);
for(int i=0;i<6;i++){
a[i]=sacrifice[i]-48;
}
for(int i=0;i<5;i++)
for(int j=i+1;j<6;j++)
if(a[j-1]>a[j]){
int sac=a[j-1];
a[j-1]=a[j];
a[j]=sac;
}
if(a[0]>2||a[1]>3||a[2]>5||a[3]>5){
printf("N/A\n");
}
else if(a[2]*10+a[3]<60&&a[4]*10+a[5]<60){
string min;
min+=a[0]+48;min+=a[1]+48;min+=":";min+=a[2]+48;min+=a[3]+48;min+=":";min+=a[4]+48;min+=a[5]+48;
printf("%s",min.c_str());
for(int i=1;i<6;i++){
if(a[i]>a[0]&&a[i]<=2){
int sac=a[i];
a[i]=a[0];
a[0]=sac;
}
}
int jud=9;
if(a[0]==2)jud=3;
for(int i=2;i<6;i++){
if(a[i]>a[1]&&a[i]<=jud){
int sac=a[i];
a[i]=a[1];
a[1]=sac;
}
}
if(a[4]>5&&a[5]>5){
int sac=a[2],sac2=a[4];
a[2]=a[3];
a[4]=sac;
a[3]=a[5];
a[5]=sac2;
}
else if(a[5]>5){
int sac=a[2],sac2=a[3];
a[3]=a[5];
a[2]=a[4];
a[4]=sac2;
a[5]=sac;
}
else{
int sac=a[5];
a[5]=a[2];
a[2]=sac;
sac=a[3];
a[3]=a[4];
a[4]=sac;
}
string max;
max+=a[0]+48;max+=a[1]+48;max+=":";max+=a[2]+48;max+=a[3]+48;max+=":";max+=a[4]+48;max+=a[5]+48;
if(max!=min)printf(",%s\n",max.c_str());
else
printf("\n");
}
}
}
#include <string>
#include <iostream>
#include <stdio.h>
int main()
{
using namespace std;
string sacrifice;
int a[6];
while(1){
getline(cin,sacrifice);
for(int i=0;i<6;i++){
a[i]=sacrifice[i]-48;
}
for(int i=0;i<5;i++)
for(int j=i+1;j<6;j++)
if(a[j-1]>a[j]){
int sac=a[j-1];
a[j-1]=a[j];
a[j]=sac;
}
if(a[0]>2||a[1]>3||a[2]>5||a[3]>5){
printf("N/A\n");
}
else if(a[2]*10+a[3]<60&&a[4]*10+a[5]<60){
string min;
min+=a[0]+48;min+=a[1]+48;min+=":";min+=a[2]+48;min+=a[3]+48;min+=":";min+=a[4]+48;min+=a[5]+48;
printf("%s",min.c_str());
for(int i=1;i<6;i++){
if(a[i]>a[0]&&a[i]<=2){
int sac=a[i];
a[i]=a[0];
a[0]=sac;
}
}
int jud=9;
if(a[0]==2)jud=3;
for(int i=2;i<6;i++){
if(a[i]>a[1]&&a[i]<=jud){
int sac=a[i];
a[i]=a[1];
a[1]=sac;
}
}
if(a[4]>5&&a[5]>5){
int sac=a[2],sac2=a[4];
a[2]=a[3];
a[4]=sac;
a[3]=a[5];
a[5]=sac2;
}
else if(a[5]>5){
int sac=a[2],sac2=a[3];
a[3]=a[5];
a[2]=a[4];
a[4]=sac2;
a[5]=sac;
}
else{
int sac=a[5];
a[5]=a[2];
a[2]=sac;
sac=a[3];
a[3]=a[4];
a[4]=sac;
}
string max;
max+=a[0]+48;max+=a[1]+48;max+=":";max+=a[2]+48;max+=a[3]+48;max+=":";max+=a[4]+48;max+=a[5]+48;
if(max!=min)printf(",%s\n",max.c_str());
else
printf("\n");
}
}
}
更多追问追答
追问
你这个也不合理。例如输入651345,应该输出13:45:56和16:55:43,但是你的程序输出是N/A
追答
输出na那里有个a0忘记判断了,我回去加一下
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询