Linux学习之路(番外1)

今天学到了一个做了几道题:

       1、在时间复杂度为一的情况下找出数组中重复的数字,其中数组a[n]中的数为1~n-1;

        #include <stdio.h>

        int main()
        {
            int temp;
            int i;
            int a[5]={1,2,4,3,3};
            int count[5]={0};
    
            for (i=0;i<5;i++)
           {
                 temp = a[i];
                 count[temp-1]++;
                 if(count[temp-1]==2)
                 {
                     printf ("%d\n",temp);
                 }

            }

         return 0;

      }

       使用2个数组,第一个放置原数组,即a[n],设置第二个数组count[n],其中元素初始值都为0,对a[n]进行遍历,从a[0]~a[n-1]一一作为count[n]的下标,设置变量temp,使temp=a[n],将(temp-1)填入count[n]中,让count[temp-1]++,一旦count[temp-1]出现2则输出temp;

 

 

     2、只用一个字符串和一个变量,找出一个数组中重复的数字,其中数组a[n]中的数为1~n-1;

       

    #include <stdio.h>

    int a[5]={3,4,1,2,4};
    int temp;
    int swap;
    
   int main()
  {
    
      temp=5;
      while (temp != a[temp-1])
      {
          swap=a[temp-1];
          a[temp-1]=temp;
          temp=swap;
      }
    
      printf ("%d\n",temp);
  }

最后,感谢杰哥!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值