PAT乙级1005. 继续(3n+1)猜想 (25)

卡拉兹(Callatz)猜想已经在1001中给出了描述。在这个题目里,情况稍微有些复杂。

当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数。例如对n=3进行验证的时候,我们需要计算3、5、8、4、 2、1,则当我们对n=5、8、4、2进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重复计算,因为这4个数已经在验证3的时候遇到过了,我 们称5、8、4、2是被3“覆盖”的数。我们称一个数列中的某个数n为“关键数”,如果n不能被数列中的其他数字所覆盖。

现在给定一系列待验证的数字,我们只需要验证其中的几个关键数,就可以不必再重复验证余下的数字。你的任务就是找出这些关键数字,并按从大到小的顺序输出它们。

输入格式:每个测试输入包含1个测试用例,第1行给出一个正整数K(<100),第2行给出K个互不相同的待验证的正整数n(1<n<=100)的值,数字间用空格隔开。

输出格式:每个测试用例的输出占一行,按从大到小的顺序输出关键数字。数字间用1个空格隔开,但一行中最后一个数字后没有空格。

输入样例:
6
3 5 6 7 8 11
输出样例:
7 6

【总结】:

 1 #include<stdio.h>
 2 int main() 
 3 {
 4   int n=0,i=0,j=0,temp=0;
 5   scanf("%d",&n);
 6   int a[128];
 7   int fugaishu[5096];
 8   int guanjianshu[128];
 9   int count=0,count1=0,count2=0,count3=0;
10   for(i=0;i<n;i++) {
11     scanf("%d",&a[i]);
12     if(a[i]>1&&a[i]<=100) {
13     fugaishu[count1]=a[i];
14     count1++;
15     }
16   }
17 
18   for(i=0;i<n;i++){
19     int temp=0;
20     if(a[i]>1&&a[i]<=100) {
21       temp=a[i];
22       while(temp!=1) {
23         if(temp%2==0) {
24           temp=temp/2;
25           fugaishu[count1]=temp;
26           count1++;
27         }
28         else {
29           temp=(3*temp+1)/2;
30           fugaishu[count1]=temp;
31           count1++;
32         }
33       }
34     }
35   }
36 
37   for(i=0;i<n;i++){
38     count=0;
39     for(j=0;j<count1;j++){
40       if(fugaishu[j]==a[i]) count++;
41     }
42     if(count==1) {
43       guanjianshu[count2]=a[i];
44       count2++;
45     }
46   }
47 
48   for(i=0;i<count2;i++) {
49     for(j=i+1;j<count2;j++){
50       if(guanjianshu[i]<guanjianshu[j]) {
51         temp=guanjianshu[i];
52         guanjianshu[i]=guanjianshu[j];
53         guanjianshu[j]=temp;
54       }
55     }
56   }
57   for(i=0;i<count2-1;i++) printf("%d ",guanjianshu[i]);
58   printf("%d",guanjianshu[i]);
59   return 0;
60 
61 }

本来是有注释的,但是因为改了很多遍,也有很多方法是错的所以……暂时就这样吧。

排版和算法都不是很好,有空会修改。

 

17.12.9

 1 #include <stdio.h>
 2 //#include <string.h>
 3 
 4 int _tmain(int argc, _TCHAR* argv[])
 5 {
 6     int n=0,i=0,j=0,temp=0;
 7     scanf("%d",&n);
 8     int a[128];    //题目给出数
 9     int fugaishu[2048];    //中间数
10     int guanjianshu[128];    //关键数
11     int count1=0;    //fugaishu[count1]
12     int count2=0;    //guanjianshu[count2]
13 
14     //同时存入a[n],fugaishu[count].
15     for(i=0;i<n;i++) {
16         scanf("%d",&a[i]);
17         fugaishu[i]=a[i];
18     }
19 
20     //把a[n]中的所有数字的小于100的中间数字都存入fugaishu[count1]
21     count1=n;
22     for(i=0;i<n;i++){
23         temp=a[i];
24         while(temp>1){
25             temp=(temp%2==0)?temp/2:(3*temp+1)/2;
26             if(temp<100) {
27                 fugaishu[count1]=temp;
28                 count1++;
29             }
30         }
31     }
32     
33     //把a[n]在fugaishu[count1]中只有一个的数字存入guanjianshu[count2]
34     for(i=0;i<n;i++){
35         temp=0;
36         for(j=0;j<count1;j++){
37             if(fugaishu[j]==a[i]) temp++;
38         }
39         if(temp==1) {
40             guanjianshu[count2]=a[i];
41             count2++;
42         }
43     }
44   
45     //把guanjianshu[count2]排序
46     for(i=0;i<count2;i++) {
47         for(j=i+1;j<count2;j++){
48             if(guanjianshu[i]<guanjianshu[j]) {
49                 temp=guanjianshu[i];
50                 guanjianshu[i]=guanjianshu[j];
51                 guanjianshu[j]=temp;
52             }
53         }
54     }
55     //输出
56     for(i=0;i<(count2-1);i++) printf("%d ",guanjianshu[i]);
57     printf("%d",guanjianshu[i]);
58     return 0;
59 }

总之改了一下,看上去好一些。

 


posted @ 2017-09-18 16:46  水草精  阅读(165)  评论(0编辑  收藏  举报