输入数据包含多个用例,每个用例首先包含一个整数n,然后接下来一行有n个整数a[i],其中: 1<=n<=500, 1<a[i]<=1000
请计算并输出数组a中包含的关键数,并按照其输入顺序的逆序输出,每个用例输出占一行。
3 3 8 4 5 3 8 4 7 15 5 3 8 4 15 7 0
3 15 7 3 7 15 3
#include <stdio.h> #define N 500 #define MAX 100000 int NextNum(int x) { if(x%2==0) return x/2; else return (3*x+1)/2; } int main() { int n, buf[N]; bool cover[MAX];//i是否被覆盖 while(scanf("%d", &n)!=EOF) { if(n<=0) break; for(int i=0; i<MAX; i++) cover[i]=false; for(int i=0; i<n; i++) { scanf("%d", &buf[i]); int x=buf[i]; if(cover[x]==false) { while(x!=1) { x=NextNum(x); cover[x]=true; } } } bool isFirst=true; for(int i=n-1; i>=0; i--) { if(cover[buf[i]]==false) { if(isFirst) isFirst=false; else printf(" "); printf("%d", buf[i]); } } printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int i, t, n; vector<int> ans; map<int, bool> mp; while(~scanf("%d", &n) && n){ ans.clear(); mp.clear(); for(i=0; i<n; i++){ scanf("%d", &t); if(mp[t] == false){ ans.push_back(t); //将关键字保留 while(t != 1){ if(t & 1) t = 3 * t + 1; t >>= 1; mp[t] = true; //标记非关键数 } } } t = 0; //标记是否输出空格 for(i=ans.size()-1; i>=0; i--){ if(mp[ans[i]] == false){ //这里再加一层判断 因为可能关键数可能在后面出现 前面某个数被误认为是关键数了 if(t == 1) printf(" "); else t = 1; printf("%d", ans[i]); } } printf("\n"); } return 0; }这个题目,我是看着晕了,因为此题还和前面的题有联系,要想做出此题,还得看前面某题的题干:xxx定律
#include <iostream> #include <string.h> using namespace std; #define MaxN 500 #define MaxV 100000 void generate_cover_num(int k, int cover_num[]) { k = (0 == k % 2) ? k /= 2 : (3 * k + 1) / 2; while (1 != k && !cover_num[k]) { cover_num[k] = 1; k = ((k % 2) ? (3 * k + 1) : k) / 2; } } int main() { int n = 0, a = 0, cnt = 0, cover_num[MaxV] = { 0 }, digits_list[MaxN] = {0}; while (cin >> n && 0 != n) { cnt = 0; memset(cover_num, 0, MaxV * sizeof(int)); while (n-- && cin >> digits_list[cnt]) generate_cover_num(digits_list[cnt++], cover_num); while (cnt--) if (!cover_num[digits_list[cnt]]) { ++n; if (n > 0) cout << ' '; cout << digits_list[cnt]; } cout << endl; } return 0; }
//看了大部分人都用数组记录,我用set记录xxx经过的值。最后,反向遍历输入的数组,无法再set中查到的就输出。 #include<iostream> #include<set> using namespace std; set<int> st; int a[1001]; void xxx(int a){ while(a!=1){ if(a%2==0){a/=2;} else {a=(a*3+1)/2;} st.insert(a); } } int main(){ int n,i,j; while(cin>>n){ if(n==0){break;} st.clear(); for(i=0;i<n;++i){ cin>>a[i]; xxx(a[i]); } bool is_first=true; for(i=n-1;i>=0;--i){ if(st.find(a[i])==st.end()){ if(is_first){cout<<a[i];is_first=false;} else{cout<<" "<<a[i];} } } cout<<endl; } return 0; }
#include<bits/stdc++.h> #define inf 0x3f3f3f3f #define maxsize 100005 #define nextsize 505 typedef long long ll; using namespace std; int n,v[nextsize]; bool a[maxsize]; void f(int m) { if(a[m]) return; while(m!=1) { if(m&1) m=(3*m+1)/2; else m/=2; a[m]=true; } } int main(int argc, char const *argv[]) { #ifdef ONLINE_JUDGE #else freopen("input.txt","r",stdin); #endif while(cin>>n&&!cin.eof()) { if(!n) break; memset(v,0,nextsize*sizeof(int)); memset(a,false,maxsize*sizeof(bool)); for(int i=0; i<n; i++) { cin>>v[i]; f(v[i]); } int first=0; for(int i=n-1; i>=0; i--) { if(!a[v[i]]) { if(!first) first=1; else cout<<" "; cout<<v[i]; } } cout<<endl; } return 0; }
import java.util.*; public class Main { public static void main(String[] args) { ArrayList<Integer> cover = new ArrayList<>(); Scanner in = new Scanner(System.in); while (in.hasNext()) { cover = new ArrayList<>(); int n = in.nextInt(); if (n == 0) break; int[] num = new int[n]; for (int i = 0; i < n; i++) { num[i] = in.nextInt(); } for (int i = 0; i < n; i++) { int n1= num[i]; while(n1!=1){ if(n1%2==0){ n1=n1/2; if(n1!=1){ if(cover.indexOf(n1)==-1){ cover.add(n1); } } }else{ n1=(n1*3+1)/2; if(n1!=1){ if(cover.indexOf(n1)==-1){ cover.add(n1); } } } } } int[] re = new int[n]; int size =0; for(int i=0;i<n;i++){ if(cover.indexOf(num[i])==-1){ re[size++] = num[i]; } } for(int i=size-1;i>=0;i--){ if(i!=0){ System.out.print(re[i]+" "); }else{ System.out.println(re[i]); } } } } }
#include <iostream> #include <vector> using namespace std; int table[1005]; void backup(int i) { if(i==1||i>1000||table[i]==0)return; table[i] = 0; // 被访问 if(i%2==0) backup(i/2); else backup((3*i+1)/2); } int main() { int n; while(cin>>n) { if(n==0)break; fill(table, table+1000, 1); // 假设均未被访问 vector<int> nums(n); for(int i=0;i<n;i++){ cin>>nums[i]; if(table[nums[i]]) { // 如果已被访问则没必要递归 backup(nums[i]); table[nums[i]] = 1; // 原数字标记恢复 } } for(int i=n-1;i>=0;i--) { // 倒序输出 if(table[nums[i]])cout<<nums[i]<<' '; } cout<<endl; } } // 64 位输出请用 printf("%lld")
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = br.readLine()) != null) { int n = Integer.parseInt(s); if (n==0)break; String[] str = br.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(str[i]); } HashSet<Integer> coverset = new HashSet<>(); ArrayList<Integer> outlist = new ArrayList<>(); for (int i = 0; i < n; i++) { int t = a[i]; while (t != 1) { if (t % 2 == 0) { t = t / 2; } else { t = (t * 3 + 1) / 2; } coverset.add(t); } } for (int i = 0; i < n; i++) { if (!coverset.contains(a[i])) outlist.add(a[i]); } for (int i = outlist.size() - 1; i >= 0; --i) { System.out.print(outlist.get(i) + " "); } System.out.println(); } } }
#include <iostream> #include <cstring> using namespace std; int arr[101000] = {0}; int main(){ int n; while(cin >> n){ if(n == 0) break; memset(arr,0,sizeof(arr)); int *temp = new int[n]; for(int i = 0;i < n;i++) cin >> temp[i]; for(int i = 0;i < n;i++){ if(arr[temp[i]] == 0){ int ch = temp[i]; while(ch != 1){ if(ch % 2 == 1){ ch = (ch * 3 + 1) / 2; arr[ch] = 1; }else{ ch /= 2; arr[ch] = 1; } } } } for(int i = n - 1;i >= 0;i--){ if(arr[temp[i]] == 0) cout << temp[i] << " "; } cout << endl; } }
#include <iostream> using namespace std; #include <map> map<int,int> m; int f[500]; int main(){ int n,x; while(cin>>n){ if(n==0)break; for(int i=n-1;i>=0;i--){ cin>>x; f[i]=x; } for(int i=0;i<n;i++){ int c=f[i]; while(c>1){ if(c%2==0){c/=2;m[c]=1;} else { c=3*c+1; c/=2; m[c]=1; } } } for(int i=0;i<n;i++){ if(m[f[i]]!=1)cout<<f[i]<<" "; } cout<<endl; } return 0; }
// 对于一个数 n,如果是偶数,就把 n 砍掉一半;如果是奇数,把 n 变成 3*n+ 1 后砍掉一半,直到该数变为 1 为止。 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 1e6; const int maxa = 510; bool hashTable[maxn];//false为覆盖数, true为关建数 int a[maxa]; void func(int N) { while(N != 1) { if(N % 2 == 0){ N = N / 2; hashTable[N] = false; } else { N = (3 * N + 1) / 2; hashTable[N] = false; } } } int main() { int N; while(cin >> N) { if(N == 0) break; memset(hashTable, true, sizeof(hashTable));//先假设全为关建数 for(int i = 0; i < N; ++i) { cin >> a[i]; func(a[i]);//划去覆盖数 } for(int i = N - 1; i >= 0; --i) { if(hashTable[a[i]] == true) cout << a[i] << " "; } cout << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; map<int,int> M; int main(){ int n,i,temp,a[505]; while(cin>>n){ M.clear(); for(i=0;i<n;i++) cin>>a[i]; for(i=0;i<n;i++){ temp=a[i]; while(temp!=1){ if(temp%2==0) temp/=2; else temp=(3*temp+1)/2; M[temp]++; } } for(i=n-1;i>=0;i--){ if(M[a[i]]==0) cout<<a[i]<<" "; } cout<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; while(cin>>n) { int a[510]; bool coverd[50000]; for(int i=0; i<n; i++) { cin>>a[i]; } for(int i=0; i<50000; i++) { coverd[i]=false; } for(int i=0; i<n; i++) { int tmp=a[i]; if(coverd[tmp]==false) { while(tmp!=1) { if(tmp%2==0) { tmp=tmp/2; if(coverd[tmp]==true) break; coverd[tmp]=true; } else { tmp=(3*tmp+1)/2; if(coverd[tmp]==true) break; coverd[tmp]=true; } } } } // for(int i=0;i<n;i++){ // cout<<coverd[a[i]]<<" "; // } for(int i=n-1; i>=0; i--) { if(!coverd[a[i]]) cout<<a[i]<<" "; } cout<<endl; } return 0; }
#include<iostream> using namespace std; int main(){ int n,a[2][500]; while(cin>>n&&n!=0){ for(int i=0;i<n;i++) { cin>>a[0][i];//输入序列 a[1][i]=0;//初始化 } for(int i=0;i<n;i++){ int x=a[0][i]; while(x!=1){ if(x%2==0){ x=x/2; for(int j=0;j<n;j++){//将覆盖数的标志改为1 if(x==a[0][j]) {a[1][j]=1;} } } else{ x=(3*x+1)/2; for(int j=0;j<n;j++){//将覆盖数的标志改为1 if(x==a[0][j]) {a[1][j]=1;} } } } } //cout<<endl; for(int i=n-1;i>=0;i--){ if(a[1][i]==0) cout<<a[0][i]<<" "; } cout<<endl; } return 0; }
#include <iostream> #include<cmath> using namespace std; class specialnum { public: int value; int tag; }; int judge2(int n) { float t = log(n)/log(2); if(t==int(t)) return 1; else return 0; } int judge(int n,int p) { if(n == 1) { return 0; } while(true) { if(n%2 == 0) { n = n /2; } else { n = n*3+1; n = n /2; } if(n==p) { return 1; } if(n == 1) { break; } } return 0; } int main() { int n = 0; while(cin>>n&&n!=0) { specialnum *numbers = new specialnum[n]; int index = 0; for(int i = 0;i<n;i++) { int tmp;cin>>tmp; if(judge2(tmp)==0) { numbers[index].value = tmp; numbers[index].tag=0; index++; } } for(int h = 0;h<index;h++) { for(int q = 0;q<index;q++) { if(q!=h) { if(judge(numbers[h].value, numbers[q].value)) { numbers[q].tag = 1; } } } } for(int m = index - 1;m>=0;m--) { if(numbers[m].tag==0) { cout<<numbers[m].value<<" "; } } cout<<endl; } }
while True: try: n=int(input().strip()) inp=list(map(int,input().strip().split(' '))) list1=[n] result1=[] result2=[] for i in inp: if i not in result1: while i!=1: if i%2==0: i=i//2 result1.append(i) else: i=(i*3+1)//2 result1.append(i) for i in inp: if i not in result1: result2.append(str(i)) print(' '.join(result2[::-1])) except: break