编程题由于没法测试所以不知道对错,如有错误希望指正 填空题使用Python和C++来计算,有的地方用Python比C++方便些
答案:624 遍历转换为字符串然后继续遍历找2的个数
num = 0
for i in range(1, 2021):
string = str(i)
for j in range(0, len(string)):
if (string[j] == '2'):
num += 1
print(num)
答案:2481215 辗转相除法
def gys(a, b):
temp = a % b
while (temp != 0):
a = b
b = temp
temp = a % b
return b
num = 0
for i in range(1, 2021):
for j in range(1, 2021):
if (gys(i, j) == 1):
num += 1
print(num)
答案:761 左下走:i + 1, j - 1 右上走:i - 1, j + 1
n = 2
i = 1
j = 2
jud = True
while (i != 20 or j != 20):
if (i == 1 and jud == False):
jud = True
j += 1
n += 1
print(i, j, n)
if (j == 1 and jud == True):
i += 1
jud = False
n += 1
print(i, j, n)
if (jud == False):
i -= 1
j += 1
else:
j -= 1
i += 1
n += 1
print(i, j, n)
答案:8879 没想到特别好的办法,if else解决了
day = 1
week = 6
month = 1
year = 2000
num = 0
while (True):
if (week == 1 or day == 1):
num += 2
else:
num += 1
if (year == 2020 and month == 10 and day == 1):
break
if (month == 2):
if (year % 4 == 0):
if (day == 29):
day = 0
month = 3
else:
if (day == 28):
day = 0
month = 3
elif (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):
if (day == 31):
day = 0
if (month == 12):
month = 1
year += 1
else:
month += 1
else:
if (day == 30):
day = 0
month += 1
day += 1
week = (week + 1) % 7
print(num)
答案:80 并查集 + DFS
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
const int N = 10;
int connect[N][N], num = 0, light[N], swi[N];
int find(int n)
{
if (light[n] == n)
return n;
return find(light[n]);
}
void dfs(int n)
{
if (n > 7) {
for (int i = 0; i <= 7; i++)
light[i] = i;
for (int i = 0; i <= 7; i++) {
for (int j = 0; j <= 7; j++) {
if (swi[i] && swi[j] && connect[i][j]) {
int x = find(i);
int y = find(j);
if (x != y) {
light[x] = y;
}
}
}
}
int k = 0;
for (int i = 0; i <= 7; i++) {
if (swi[i] && light[i] == i)
k++;
}
if (k == 1)
num++;
return;
}
swi[n] = 0;
dfs(n + 1);
swi[n] = 1;
dfs(n + 1);
}
int main()
{
connect[1][2] = connect[1][6] = 1;
connect[2][1] = connect[2][7] = connect[2][3] = 1;
connect[3][2] = connect[3][4] = connect[3][7] = 1;
connect[4][3] = connect[4][5] = 1;
connect[5][4] = connect[5][6] = connect[5][7] = 1;
connect[6][1] = connect[6][5] = connect[6][7] = 1;
dfs(1);
cout << num;
return 0;
}
这个不难,算是水题吧
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int main()
{
int i = 0, n, m;
float x1 = 0, x2 = 0;
cin >> n;
while (i++ < n)
{
cin >> m;
if (m >= 60)
{
x1++;
if (m >= 85)
{
x2++;
}
}
}
float a = x1 / n * 100, b = x2 / n * 100;
float c = int(a) + 0.5, d = int(b) + 0.5;
if (a >= c)
a = int(a) + 1;
else
a = int(a);
if (b >= d)
b = int(b) + 1;
else
b = int(b);
cout << a << "%" << endl
<< b << "%";
return 0;
}
输入n转换为字符串然后判断
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
#include <string>
using namespace std;
int main()
{
int n;
int res1 = INT_MAX, res2;
cin >> n;
string str;
n++;
while (true) {
str = to_string(n);
int i;
for (i = 0; i < 4; i++) {
if (str[i] != str[7 - i])
break;
}
if (i == 4) {
res1 = min(res1, n);
if (str[0] == str[2] && str[1] == str[3]) {
res2 = n;
break;
}
}
n++;
}
cout << res1 << endl
<< res2;
return 0;
}
对每个子字符串区间用哈希表把出现的字符存起来,然后求map.size()。 需要注意的是在区间左边界向右移动时需要把map存的那个值 - 1,然后判断key == 0?。等于0应该把这个key去掉,不然求size会加上value为0的key
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main()
{
map<char, int> mp;
string str;
int num = 0;
cin >> str;
for (int i = 0; i < str.size(); i++)
{
map<char, int> mp;
for (int j = i; j < str.size(); j++)
{
mp[str[j]]++;
num += mp.size();
}
}
cout << num;
return 0;
}
这俩不太会,过后再补