乙_1005 继续(3n+1)猜想 (25分)
分析: 注意输出空格形式,将容器中其他值设置为0来找出关键字,直接用sort函数排序
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
bool flag=0;
int n ,a;
vector<int> vc;
cin >> n;
for(int i=0;i<n ;i++){
cin >> a;
vc.push_back(a);
}
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
bool flag=0;
int n ,a;
vector<int> vc;
cin >> n;
for(int i=0;i<n ;i++){
cin >> a;
vc.push_back(a);
}
for(int i=0; i<vc.size();i++){
if(vc[i]){
a = vc[i];
while(a!=1){
if(a%2==0)
a=a/2;
else
a=(3*a+1)/2;
for(int j=0; j< n; j++){ // key
if(vc[j]==a&&vc[j]!=0)
vc[j] = 0;
}
}
}
}
sort (vc.begin(),vc.end());
for(int i=n-1; i>= 0; i--){
if(vc[i]){
if(flag) cout << " ";
cout << vc[i];
}
flag =1;
}
if(vc[i]){
a = vc[i];
while(a!=1){
if(a%2==0)
a=a/2;
else
a=(3*a+1)/2;
for(int j=0; j< n; j++){ // key
if(vc[j]==a&&vc[j]!=0)
vc[j] = 0;
}
}
}
}
sort (vc.begin(),vc.end());
for(int i=n-1; i>= 0; i--){
if(vc[i]){
if(flag) cout << " ";
cout << vc[i];
}
flag =1;
}
system("pause");
return 0;
}
return 0;
}