当前位置:网站首页>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 .
边栏推荐
- 【Kotlin学习】类、对象和接口——定义类继承结构
- Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
- 【点云处理之论文狂读经典版8】—— O-CNN: Octree-based Convolutional Neural Networks for 3D Shape Analysis
- Jenkins learning (II) -- setting up Chinese
- LeetCode 508. 出现次数最多的子树元素和
- LeetCode 513. Find the value in the lower left corner of the tree
- 推荐一个 yyds 的低代码开源项目
- [point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis
- State compression DP acwing 91 Shortest Hamilton path
- The difference between if -n and -z in shell
猜你喜欢
数位统计DP AcWing 338. 计数问题
State compression DP acwing 291 Mondrian's dream
Basic knowledge of network security
Redis learning (I)
【点云处理之论文狂读经典版9】—— Pointwise Convolutional Neural Networks
低代码前景可期,JNPF灵活易用,用智能定义新型办公模式
【点云处理之论文狂读前沿版8】—— Pointview-GCN: 3D Shape Classification With Multi-View Point Clouds
On a un nom en commun, maître XX.
LeetCode 515. 在每个树行中找最大值
Digital statistics DP acwing 338 Counting problem
随机推荐
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
AcWing 787. 归并排序(模板)
CSDN markdown editor help document
传统企业数字化转型需要经过哪几个阶段?
Install third-party libraries such as Jieba under Anaconda pytorch
excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
Methods of checking ports according to processes and checking processes according to ports
Recommend a low code open source project of yyds
AcWing 788. 逆序对的数量
Jenkins learning (I) -- Jenkins installation
【Kotlin学习】类、对象和接口——定义类继承结构
精彩回顾|I/O Extended 2022 活动干货分享
C language file reading and writing
LeetCode 715. Range 模块
Simple use of MATLAB
Tag paste operator (#)
剑指 Offer II 029. 排序的循环链表
[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
[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs
LeetCode 513. Find the value in the lower left corner of the tree