zoukankan      html  css  js  c++  java
  • 5 c语言数组

    其中包括:冒泡 高精度加法 统计不相同的数字个数(数组) 数组元素倒序输出 go~~~~

     1 #include <stdio.h>
     2 
     3 
     4 /*
     5     功能:冒泡
     6     时间:2016.11.15
     7 */
     8 
     9 void InputArr(int a[],int n)
    10 {
    11     int i;
    12     
    13     for(i=0;i<n;i++)
    14     {
    15         printf("请输入第一个%d元素",i+1);
    16         scanf("%d",&a[i]);
    17 
    18     }
    19     if(i==n)
    20     {
    21         printf("输入完毕了哦
    ");
    22     }
    23     
    24 
    25 
    26 }
    27 void OutArr(int a[],int n)
    28 {
    29     int i;
    30     printf("-------排序后--------
    ");
    31     for(i=0;i<n;i++)
    32     {
    33         printf("%d",a[i]);
    34     }
    35 }
    36 void BubbleSort(int a[],int  n)
    37 {
    38     int i ;
    39     int j;
    40     int temp;
    41     for(i=0;i<n;i++)
    42     {
    43         for(j=0;j<n-i-1;j++)//这里为什么会是n-i-1 冒泡就是相邻两者比较总有个大的在后面 
    44                             //到所谓最后一次之前就确定下来了也就是n-2和n-1比较
    45         {
    46             if(a[j]>a[j+1])
    47             {
    48                 temp = a[j];
    49                 a[j] = a[j+1];
    50                 a[j+1] = temp;
    51             }
    52         }
    53     }
    54 }
    55 void main()
    56 {
    57     int n = 0;
    58     int a[20];//定义最大容量为20的数组
    59     printf("请输入需要排序元素的个数
    ",n);
    60     scanf("%d",&n);
    61     InputArr(a,n);
    62     BubbleSort(a,n);
    63     OutArr( a, n);
    64 
    65     system("pause");
    66 
    67 }

     1 #include <stdio.h>
     2 #include <string.h>
     3 #define M 100
     4 
     5 /*
     6     功能:高精度加法
     7     时间:2016.11.15
     8 */
     9 void main()
    10 {
    11     int a[M] = {0};
    12     int b[M] = {0};
    13     int c[M] = {0};
    14     char s[M+1];
    15     int i = 0;
    16     int n1 = 0;
    17     int n2 = 0;
    18     int max = 0;
    19     int e = 0;
    20     printf("请输入整数a
    ");
    21     gets(s);//从流中取一字符串
    22     n1 = strlen(s);//注意 strlen()这个函数求长度不包括末尾的结束符''
    23     //printf("%d",n1);
    24     for(i=n1-1;i>=0;i--)//数组元素是下标为0哦
    25     {
    26         a[n1-1-i] = s[i] - '0';//数字字符的ASCII-字符0正好为其对应的整数 
    27                                 //比如字符9的ASCII为57 0字符为48相减为9 make it
    28     }
    29     printf("请输入整数b
    ");
    30 
    31     gets(s);
    32     n2 = strlen(s);
    33     for(i=n2-1;i>=0;i--)
    34     {
    35         b[n2-1-i] = s[i] - '0';
    36     }
    37     if(n1>n2)
    38     {
    39         max = n1;
    40     }else
    41     {
    42         max = n2;
    43     }
    44     for(i=0;i<=max;i++)
    45     {
    46         c[i] = (a[i]+b[i]+e)%10;
    47         e = (a[i]+b[i]+e)/10;
    48     }
    49     if(c[max]>0)
    50         printf("%d",c[max]);
    51     for(i=max-1;i>=0;i--)
    52     {
    53         printf("%d",c[i]);
    54     }
    55     system("pause");
    56 }

     1 #include <stdio.h>
     2 
     3 //*
     4 //    功能:数组存储数字 统计不同的数字个数
     5 //    时间:2016.11.16
     6 //*/
     7 
     8 //统计
     9 void Statistic(int a[],int n)
    10 {
    11     int i;
    12     int j;
    13     int count = 0;//统计不同数字的个数
    14     for(i=0;i<n;i++)
    15     {
    16         for(j=0;j<i;j++)
    17         {
    18             if(a[i]==a[j])
    19             {
    20                 break;//退出本次循环
    21             }
    22             
    23         }
    24         if(j==i)//这里为什么是j=i 一旦break 增量j不会再变化 如果没有break即使要完成了循环 变量会增加1
    25         {
    26             count++;
    27         }
    28     }
    29     printf("去掉重复的元素后个数为=%d",count);
    30 }
    31 void Input(int a[],int n)
    32 {
    33     int i;
    34     printf("请输入%d个元素
    ",n);
    35     for(i=0;i<n;i++)
    36     {
    37         scanf("%d",&a[i]);
    38         
    39     }
    40     
    41 }
    42 void main()
    43 {
    44     int a[20];
    45     int n;
    46     printf("需要录入的个数为
    ");
    47     scanf("%d",&n);
    48     Input( a,n);
    49     Statistic(a,n);
    50     system("pause");
    51 }
    52 
    53 //void main()
    54 //{
    55 //    int a[20];
    56 //    int i,t,p=0;
    57 //    for(i=0;i<10;i++)
    58 //    {
    59 //        scanf("%d",&a[i]);
    60 //        for(t=0;t<i;t++)
    61 //        {
    62 //            if(a[t]==a[i])
    63 //                break;//退出本次循环
    64 //        }
    65 //        if(t==i)
    66 //        {
    67 //            p++;
    68 //        }
    69 //        
    70 //    }printf("
    p=%d",p);
    71 //    system("pause");
    72 //}

     1 #include <stdio.h>
     2 
     3 /*
     4     功能:数组元素倒序输出
     5     时间:2016.11.16
     6 */
     7 void rever(int a[],int n)
     8 {
     9     
    10     int i ;
    11     printf("--------倒序输出为----------
    ");
    12     for(i=n-1;i>=0;i--)
    13     {
    14         printf("%x,%d
    ",&a[i],a[i]);
    15     }
    16 }
    17 void main1()
    18 {
    19     int a[4]={1,3,4,5};//定义5个元素的数组
    20     int i;
    21     int count = 0;
    22     printf("----------正序为---------
    ");
    23     for(i=0;i<sizeof(a[i]);i++)//int 4个字节 4个元素
    24     {
    25         printf("%x,%d
    ",&a[i],a[i]);
    26         count++;
    27     }
    28     printf("%d
    ",count);//也就是17个内存单元
    29     rever( a,count);
    30     system("pause");
    31 }
  • 相关阅读:
    [CF1462F] The Treasure of The Segments
    [CF1466E] Apollo versus Pan
    SYZOJ 搭建 Note
    [CF1476D] Journey
    [CF1476E] Pattern Matching
    [CF1494D] Dogeforces
    [CF1383B] GameGame
    [CF1383A] String Transformation 1
    [CF1453D] Checkpoints
    [CF1453C] Triangles
  • 原文地址:https://www.cnblogs.com/lanjianhappy/p/6070463.html
Copyright © 2011-2022 走看看