|
查看: 1700|回复: 29
|
C++问题(完)
[复制链接]
|
|
|
怎样把array里的value从小排到大?
怎样把他们soft成小到大?
我要formula罢了。。。
谢谢各位大大。。。
以下是我老师教的。。。
#include<iostream.h>
void main()
{
float m[5]={8,9,1,2,4};
int i,j,temp;
for(i=0;i<=4;i++)
{for(j=i+1;j<=4;j++)
if(m>m[j])
{temp= m;
m = m[j];
m[j] = temp;
}
}
for(i=0;i<=4;i++)
cout<<m;
}
[ 本帖最后由 毛毛人 于 16-4-2009 12:53 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 01:24 PM
|
显示全部楼层
|
bubble sort, insertion sort |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 8-4-2009 01:27 PM
|
显示全部楼层
原帖由 qwe88 于 8-4-2009 01:24 PM 发表 
bubble sort, insertion sort
How? wad coding? i wan C++ only... not java.... |
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 01:27 PM
|
显示全部楼层
#include <stdlib.h>
#include <stdio.h>
#define uint32 unsigned int
typedef int (*CMPFUN)(int, int);
void ArraySort(int This[], CMPFUN fun_ptr, uint32 ub)
{
/* bubble sort */
uint32 indx;
uint32 indx2;
int temp;
int temp2;
int flipped;
if (ub <= 1)
return;
indx = 1;
do
{
flipped = 0;
for (indx2 = ub - 1; indx2 >= indx; --indx2)
{
temp = This[indx2];
temp2 = This[indx2 - 1];
if ((*fun_ptr)(temp2, temp) > 0)
{
This[indx2 - 1] = temp;
This[indx2] = temp2;
flipped = 1;
}
}
} while ((++indx < ub) && flipped);
}
#define ARRAY_SIZE 14
int my_array[ARRAY_SIZE];
void fill_array()
{
int indx;
for (indx=0; indx < ARRAY_SIZE; ++indx)
{
my_array[indx] = rand();
}
/* my_array[ARRAY_SIZE - 1] = ARRAY_SIZE / 3; */
}
int cmpfun(int a, int b)
{
if (a > b)
return 1;
else if (a < b)
return -1;
else
return 0;
}
int main()
{
int indx;
int indx2;
for (indx2 = 0; indx2 < 80000; ++indx2)
{
fill_array();
ArraySort(my_array, cmpfun, ARRAY_SIZE);
for (indx=1; indx < ARRAY_SIZE; ++indx)
{
if (my_array[indx - 1] > my_array[indx])
{
printf("bad sort\n");
return(1);
}
}
}
return(0);
}
|
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 01:30 PM
|
显示全部楼层
sort( r, lo, up )
ArrayToSort r;
int lo, up;
{int i, j;
while (up>lo) {
j = lo;
for ( i=lo; i<up; i++ )
if ( r.k > r[i+1].k ) {
exchange( r, i, i+1 );
j = i;}
up = j;
for ( i=up; i>lo; i-- )
if ( r.k < r[i-1].k ) {
exchange( r, i, i-1 );
j = i;}
lo = j;
}
} |
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 01:31 PM
|
显示全部楼层
Selection sort
void selection Sort ( char *a , int n ){
int min ;
int j , k ;
char curr ;
for ( j =0; j<n ; j++){
min=j ;
for ( k=j ; k<n ; k++)
i f ( a [ k]<a [ min ] ) min=k ;
c u r r=a [ min ] ;
a [j]=c u r r ;
cout<<j<<” : ”<<a<<”\n” ; } // end of loop j
}// end of function
Insertion Sort
void insertionSort ( char *a , i n t n ){
char curr ;
int j , k ;
for ( j =0; j<n ; j++){
curr=a [ j ] ;
for ( k=j ; k>0;k-- ){
i f ( a [ k−1]<=curr ) break ;
a [ k]=a [ k−1] ;} // end o f k loop
a [ k]=c u r r ;
cout<<j<<” : ”<<a<<”\n” ; } // end of loop j
}// end of function
[ 本帖最后由 啊蛋 于 8-4-2009 01:32 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 01:32 PM
|
显示全部楼层
回复 1# 毛毛人 的帖子
瓦老!!, 现在的 学生 真是的
至少 你 Online search 如何做, 然后自己 尝试做 如果 有Bug
才 post 上来。
你哪一间 学院 或 大学 ??  |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 8-4-2009 01:34 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 01:36 PM
|
显示全部楼层
原帖由 cikare1 于 8-4-2009 01:27 PM 发表 
#include
#include
#define uint32 unsigned int
typedef int (*CMPFUN)(int, int);
void ArraySort(int This[], CMPFUN fun_ptr, uint32 ub)
{
/* bubble sort */
uint32 indx;
uint32 ...
原帖由 cikare1 于 8-4-2009 01:30 PM 发表 
sort( r, lo, up )
ArrayToSort r;
int lo, up;
{int i, j;
while (up>lo) {
j = lo;
for ( i=lo; i r.k ) {
exchange( r, i, i+1 );
...
原帖由 啊蛋 于 8-4-2009 01:31 PM 发表 
Selection sort
void selection Sort ( char *a , int n ){
int min ;
int j , k ;
char curr ;
for ( j =0; j
可以的话 给 PSEUDOCODE 就可以了 |
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 01:37 PM
|
显示全部楼层
回复 8# 毛毛人 的帖子
噢噢 那还好 不过 你 老师 没有 教吗??
以前我 中学 学 PHP4, C C++ 和 BASIC |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 8-4-2009 01:38 PM
|
显示全部楼层
原帖由 keatliang2005 于 8-4-2009 01:36 PM 发表 
可以的话 给 PSEUDOCODE 就可以了
对哦...我没想到... |
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 01:41 PM
|
显示全部楼层
回复 11# 毛毛人 的帖子
这样你可以 学 更多  |
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 01:50 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 01:53 PM
|
显示全部楼层
回复 13# qwe88 的帖子
C 和 C++ 那时 不通 PHP 还好 BASIC 呢 不错
不过这些都是 自学的。
不过那时教的 是 VB6 |
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 02:05 PM
|
显示全部楼层
回复 14# keatliang2005 的帖子
oo, 还以为你学校教你酱多语言 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 8-4-2009 04:20 PM
|
显示全部楼层
#include<iostream.h>
void main()
{
float m[5]={8,9,1,2,4};
int i,j,temp;
for(i=0;i<=4;i++)
{for(j=i+1;j<=4;j++)
if(m>m[j])
{temp= m;
m = m[j];
m[j] = temp;
}
}
for(i=0;i<=4;i++)
cout<<m;
}
老师教我了= =lll。。。
paiseh。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 08:03 PM
|
显示全部楼层
回复 14# keatliang2005 的帖子
我中四时 学校教fedora, VI, shell script...
当时我也不知道 是什么东东
在大学 学 Operating System时。。。 才flashback 起来 |
|
|
|
|
|
|
|
|
|
|
发表于 8-4-2009 11:34 PM
|
显示全部楼层
|
你们都好。 我中学学的都是ms word, ms powerpoint, ms excel etc。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 9-4-2009 01:50 AM
|
显示全部楼层
我中学的电脑节都用来看戏 |
|
|
|
|
|
|
|
|
|
|
发表于 9-4-2009 11:16 AM
|
显示全部楼层
我。。。我中学时,根本就没有电脑课。。。
(当时电脑还在用青色monitor呢。。。) |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|