1. 2 的幂
给你一个整数 n,请你判断该整数是否是 2 的幂次方。如果是,返回 true ;否则,返回 false 。
如果存在一个整数 x 使得 n == 2^x ,则认为 n 是 2 的幂次方。
示例 1:
输入:n = 1
输出:true
解释:2^0 = 1
示例 2:
输入:n = 16
输出:true
解释:2^4 = 16
示例 3:
输入:n = 3
输出:false
示例 4:
输入:n = 4
输出:true
示例 5:
输入:n = 5
输出:false
提示:
-2^31 <= n <= 2^31 - 1
进阶:你能够不使用循环/递归解决此问题吗?
代码:
#include <bits/stdc++.h> using namespace std; class Solution{ public: bool isPowerOfTwo(int n){ if (n < 1) return false; while (n%2 == 0) n /= 2; return n == 1; } void PowerOfTwo(int n){ cout << (isPowerOfTwo(n) ? "true" : "false") << endl; } }; int main() { Solution s; s.PowerOfTwo(1); s.PowerOfTwo(16); s.PowerOfTwo(3); s.PowerOfTwo(4); s.PowerOfTwo(5); return 0; }
输出:
true
true
false
true
false
递归法:
#include <bits/stdc++.h> using namespace std; class Solution{ public: bool isPowerOfTwo(int n){ if (n < 1) return false; else if (n == 1) return true; else return n % 2 == 0 && isPowerOfTwo(n / 2); } void PowerOfTwo(int n){ cout << (isPowerOfTwo(n) ? "true" : "false") << endl; } }; int main() { Solution s; s.PowerOfTwo(1); s.PowerOfTwo(16); s.PowerOfTwo(3); s.PowerOfTwo(4); s.PowerOfTwo(5); return 0; }
进阶: 位运算,可以不使用循环/递归
2^n满足条件: n & (n - 1) == 0
#include <bits/stdc++.h> using namespace std; class Solution{ public: bool isPowerOfTwo(int n){ return n > 0 && (n & (n - 1)) == 0; } void PowerOfTwo(int n){ cout << (isPowerOfTwo(n) ? "true" : "false") << endl; } }; int main() { Solution s; s.PowerOfTwo(1); s.PowerOfTwo(16); s.PowerOfTwo(3); s.PowerOfTwo(4); s.PowerOfTwo(5); return 0; }
2. 打印年历
要求:模仿现实生活中的挂历。 当前页以系统当前日期的月份为准显示当前月的每一天(显示出日及对应的星期几)。 当系统日期变到下一月时,系统自动翻页到下一月。
代码:
#include <stdio.h> int year(int y) { if ((y%4==0) && (y%100!=0) || y%400==0) return 366; else return 365; } int main() { int y; int i,j,sum=0; int begin,week; int days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; scanf("%d",&y); for(i=1;i<y;i++) sum+=year(i); week=(sum+1)%7; if(year(y)==366) days[1]=29; printf("\n%d年日历如下:\n",y); for(i=0;i<12;i++) { printf(" %d月 \n",i+1); printf(" 7 1 2 3 4 5 6\n"); printf("=====================\n"); begin=1; for(j=0;j<week;j++) printf(" "); while(begin<=days[i]) { printf("%3d",begin); begin++; week=(week+1)%7; if(week%7==0) printf("\n"); } printf("\n"); } return 0; }
输出:
2023
2023年日历如下:
1月
7 1 2 3 4 5 6
=====================
1 2 3 4 5 6
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
2月
7 1 2 3 4 5 6
=====================
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28
3月
7 1 2 3 4 5 6
=====================
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
4月
7 1 2 3 4 5 6
=====================
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
5月
7 1 2 3 4 5 6
=====================
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
6月
7 1 2 3 4 5 6
=====================
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
7月
7 1 2 3 4 5 6
=====================
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
8月
7 1 2 3 4 5 6
=====================
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
9月
7 1 2 3 4 5 6
=====================
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
10月
7 1 2 3 4 5 6
=====================
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
11月
7 1 2 3 4 5 6
=====================
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
12月
7 1 2 3 4 5 6
=====================
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
3. 路径交叉
给你一个整数数组 distance 。
从 X-Y 平面上的点 (0,0) 开始,先向北移动 distance[0] 米,然后向西移动 distance[1] 米,向南移动 distance[2] 米,向东移动 distance[3] 米,持续移动。也就是说,每次移动后你的方位会发生逆时针变化。
判断你所经过的路径是否相交。如果相交,返回 true ;否则,返回 false 。
示例 1:
输入:distance = [2,1,1,2]
输出:true
示例 2:
输入:distance = [1,2,3,4]
输出:false
示例 3:
输入:distance = [1,1,1,1]
输出:true
提示:
1 <= distance.length <= 10^51 <= distance[i] <= 10^5
代码:
#include <bits/stdc++.h> using namespace std; class Solution { public: bool isSelfCrossing(vector<int> &distance) { int all_step = distance.size(), current_step = 0; if (all_step < 4) return false; current_step = 2; while (current_step < all_step && distance[current_step] > distance[current_step - 2]) current_step++; if (current_step == all_step) return false; if (distance[current_step] >= distance[current_step - 2] - (current_step > 3 ? distance[current_step - 4] : 0)) distance[current_step - 1] -= current_step > 2 ? distance[current_step - 3] : 0; current_step++; while (current_step < all_step && distance[current_step] < distance[current_step - 2]) current_step++; return current_step != all_step; } }; int main() { Solution s; vector<int> nums = {2,1,1,2}; cout << (s.isSelfCrossing(nums) ? "true" : "false") << endl; nums = {1,2,3,4}; cout << (s.isSelfCrossing(nums) ? "true" : "false") << endl; nums = {1,1,1,1}; cout << (s.isSelfCrossing(nums) ? "true" : "false") << endl; return 0; }
输出:
true
false
true


