PAT乙级1005题解

3n+1plus,直接选择复用1001的代码,没有想到太好的办法就纯模拟的,把所有过程量记录下来最后比对。值得注意的一点是初值不能被记录,否则对比就没有意义了。下面是AC代码

#include <stdio.h>
#include <stdlib.h>
void add(int temp, int *cdd, int *ctcdd){
    int tag = 1;
    for(int i = 0; i < *ctcdd; i++){
        if(temp == cdd[i]){
            tag = 0;
            break;
        }
    }
    if(tag){
        cdd[*ctcdd] = temp;
        (*ctcdd)++;
    }
}

int main(void)
{
    int n;
    scanf("%d", &n);
    int fresh[100];
    int cdd[1000];
    int ctcdd = 0;
    for(int i = 0; i <n; i++){
        scanf("%d", &fresh[i]);
    }
    for(int i = 0; i < n; i++){
        int temp = fresh[i];
        while(temp != 1){
            if(temp % 2){
                temp = (3 * temp + 1) / 2;
            }else{
                temp = temp / 2;
            }
            add(temp, cdd, &ctcdd);//第一个不能加进去
        }
    }
    int key[100];
    int ctkey = 0;
    for(int i = 0; i < n; i++){
        int tag = 1;
        for(int j = 0; j < ctcdd; j++){
            if(fresh[i] == cdd[j]){
                tag = 0;
                break;
            }
        }
        if(tag){
            key[ctkey] = fresh[i];
            ctkey++;
        }
    }
    for(int i = 0; i + 1 < ctkey; i++){
        for(int j = i + 1; j < ctkey; j++){
            if(key[j] > key[i]){
                int temp = key[j];
                key[j] = key[i];
                key[i] = temp;
            }
        }
    }
    printf("%d", key[0]);
    for(int i = 1; i < ctkey; i++){
        printf(" %d", key[i]);
    }
    //printf("\nn = %d\nctcdd = %d\nctkey = %d\n", n, ctcdd, ctkey);
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值