c语言求幸运数字程序,算法题挑选幸运数字,该如何处理

C/C++ code#include

#include

#include

using namespace std;

struct AssessTypeInfo

{

int array; //输入的幸运数字

int number; //序号

};

bool lessmark(const AssessTypeInfo& s1,const AssessTypeInfo& s2)

{

return s1.number < s2.number;

}

int main()

{

vector ctn;

int n = 0;

int temp = -1;

int i = 0;

bool bRet = false;

scanf("%d", &n);

while(n)

{

AssessTypeInfo a;

for(i=0; i

{

scanf("%d", &a.array);

a.number = i+1;

ctn.push_back(a);

}

sort(ctn.begin(), ctn.end(), lessmark); //升序排序

i = 0;

temp = ctn[i].array;

for(i=1; i

{

if(temp != ctn[i].array)

{

if(bRet)

{

temp = ctn[i].array;

bRet = false;

continue;

}

break;

}

bRet = true;

}

printf("%d %d\n",ctn[i].number,ctn[i].array);

}

return 0;

}

------解决方案--------------------C/C++ code#include

using namespace std;

void getLuckyNumber(int arr[], int n, int *luckyPos, int *luckyNum)

{

int *temp = new int[1000];

memset(temp, 0, 1000 * sizeof(int));

for(int i = 0; i < n; ++i)

{

if(temp[arr[i]] == 0)

temp[arr[i]] = 1;

else

temp[arr[i]] = 2;

}

*luckyNum = 1001;

for(int i = 0; i < 1000; ++i)

{

if(temp[i] == 1)

{

if(i < *luckyNum)

{

*luckyNum = i;

}

}

}

if(*luckyNum == 0)

{

*luckyPos = -1;

return;

}

for(int i = 0; i < n; ++i)

{

if(*luckyNum == arr[i])

{

*luckyPos = i + 1;

break;

}

}

}

int main()

{

int arr[] = {1, 2, 3, 1, 2, 4, 3};

int pos, num;

getLuckyNumber(arr, sizeof(arr) / sizeof(arr[0]), &pos, &num);

cout << pos << endl;

cout << num << endl;

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值