当前位置:网站首页>Temper cattle ranking problem
Temper cattle ranking problem
2022-07-03 09:16:00 【Fingering 13】
Temper cattle sorting problem
Preface :
**[ Low voice bb]:** Because it's a school exam ,, therefore ... I'm programming for test cases ( A little ~~) Because I'm not very good at , There is a problem with this code , It can only require the number of cattle to be less than 10, And the order of rotation cannot be greater than 2,,emm… And then there is DEV C++ May run incorrectly ( I don't know , Waiting for the boss' advice ...), stay V.C 6.0 No problem .
but, I still smelled shamelessly , After all, I'm very happy .
so, be it so , I think my comments are quite clear .
/* The procedure has great limitations , The number of cows must not exceed 10 only ( It can be modified manually ) , And the order of rotation can only be 1 Once or twice . */
#include<iostream>
using namespace std;
int main()
{
// Enter the number of cattle and array
//i Is a variable that records the number of cycles ,n Is array length
int a[10] = {
0};
int i,n;
cout<<" Please enter the number of cows :" ;
scanf("%d",&n);
cout<<" Please enter the temper value of cattle :";
for (i=0;i<n;i++)
scanf("%d",&a[i]);
/* Give rotation use count Record the first value of each rotation , Use a multiline array cun[jie][shu] To save the rotation value for the subsequent exchange process , Use a single row array wei[weishu] To save the values that have appeared in the rotation Used to judge whether the rotation is over */
int count = 1;
int lun[10];
int cun[10][10]={
0};
int cun1[10]={
0};
int wei[10];
int weishu=0;
int jie=0,shu =0;
// Give rotation , Save the rotation results in cun[jie][shu] And wei[weishu] in .
while(1)
{
int i = 1;
// Using the dead loop , Give the result of rotation in order .
while(1)
{
lun[0] = count ;
lun[i] = a[lun[i-1]-1];
if(lun[1] == count)
break;
cun[jie][shu] = lun[i-1];
wei[weishu] = lun[i-1];
weishu++;shu++;
if(lun[i] == count)
break;
i++;
}
// Yes wei[weishu] Search and check , Back to the count assignment
int s = 1;
int jige = 0;
for(s=1;s<=n;s++)
{
int y=0;
int buxiangdeng=0;
/* take 1 To n Each value of is related to wei[weishu] Compare arrays , If equal Variable jige increase 1, Unequal variables buxiangdeng increase 1 When the cycle is complete buxiangdeng = 10, Numbers s Not in wei[weishu] in So the next one count Namely s if s != 10 ,s It's just wei[weishu] There has been , It's not the next one count once jige Variables and n equal , It means that all the figures are wei[weishu] There has been , End of rotation !!*/
for(y=0;y<10;y++)
{
if(s == wei[y])
{
jige++;
}
else
buxiangdeng++;
}
if(buxiangdeng ==10)
{
count =s; break;
}
}
if(jige==n)
{
break;
}
shu=0; jie++;
}
// Definition n1 And n2 Express The number of numbers in each rotation
int n1=0,n2=0;
for(i=0;i<10;i++)
{
if(cun[0][i] != 0)
n1++;
if(cun[1][i] != 0)
n2++;
}
//daijia1, Calculate the time sequencing cost of not exchanging cattle
int daijiaq=0,daijiah=0;
int daijia1=0,daijia2=0;
for(i=0;i<n1;i++)
daijiaq = daijiaq+cun[0][i];
daijiaq = daijiaq + cun[0][0]*(n1-2);
for(i=0;i<n2;i++)
daijiah = daijiah+cun[1][i];
daijiah = daijiah + cun[1][0]*(n2-2);
daijia1 = daijiaq +daijiah;
//daijia2 , Calculate the time cost of exchanging cattle , When there is only one stage of rotation , There is no need to consider the exchange problem , take daijia2 It's very large
daijiaq=0;daijiah=0;
if(cun[1][0]!=0)
{
for(i=0;i<n1;i++)
daijiaq = daijiaq+cun[0][i];
for(i=0;i<n2;i++)
daijiah = daijiah+cun[1][i];
daijiah = daijiah + cun[0][0]*(n2+n1-1)+cun[1][0];
daijia2 = daijiaq +daijiah;
}
else
daijia2 = 9999999;
// Deal with head changing , And put the first result in cun[0][shu] in , Put the second result Put it in cun1[i] in
// When you need to exchange cows
if(daijia1>daijia2)
{
cout<<" The time cost of sorting is :"<<daijia2<<"\n";
cun1[0] = cun[0][0];
int i=0;
for(i=1;i<=n2;i++)
cun1[i] = cun[1][i-1];
cun1[n2+1] = cun[1][0];
}
// When there is no need to exchange cows
else{
cout<<" The time cost of sorting is :"<<daijia1<<"\n";
int i=0;
for(i=0;i<n2;i++)
cun1[i] = cun[1][i];
}
// Take again n1 ,n2.
n1=0,n2=0;
for(i=0;i<10;i++)
{
if(cun[0][i] != 0)
n1++;
if(cun1[i] != 0)
n2++;
}
// Define two variables diyipai dierpai To find a cow to exchange with a cow
int diyipai = n1,dierpai = n2;
for(i=0;i<n1-1;i++)
{
// shou weiba For the two cows to be exchanged
int shou = cun[0][0];
int weiba =cun[0][diyipai-1];
diyipai--;
// In exchange for
int j=0;
for(j=0;j<n;j++)
{
if(a[j] == shou)
{
a[j] = weiba;continue;
}
if(a[j] == weiba)
{
a[j] = shou;continue;
}
}
// Output the exchange results
int t=0;
for(t=0;t<n;t++)
cout<<a[t]<<" ";
cout<<"\n";
}
// Same as the first row above , I'm too lazy to do it anymore ,,( Brief introduction ~)
for(i=0;i<n2-1;i++)
{
int shou = cun1[0];
int weiba =cun1[dierpai-1];
dierpai--;
// In exchange for
int j=0;
for(j=0;j<n;j++)
{
if(a[j] == shou)
{
a[j] = weiba; continue;
}
if(a[j] == weiba)
{
a[j] = shou; continue;
}
}
// Output the exchange results
int t=0;
for(t=0;t<n;t++)
cout<<a[t]<<" ";
cout<<"\n";
}
}
Um. , It's a long code ( Because I'm stupid ...)
【 Here are the test cases :】
【 ending 】: Mora , Mora , I hope I can help others ( Manual funny ), also , Can the boss tell me , Why is it DEV Some test examples on will run incorrectly .
边栏推荐
猜你喜欢
【点云处理之论文狂读经典版14】—— Dynamic Graph CNN for Learning on Point Clouds
LeetCode 871. 最低加油次数
AcWing 785. 快速排序(模板)
20220630 learning clock in
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
Digital statistics DP acwing 338 Counting problem
LeetCode 532. K-diff number pairs in array
Instant messaging IM is the countercurrent of the progress of the times? See what jnpf says
CSDN markdown editor help document
我们有个共同的名字,XX工
随机推荐
低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis
With low code prospect, jnpf is flexible and easy to use, and uses intelligence to define a new office mode
LeetCode 438. Find all letter ectopic words in the string
npm install安装依赖包报错解决方法
Bert install no package metadata was found for the 'sacraments' distribution
Using DLV to analyze the high CPU consumption of golang process
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
【Kotlin学习】类、对象和接口——定义类继承结构
Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
LeetCode 515. Find the maximum value in each tree row
Linxu learning (4) -- Yum and apt commands
DOM render mount patch responsive system
AcWing 787. Merge sort (template)
The method of replacing the newline character '\n' of a file with a space in the shell
【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
Problems in the implementation of lenet
Vscode connect to remote server
String splicing method in shell
STM32F103 can learning record