CPP/计算机组成原理实验代码/OPT3.cpp

89 lines
2.1 KiB
C++
Raw Normal View History

2023-05-12 00:34:15 +08:00
#include <stdio.h>
#define number 11
#define block 3
#include <iostream>
using namespace std;
int opt(int a[])
{
int i, j, zero = 0, one = 0, two = 0, q = 0;
int b[block];
for (i = 0; i < block; i++)
{
b[i] =-1; //<2F><>Ϊ-1
}
for (i = 0; i < number; i++)
{
if (i < 3)
{
// printf("ȱҳ<C8B1><D2B3>%d\n", a[i]);
q++;
for (j = 0; j < block; j++)
{
if (b[j] == -1)
{
b[j] = a[i];
break;
}
}
}
else
{
if (b[0] != a[i] && b[1] != a[i] && b[2] != a[i])
{
// printf("ȱҳ<C8B1><D2B3>%d\n", a[i]);
q++;
for (int t = i + 1; t < number; t++)
{
if (b[0] == a[t])
zero = 1;
else if (b[1] == a[t])
one = 1;
else if (b[2] == a[t])
two = 1;
if (zero == 1 && one == 1 && two == 0)
{
b[2] = a[i];
break;
}
else if (zero == 1 && one == 0 && two == 1)
{
b[1] = a[i];
break;
}
else if (zero == 0 && one == 1 && two == 1)
{
b[0] = a[i];
break;
}
}
}
// else
// printf("%d<><64>ȱҳ<C8B1><D2B3>\n", a[i]);
}
for (int f = 0; f < block; f++)
{
printf("%d\t", b[f]);
}
printf("\n");
zero = 0;
one = 0;
two = 0;
}
return q;
}
int main()
{
int i, o, l=0,f=0;
int a[number];
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n");
for (i = 0; i < number; i++)
{
cin>>a[i];
}
printf("optҳ<EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
o = opt(a);
printf("ȱҳ%d<>Σ<EFBFBD><CEA3>û<EFBFBD>%d<>Ρ<EFBFBD>\n\n\n", o, o - block);
return 0;
}