hdu1040 As Easy As A+B(C语言)

来源:互联网 发布:轰炸手机号码软件下载 编辑:程序博客网 时间:2024/05/17 09:10
Problem Description
These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.
Give you some integers, your task is to sort these number ascending (升序).
You should know how easy the problem is now!
Good luck!
 

Input
Input contains multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line. 
It is guarantied that all integers are in the range of 32-int.
 

Output
For each case, print the sorting result, and one line one case.
 

Sample Input
23 2 1 39 1 4 7 2 5 8 3 6 9
 

Sample Output
1 2 31 2 3 4 5 6 7 8 9
 

Author
lcy



C语言AC代码
#include<stdio.h>#include<string.h>int main(){    int n,j;    scanf("%d",&n);    for(j=0;j<n;j++)    {        int k,a[10000],i=0,z,t,p;        scanf("%d",&k);z=k;        memset(a,0,z);        while(k--)        {            scanf("%d",&a[i]);            i++;        }        for(i=0;i<z;i++)            for(p=z-1;p>i;p--)            {                if(a[i]>a[p])                {t=a[i];a[i]=a[p];a[p]=t;}            }        for(i=0;i<z;i++)        {            if(i==0)            printf("%d",a[i]);            else                printf(" %d",a[i]);        }        printf("\n");    }    return 0;}
思路:给T行的N个数字升序输出。


顺便记录一下选择排序法和冒牌排序法及相应的优化版。


选择排序法


//交换data1和data2所指向的整形  
void DataSwap(int* data1, int* data2)  
{  
    int temp = *data1;  
    *data1 = *data2;  
    *data2 = temp;  
}  
  
/******************************************************** 
*函数名称:SelectionSort 
*参数说明:pDataArray 无序数组; 
*          iDataNum为无序数据个数 
*说明:    选择排序 
*********************************************************/  
void SelectionSort(int* pDataArray, int iDataNum)  
{  
    for (int i = 0; i < iDataNum - 1; i++)    //从第一个位置开始  
    {  
        int index = i;  
        for (int j = i + 1; j < iDataNum; j++)    //寻找最小的数据索引   
            if (pDataArray[j] < pDataArray[index])  
                index = j;  
  
        if (index != i)    //如果最小数位置变化则交换  
            DataSwap(&pDataArray[index], &pDataArray[i]);  
    }  
}  1从小到大
#include<stdio.h>
int main()
{
int i,j,min,temp,a[11];
for(i=1;i<=10;i++)
scanf("%d",&a[i]);
printf("\n");
for(i=1;i<=9;i++)
{
min=i;
for(j=i+1;j<=10;j++)
if(a[min]>a[j])
min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
for(i=1;i<=10;i++)
printf("%5d",a[i]);
printf("\n");
return 0;
}
2从大到小
#include<stdio.h>
int main()
{
int i,j,min,temp,a[11];
for(i=1;i<=10;i++)
scanf("%d",&a[i]);
printf("\n");
for(i=1;i<=9;i++)
{
min=i;
for(j=i+1;j<=10;j++)
if(a[min]<a[j])
min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
for(i=1;i<=10;i++)
printf("%5d",a[i]);
printf("\n");
return 0;
}
优化版:
void SelectSort(vector<int>& a)
{
    int left = 0;
    int right = a.size() - 1;
    int min = left;//存储最小值的下标
    int max = left;//存储最大值的下标
    while(left <= right)
    {
        min = left;
        max = left;
        for(int i = left; i <= right; ++i)
        {
            if(a[i] < a[min])
            {
                min = i;
            }
            if(a[i] > a[max])
            {
                max = i;
            }
        }
        swap(a[left],a[min]);
        if(left == max)
            max = min;
        swap(a[right],a[max]);


        ++left;
        --right;
    }
}

冒泡排序法
1从小到大A
#include<stdio.h>
int main()
{
int a[10];
int i,j,t;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\n");
for(j=0;j<9;j++)
for(i=0;i<9-j;i++)
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
1从小到大B
#include <stdio.h>  
void swap(int *a, int *b);  
int main()  
{  
    int    array[10];
    int    i, j;  
for(i=0;i<10;i++)
scanf("%d",&array[i]);
    for (i = 0; i < 10; i++)  
    { 
        for (j = 9; j > i; j--)  
        {  
            if (array[j] < array[j-1])  
            {  
                    swap(&array[j], &array[j-1]);  
            }  
        }  
    }  
    for (i = 0; i < 10; i++)  
    {  
        printf("%d ", array[i]);  
    }
printf("\n");
    return    0;  
}  
void swap(int *a, int *b)  
{  
    int    temp;  
    temp = *a;  .
      *a = *b;  
      *b = temp;  
}  
2从大到小
#include<stdio.h>
int main()
{
int a[10];
int i,j,t;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\n");
for(j=0;j<9;j++)
for(i=0;i<9-j;i++)
if(a[i]<a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
for(i=0;i<10;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}

优化版
#include <stdio.h>  
void swap(int *a, int *b);  
int main()  
{  
    int    array[10] = {2, 1, 3, 4, 5, 6, 7, 8, 9, 10};  
    int    i, j;  
    int    flag = 1;   //设置标记变量  
    for (i = 0; i < 10 && flag; i++)  
    {  
        flag = 0;      //只要flag在下一次外循环条件检测的时候值为0,就说明已经排好序,不用继续循环  
        for (j = 9; j > i; j--)  
        {  
            if (array[j] < array[j-1])  
            {  
                swap(&array[j], &array[j-1]);  
                flag = 1;   //如果有交换,就将标记变量赋1  
            }  
        }  
    }  
  
    for (i = 0; i < 10; i++)  
    {  
        printf("%d\n", array[i]);  
    }  
    return    0;  
}  
void swap(int *a, int *b)  
{  
    int    temp;  
    temp = *a;  
      *a = *b;  
      *b = temp;  
}  




原创粉丝点击