当前位置:网站首页>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 .
边栏推荐
- Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
- Install third-party libraries such as Jieba under Anaconda pytorch
- State compression DP acwing 291 Mondrian's dream
- 【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks
- AcWing 788. Number of pairs in reverse order
- Severity code description the project file line prohibits the display of status error c2440 "initialization": unable to convert from "const char [31]" to "char *"
- 【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
- 状态压缩DP AcWing 91. 最短Hamilton路径
- Memory search acwing 901 skiing
- [point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
猜你喜欢

AcWing 786. Number k

dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article

数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎

一个优秀速开发框架是什么样的?

Jenkins learning (I) -- Jenkins installation

LeetCode 535. TinyURL 的加密与解密

Introduction to the basic application and skills of QT

传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!

【点云处理之论文狂读经典版8】—— O-CNN: Octree-based Convolutional Neural Networks for 3D Shape Analysis

How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?
随机推荐
【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks
2022-2-13 learning the imitation Niuke project - home page of the development community
数字化转型中,企业设备管理会出现什么问题?JNPF或将是“最优解”
[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
LeetCode 535. TinyURL 的加密与解密
教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
LeetCode 241. 为运算表达式设计优先级
2022-2-14 learning the imitation Niuke project - send email
LeetCode 513. 找树左下角的值
Recommend a low code open source project of yyds
Computing level network notes
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
LeetCode 532. K-diff number pairs in array
【点云处理之论文狂读前沿版12】—— Adaptive Graph Convolution for Point Cloud Analysis
[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds
Solve POM in idea Comment top line problem in XML file
LeetCode 30. Concatenate substrings of all words
2022-2-13 learn the imitation Niuke project - Project debugging skills