当前位置:网站首页>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 .
边栏推荐
- Beego learning - JWT realizes user login and registration
- Vscode connect to remote server
- Using variables in sed command
- 传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
- Tag paste operator (#)
- Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
- 状态压缩DP AcWing 291. 蒙德里安的梦想
- 【点云处理之论文狂读经典版8】—— O-CNN: Octree-based Convolutional Neural Networks for 3D Shape Analysis
- What are the stages of traditional enterprise digital transformation?
- 数位统计DP AcWing 338. 计数问题
猜你喜欢

On February 14, 2022, learn the imitation Niuke project - develop the registration function

AcWing 786. Number k

State compression DP acwing 91 Shortest Hamilton path

AcWing 785. Quick sort (template)

Redis learning (I)

LeetCode 1089. 复写零
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years

npm install安装依赖包报错解决方法

Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!

【点云处理之论文狂读经典版7】—— Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs
随机推荐
2022-2-14 learning xiangniuke project - generate verification code
Find the combination number acwing 886 Find the combination number II
[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs
Tree DP acwing 285 A dance without a boss
On a un nom en commun, maître XX.
传统企业数字化转型需要经过哪几个阶段?
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
LeetCode 30. 串联所有单词的子串
[untitled] use of cmake
Divide candy (circular queue)
The less successful implementation and lessons of RESNET
Install third-party libraries such as Jieba under Anaconda pytorch
AcWing 787. Merge sort (template)
LeetCode 715. Range module
LeetCode 871. Minimum refueling times
Too many open files solution
【点云处理之论文狂读经典版8】—— O-CNN: Octree-based Convolutional Neural Networks for 3D Shape Analysis
Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
2022-1-6 Niuke net brush sword finger offer
Solve POM in idea Comment top line problem in XML file