#include
using namespace std;
int main()
{
int ans = 0;
for(int i = 1; i <= 2020; i ++)
{
int j = i;
while(j)
{
if(j % 10 == 2)
ans ++;
j /= 10;
}
}
cout << ans;
return 0;
}
试题 B: 既约分数
#include
using namespace std;
int gcd(int x, int y)
{
if(y)
return gcd(y, x % y);
else
return x;
}
int main()
{
int ans = 0;
for(int i = 1; i <= 2020; i ++)
for(int j = 1; j <= 2020; j ++)
if(gcd(i, j) == 1)
ans ++;
cout << ans;
}
试题 C: 蛇形填数
#include
using namespace std;
int main()
{
int a[50][50] = {0};
int x = 1, d = 1;
for(int i = 1; i <= 40; i ++)
{
if(d == 1)
{
for(int j = i, k = 1; j >= 1; j --, k ++)
a[j][k] = x ++;
d = -d;
}
else
{
for(int j = 1, k = i; j <= i; j ++, k --)
a[j][k] = x ++;
d = -d;
}
}
cout << a[20][20];
// for(int i = 1; i <= 30; i ++)
// {
// for(int j = 1; j <= 30; j ++)
// cout << a[i][j] << " ";
// cout << endl;
// }
return 0;
}
试题 D: 跑步锻炼
// 太懒不想写
试题 E: 七段码
#include
using namespace std;
int g[10][10], f[10], ans, e[10][10];
bool hh[10];
int find(int x)
{
if(x != f[x])
return f[x] = find(f[x]);
return f[x];
}
void dfs(int u)
{
if(u >= 8)
{
for(int i = 1; i <= 7; i ++)
f[i] = i;
for(int i = 1; i <= 7; i ++)
for(int j = 1; j <= 7; j ++)
if(g[i][j] == 1 && hh[i] && hh[j])
{
int fi = find(i), fj = find(j);
if(fi != fj)
f[fi] = fj;
}
int cnt = 0;
for(int i = 1; i <= 7; i ++)
if(hh[i] && f[i] == i)
cnt ++;
if(cnt == 1)
ans ++;
}
else
{
hh[u] = 1;
dfs(u + 1);
hh[u] = 0;
dfs(u + 1);
}
}
int main()
{
g[1][2] = g[2][1] = 1;
g[1][6] = g[6][1] = 1;
g[6][7] = g[7][6] = 1;
g[2][7] = g[7][2] = 1;
g[6][5] = g[5][6] = 1;
g[5][4] = g[4][5] = 1;
g[4][3] = g[3][4] = 1;
g[2][3] = g[3][2] = 1;
g[3][7] = g[7][3] = 1; // 忘了写
g[5][7] = g[7][5] = 1; //
dfs(1);
cout << ans;
}
试题F: 成绩统计
#include
using namespace std;
const int N = 1e4 + 10;
int a[N], n;
int main()
{
cin >> n;
double jg = 0.0, you = 0.0;
for(int i = 0; i < n; i ++)
{
cin >> a[i];
if(a[i] >= 60)
jg ++;
if(a[i] >= 85)
you ++;
}
int a = (jg / n + 0.005) * 100;
printf("%d%%\n", a);
int b = (you / n + 0.005) * 100;
printf("%d%%", b);
return 0;
}
试题 G: 回文日期
#include
#include
#include
using namespace std;
int n;
int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool run(int x)
{
if(x % 4 == 0 && x % 100 != 0 || x % 400 == 0)
return true;
return false;
}
bool check(int x)
{
int y = x / 10000, m = x / 100 % 100, d = x % 100;
if(m < 1 || m > 12)
return false;
if(m == 2 && run(y))
if(d > month[m] + 1 || d < 1)
return false;
if(d < 1 || d > month[m])
return false;
return true;
}
bool huiwen(int x)
{
string str;
for(int i = 0; i < 8; i ++)
{
str.push_back(x % 10 + '0');
x /= 10;
}
for(int i = 0; i < 4; i ++)
if(str[i] != str[7 - i])
return false;
return true;
}
bool AB(int x)
{
int a = x % 10, b = x / 10 % 10;
int res = a * pow(10, 7) + b * pow(10, 6) + a * pow(10, 5) + b * pow(10, 4) +
b * pow(10, 3) + a * pow(10, 2) + b * pow(10, 1) + a;
if(x == res)
return true;
return false;
}
int main()
{
cin >> n;
int x, y;
// cout << huiwen(20200202) << endl;
for(int i = n + 1; i < 100000000; i ++)
{
if(check(i) && huiwen(i))
{
x = i;
break ;
}
}
for(int i = n + 1; i < 100000000; i ++)
{
if(check(i) && AB(i))
{
y = i;
break ;
}
}
cout << x << endl;
cout << y << endl;
return 0;
}
试题H: 子串分值和
#include
#include
using namespace std;
const int N = 1e5 + 10;
long long int last[30];
char str[N];
int main()
{
cin >> (str + 1);
long long int ans = 0;
long long int n = strlen(str + 1);
for(int i = 1; i <= n; i ++)
{
ans += (i - last[str[i] - 'a']) * (n - i + 1);
last[str[i] - 'a'] = i;
}
cout << ans;
return 0;
}
试题 I: 平面切分
思路:每加入一条直线,如果和之前的直线有交点,则平面数加二,没有交点则加一。判断又有没有交点就是判断斜率是否相同。
输入的数据有可能有重复,必须去重。
#include
#include
#include
using namespace std;
const int N = 1e5 + 10;
int n, ans = 1; // 本来就有一个平面,所以答案初始值是1
pair<double, double> a[N], temp[N]; // 用double读入点 防止出现除法运算精度问题
/*
去重算法 排完序后如果一个数是第一个数或者和前一个不相同 就是唯一元素
*/
void unique()
{
int cnt = 0;
for(int i = 0; i < n; i ++)
if(i == 0 || a[i] != a[i - 1])
temp[cnt ++] = a[i];
n = cnt;
for(int i = 0; i < n; i ++)
a[i] = temp[i];
}
int main()
{
cin >> n;
for(int i = 0; i < n; i ++)
cin >> a[i].first >> a[i].second;
sort(a, a + n);
unique();
for(int i = 0; i < n; i ++)
{
int cnt = 0;
for(int j = 0; j < i; j ++)
{
if(a[i].first == a[j].first) // 斜率相同, 对答案无影响
continue ;
double x = (a[j].second - a[i].second) / (a[i].first - a[j].first);
double y = a[i].first * x + a[i].second;
// (x, y) 是两个直线的交点
// temp 存放所有不同的交点 先把交点放入,循环一遍判断是否重复 是的话长度-1就ok了
temp[cnt ++] = {x, y};
for(int k = 0; k < cnt - 1; k ++)
if(fabs(temp[k].first - x) <= 1e-8 && fabs(temp[k].second - y) <= 1e-8)
{
cnt --;
break ;
}
}
ans += (cnt + 1);
}
cout << ans << endl;
return 0;
}
试题 I: 平面切分
- 不会
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)