zoukankan      html  css  js  c++  java
  • PAT Basic Level 1005

    思路:设置一个数组读入数据,一次对数组中每一个数据进行操作,设置list 数组,下标作为关键字,内容作为标记,将操作过程中遇到的数字在list中置1,然后再次检索输入数据,看list对应位置是否为1,

    若为1,则被覆盖否则放入输出数组,对输出数组从大到小排序后即为所求者。

    AC代码

     1 #include <stdio.h>
     2 int main ()
     3 {
     4     int ret[102] = {0};
     5     int list[102] = {0};
     6     int input [102] = {0};
     7     int count = 0;
     8     scanf("%d",&count);
     9     int i = count;
    10     while(i != 0)               //将数据读入到input数组中
    11     {
    12         scanf("%d",&input[i]);
    13         i--;
    14     }
    15      i = count;
    16      int x;
    17                                               // x = input[i];      
    18     while(i != 0)
    19     {
    20         x = input[i];
    21         while(x != 1)                  //进行"猜想“,在运算过程中碰到的数字,都将list中相应位置置1
    22         {
    23         
    24             if(x % 2 == 0)
    25                 {
    26                     x = x/2;
    27                 }
    28             else
    29             {
    30                 x = (3*x+1)/2;
    31             }
    32             if (x >= 0&&x <= 100)
    33             {
    34                 list[x] = 1;
    35             }
    36         }
    37         i --;
    38     }
    39     i = 0;
    40     while(count != 0)              //list表中置1的数字都是被覆盖的,若输入的数据在list表中不是1则是索求着
    41     {
    42         if(list[input[count]] != 1)
    43         {
    44             ret[i] = input[count];
    45             i++;
    46         }                        //i最后的值就是ret的长度,即答案的个数
    47         count --;
    48     }
    49     int j;
    50 
    51     for(j = 0;j < i - 1;j++)      //冒泡排序,将ret数组中数据从大到小排练,其实可以使用qsort函数。此处锻炼下自己编程
    52     {
    53         for(x = 0;x < i-j;x++)
    54         {
    55             int temp;
    56             if(ret[x]<ret[x+1])
    57             {
    58                 temp = ret[x+1];
    59                 ret[x+1]=ret[x];
    60                 ret[x] = temp;
    61             }
    62         }
    63     }
    64     for (j = 0;j < i;j++)
    65     {
    66         printf("%d",ret[j]);
    67         if (j != i-1)
    68         {
    69             printf(" ");
    70         } 
    71     }
    72     
    73     return 0 ;
    74     
    75 }
  • 相关阅读:
    node.js 安装后怎么打开 node.js 命令框
    thinkPHP5 where多条件查询
    网站title中的图标
    第一次写博客
    Solution to copy paste not working in Remote Desktop
    The operation could not be completed. (Microsoft.Dynamics.BusinessConnectorNet)
    The package failed to load due to error 0xC0011008
    VS2013常用快捷键
    微软Dynamics AX的三层架构
    怎样在TFS(Team Foundation Server)中链接团队项目
  • 原文地址:https://www.cnblogs.com/Ponytai1/p/5975437.html
Copyright © 2011-2022 走看看