pat乙级每日习题
时间:2020-03-18 14:06:21
收藏:0
阅读:46
欢迎加入我们:qq群:1054587486
1:https://pintia.cn/problem-sets/994805260223102976/problems/994805325918486528

1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int main(int argc, char const *argv[]) 5 { 6 int n;cin >> n; 7 int ans = 0; 8 while(n != 1){ 9 if((n & 1) == 1){ 10 n = (n * 3 + 1) >> 1; 11 }else{ 12 n >>= 1; 13 } 14 ++ans; 15 } 16 cout << ans << endl; 17 return 0; 18 }
2:https://pintia.cn/problem-sets/994805260223102976/problems/994805320306507776

1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int N = 110; 5 6 int arr[N]; 7 int n; 8 9 int main(int argc, char const *argv[]) 10 { 11 unordered_map<int, bool> hash; 12 13 cin >> n; 14 for(int i = 1;i <= n;++i){ 15 cin >> arr[i];//存进数组因为求答案要用 16 int x = arr[i]; 17 //把x最终变成1的所有中间的数都置为true,代表出现过 18 while(x != 1){ 19 if((x & 1) == 1) //&1 == 1 代表是奇数 20 x = (3 * x + 1) >> 1; 21 else 22 x >>= 1; 23 hash[x] = true; 24 } 25 } 26 27 vector<int> ans; 28 //把数组中的数没有被覆盖过的也就是 false 的加进答案 29 for(int i = 1;i <= n;++i) 30 if(!hash[arr[i]]) 31 ans.push_back(arr[i]); 32 //以下代码完全是为了格式化输出 33 sort(ans.begin(), ans.end(), greater<int>()); 34 for(int i = 0;i < ans.size()-1;++i) 35 cout << ans[i] << " "; 36 cout << ans[ans.size()-1]; 37 cout << endl; 38 39 return 0; 40 }
3:https://pintia.cn/problem-sets/994805260223102976/problems/994805317546655744

1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int N = 1e5; 5 6 int n; 7 bool state[N]; 8 9 int main(int argc, char const *argv[]) 10 { 11 cin >> n; 12 int ans = 0; 13 int pre = -1; 14 for(int i = 2;i <= n;++i){ 15 if(!state[i]){ 16 if(i - pre == 2) 17 ++ans; 18 pre = i; 19 for(int j = i + i; j <= n; j += i) 20 state[j] = true; 21 } 22 } 23 cout << ans << endl; 24 return 0; 25 }
持续更新中...
原文:https://www.cnblogs.com/sxq-study/p/12517031.html
评论(0)