今天来谈谈冒泡,这可不是QQ群冒泡,呵呵!标题改成“魔炮”,比较时尚,是吧!忽悠园友一次。
冒泡排序的关键代码
scores是一个存放数据的数组。
很难理解,我就给个例子吧!code如下:
冒泡排序的关键代码
for(i=0;i<scores.Length-1;i++)
{
for(j=0;j<scores.Length-1-i;j++)
{
if(scores[j]>scores[j+1])
{
temp=scores[j];
scores[j]=scores[j+1];
scores[j+1]=temp;
}
}
}
Console.WriteLine("排序");
for(i=0;i<scores.Length;i++)
{
Console.Write("{0}\t",scores[i])
}
Console.ReadLine();
{
for(j=0;j<scores.Length-1-i;j++)
{
if(scores[j]>scores[j+1])
{
temp=scores[j];
scores[j]=scores[j+1];
scores[j+1]=temp;
}
}
}
Console.WriteLine("排序");
for(i=0;i<scores.Length;i++)
{
Console.Write("{0}\t",scores[i])
}
Console.ReadLine();
scores是一个存放数据的数组。
很难理解,我就给个例子吧!code如下:
int[] price=new int[];
int i,j,temp;
int sum=0;
string[] name=new string[7]{"CPU","内存","硬盘","显卡","光驱","主机","显示器"};
for(i=0;i<=6;i++)
{
Console.Write("请输入{0}价格:\t",name[i]);
price[i]=int.parse(Console.ReadLine());
}
for(i=0;i<price.Length-1;i++)
{
for(price[j]>price[j+1])
{
if(price[j]>price[j+1])
{
temp=price[j];
price[j]=price[j+1];
price[j+1]=temp;
}
}
}
Console.WriteLine("DIY电脑计算价格报价单");
for(i=o;i<price.Length;i++)
{
Console.WriteLine("{0}价格:{1}\t",name[i],price[i]);
sum=sum+price[i];
}
Console.WriteLine("电脑总价格:"+sum);
Console.ReadLine();
code是手写的,未调试的,有错还请园友提出哦!int i,j,temp;
int sum=0;
string[] name=new string[7]{"CPU","内存","硬盘","显卡","光驱","主机","显示器"};
for(i=0;i<=6;i++)
{
Console.Write("请输入{0}价格:\t",name[i]);
price[i]=int.parse(Console.ReadLine());
}
for(i=0;i<price.Length-1;i++)
{
for(price[j]>price[j+1])
{
if(price[j]>price[j+1])
{
temp=price[j];
price[j]=price[j+1];
price[j+1]=temp;
}
}
}
Console.WriteLine("DIY电脑计算价格报价单");
for(i=o;i<price.Length;i++)
{
Console.WriteLine("{0}价格:{1}\t",name[i],price[i]);
sum=sum+price[i];
}
Console.WriteLine("电脑总价格:"+sum);
Console.ReadLine();