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;
}