文章目录
- 前言
- 正文
- Day1-104. 货仓选址
- Day2-898 数字三角形
- Day3-756. 蛇形矩阵
- Day4 红与黑
- Day5 1346.回文平方
- Day 6 680. 剪绳子
- Day7 1227.分巧克力
- Day 9 422.校门外的树
- Day10 429.奖学金
- Day11 1208. 翻硬币
- Day12 1532. 找硬币
- Day13 1341. 十三号星期五
- Day14 754. 平方矩阵 II
- Day15 1432. 棋盘挑战
- Day16 1371. 货币系统
- Day17 1381. 阶乘
- Day18 1353. 滑雪场设计
- Day19 1603. 整数集合划分
- Day20 482. 合唱队形
- Day21 420. 火星人
- Day22 1015. 摘花生
- Day23 126. 最大的和
- Day24 703. 数独检查
- Day25 1101. 献给阿尔吉侬的花束
- Day 26 89. a^b
- Day27 433. ISBN号码
- Day28 428. 数列
- Day29 421. 陶陶摘苹果
- 总结
前言
每天一道题,1.9号开始,直到寒假结束。
不摸,从早睡早起开始。(盲猜做不到)
正文
Day1-104. 货仓选址
acw链接
注意这题目的是让货仓位置到各个商店的距离之和最小,并不是实际路程最短。
s
o
r
t
sort
sort以后满足三分性质越往中间
a
n
s
ans
ans变小,然后变大,是单峰凹函数。注意三分模板最后最好求个
m
i
n
min
min稳妥一点。
附上三分模板
本题代码
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn):
const int N = 1e5+5;
int a[N];
int n;
int calc(int x)
{
int res = 0;
rep(i,1,n) res += abs(x - a[i]);
return res;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
cin >> n;
int ans = 0;
rep(i,1,n) cin >> a[i];
sort(a+1,a+1+n);//sort后满足三分性质
int l = a[1], r = a[n];
while(l + 1 < r)
{
int midl = (l + r) / 2;
int midr = (midl + r) / 2;
if(calc(midl) > calc(midr)) l = midl;
else r = midr;
}
cout << min(calc(l),calc(r));
}
Day2-898 数字三角形
acw链接
经典老题不解释,这题刚刚写的时候无意识中自己换了一种dp方式,也说明我的dp越来越熟练了,(其实是这道题越来越熟练了)
另外我又忘了数字三角形里有负数了,忘了MEM(f,-INF)…
const int N = 510;
int a[N][N],f[N][N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int n;
cin >> n;
rep(i,1,n)
{
rep(j,1,i)
{
cin >> a[i][j];
}
}
MEM(f,-INF);
f[1][1] = a[1][1];
rep(i,2,n)
{
rep(j,1,i)
{
f[i][j] = max(f[i-1][j],f[i-1][j-1]) + a[i][j];
}
}
int ans = -INF;
rep(i,1,n) ans = max(ans,f[n][i]);
cout << ans;
}
Day3-756. 蛇形矩阵
acw链接
简单模拟题。这种题还是要亲自手打一遍才行。
这一题有个坑就是会出现数据覆写问题,意思就是如果没有及时跳出,程序就有可能继续填充已经填好的矩阵从而产生错误的答案。处理方法有很多种,比如在
f
o
r
for
for循环里加点判断条件啊什么的,我这里用的是每一次填充完都判断一下是不是写满了if(cnt == n*m+1)break;
写满了就
b
r
e
a
k
break
break掉就好了。
const int N = 110;
int a[N][N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int n,m;
cin >> n >> m;
int cnt = 1;
int shang = 1, xia = n,zuo = 1, you = m;
while(shang <= xia && zuo <= you)
{
for(int i=zuo;i<=you;i++)
{
a[shang][i] = cnt++;
}
shang++;
if(cnt == n*m+1)break;
for(int i=shang;i<=xia;i++)
{
a[i][you] = cnt++;
}
you--;
if(cnt == n*m+1)break;
for(int i=you;i>=zuo;i--)
{
a[xia][i] = cnt++;
}
xia--;
if(cnt == n*m+1)break;
for(int i=xia;i>=shang;i--)
{
a[i][zuo] = cnt++;
}
zuo++;
if(cnt == n*m+1)break;
}
rep(i,1,n)
{
rep(j,1,m)
{
cout << a[i][j] << " \n"[j==m];
}
}
}
Day4 红与黑
acw链接
我是直接
b
f
s
bfs
bfs,另外提供一种
d
f
s
dfs
dfs的写法。
说来奇怪,以前最不会写
b
f
s
bfs
bfs觉得巨难,背模板也背不会。一年过去了,我tm直接会写了,也没有练过,我的手和脑就直接会写了。。看来算法还是要提前学,哪怕学不会说不定过一段时间就自己会了,这是真的。
b
f
s
bfs
bfs版本:
char a[22][22];
bool vis[22][22];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
while (1)
{
MEM(vis,0);
int n, m;
cin >> m >> n;
if(m == 0 || n == 0) return 0;
int bx, by;
rep(i, 1, n)
{
rep(j, 1, m)
{
cin >> a[i][j];
if (a[i][j] == '@')
{
bx = i;
by = j;
}
if (a[i][j] == '#')
{
vis[i][j] = 1;
}
}
}
queue<P> q;
q.push({bx, by});
int cnt = 1;
while (!q.empty())
{
P t = q.front();
int x = t.first, y = t.second;
q.pop();
for (int i = 0; i < 4; ++i)
{
int nx = x + dt[i][0];
int ny = y + dt[i][1];
if (x < 1 || x > n || y < 1 || y > m) continue;
if (!vis[nx][ny] && a[nx][ny] == '.')
{
q.push({nx, ny});
++cnt;
vis[nx][ny] = 1;
}
}
}
cout << cnt << endl;
}
}
d f s dfs dfs版本(注意体会区别,另外想想递归出口的事):
#include<iostream>
#include<cstring>
using namespace std;
const int N=30;
char g[N][N];
int n,m;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
bool st[N][N];
int dfs(int x,int y)
{
st[x][y]=true;
int cnt=1;
for(int i=0;i<4;i++)
{
int a=x+dx[i],b=y+dy[i];
if(a<0 || a>=n || b<0 || b>=m) continue;
if(st[a][b]) continue;
if(g[a][b]=='#') continue;
cnt+=dfs(a,b);
}
return cnt;
}
int main()
{
while(cin>>m>>n,n||m)
{
memset(st,0,sizeof st);
for(int i=0;i<n;i++) scanf("%s",g[i]);
int x,y,flag=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
if(g[i][j]=='@')
{
x=i,y=j;
flag=1;
}
if(flag) break;
}
cout<< dfs(x,y) <<endl;
}
return 0;
}
Day5 1346.回文平方
acw链接
简单模拟
注意
g
e
t
2
get2
get2函数得到的是反过来的,需要
r
e
v
e
r
s
e
reverse
reverse一下。
int B;
string get1(int x)
{
string r;
if(x <= 9) r = x +'0';
else r = x - 10 + 'A';
return r;
}
string get2(int x)
{
string res = "";
while(x)
{
int temp = x % B;
res += get1(temp);
x /= B;
}
return res;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
cin >> B;
rep(i,1,300)
{
string s1 = get2(i * i);
string s2 = s1;
reverse(s1.begin(),s1.end());
if(s1 == s2)
{
string ans1 = get2(i);
reverse(ans1.begin(),ans1.end());
cout << ans1 << " " << s1 << endl;
}
}
}
Day 6 680. 剪绳子
acw链接
简单题,浮点数二分
const int N = 1e5+5;
ll L[N];
int n,m;
bool calc(double x)
{
ll cnt = 0;
rep(i,1,n)
{
cnt += int(L[i] / x);
}
if(cnt >= m) return true;
else return false;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
cin >> n >> m;
ll mx = -INF;
rep(i,1,n)
{
cin >> L[i];
mx = max(mx,L[i]);
}
double l = 0.0,r = (double)mx;
while(l + eps < r)
{
double mid = (double)(l+r)/2.0;
if(calc(mid)) l = mid;
else r = mid;
}
cout << fixed << setprecision(2) << l;
}
Day7 1227.分巧克力
acw链接
整数二分
另附模板:
bool check(int x) {/* ... */} // 检查x是否满足某种性质
// 区间[l, r]被划分成[l, mid]和[mid + 1, r]时使用:
int bsearch_1(int l, int r)
{
while (l < r)
{
int mid = l + r >> 1;
if (check(mid)) r = mid; // check()判断mid是否满足性质
else l = mid + 1;
}
return l;
}
// 区间[l, r]被划分成[l, mid - 1]和[mid, r]时使用:
int bsearch_2(int l, int r)
{
while (l < r)
{
int mid = l + r + 1 >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
作者:yxc
链接:https://www.acwing.com/blog/content/277/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
本题代码:
const int N = 1e5+5;
P c[N];
int n,k;
bool calc(int x)
{
int cnt = 0;
rep(i,1,n)
{
int cnt1 = c[i].first / x;
int cnt2 = c[i].second / x;
cnt += cnt1 * cnt2;
}
if(cnt >= k) return true;
return false;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
cin >> n >> k;
rep(i,1,n) cin >> c[i].first >> c[i].second;
int l = 0, r = INF;
while(l < r)
{
int mid = l + r + 1>> 1;
if(calc(mid)) l = mid;
else r = mid - 1;
}
cout << l;
}
Day 9 422.校门外的树
acw链接
两种做法,比较慢的做法是直接模拟 (但是它好写啊) ,比较快的做法是进行区间合并。
下面贴出两种做法的代码:
直接模拟——
O
(
M
L
)
O(ML)
O(ML) :
const int N = 10010;
int n, m;
bool st[N];
int main()
{
scanf("%d%d", &m, &n);
while (n -- )
{
int l, r;
scanf("%d%d", &l, &r);
for (int i = l; i <= r; i ++ ) st[i] = true;
}
int res = 0;
for (int i = 0; i <= m; i ++ )
if (!st[i])
res ++ ;
printf("%d\n", res);
return 0;
}
区间合并—— O ( M l o g M ) O(MlogM) O(MlogM)
typedef pair<int, int> PII;
const int N = 110;
int n, m;
PII seg[N];
int main()
{
cin >> m >> n;
for (int i = 0; i < n; i ++ ) cin >> seg[i].first >> seg[i].second;
sort(seg, seg + n);
int res = m + 1;
int st = seg[0].first, ed = seg[0].second;
for (int i = 1; i < n; i ++ )
if (seg[i].first <= ed) ed = max(seg[i].second, ed);
else
{
res -= ed - st + 1;
st = seg[i].first, ed = seg[i].second;
}
res -= ed - st + 1;
cout << res << endl;
return 0;
}
Day10 429.奖学金
struct node
{
int id;
int yu,shu,ying;
int sum;
bool operator <(const node& b)const{
if(sum == b.sum && yu == b.yu) return id < b.id;
else if(sum == b.sum) return yu > b.yu;
return sum > b.sum;
}
}a[310];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int n;
cin >> n;
rep(i,1,n)
{
cin >> a[i].yu >> a[i].shu >> a[i].ying;
a[i].id = i;
a[i].sum = a[i].yu + a[i].shu + a[i].ying;
}
sort(a+1,a+1+n);
rep(i,1,5)
{
cout << a[i].id << " " << a[i].sum << endl;
}
}
Day11 1208. 翻硬币
acw链接
直接递推模拟就行了,我以为是什么dp。
但是你要仔细想一想为什么这样子一定是最优的,难道不会出现:从后面先翻硬币比从前往后一次翻硬币要更优吗?
其实,可以这样思考,对于第一个位置,它只能翻,因为后面的不会影响他,同理第1个位置翻好了,那第二个位置也这样想就行了。
那这样子看来,这题所说的"最优"实际上是唬人的,对于早已固定的
s
、
t
s、t
s、t,它需要翻转的次数也是固定的,那为什么还说是求"最优呢"?难道还能出现不是最优的?其实"非最优"就是很傻的翻过来再翻过去再翻过来,平常人肯定不会这么干。
const int N = 110;
int dp[N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
string s,t;
cin >> s >> t;
int n = s.size();
int ans = 0;
rep(i,0,n-2)
{
if(s[i] == t[i]) continue;
++ans;
s[i] = (s[i] == '*')?'o':'*';
s[i+1] = (s[i+1] == '*')?'o':'*';
}
if(s[n-1] != t[n-1]) cout << ans+1;
else cout << ans;
}
Day12 1532. 找硬币
acw链接
本来想枚举每一个数然后二分取找
m
−
a
[
i
]
m-a[i]
m−a[i],这样子是
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)的,显然可以。
后来想了一下其实并不需要这样,只需要用
m
a
p
map
map存一下就行了,不用二分就能很好的找到,而且好写。但有一个坑,就是加入只有1个7,然后
m
=
14
m=14
m=14,这样子应该输出
N
o
S
o
l
u
t
i
o
n
No Solution
NoSolution而不是
7
,
7
7,7
7,7。
const int N = 1e5+5;
int a[N];
map<int,int>mp;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int n,m;
cin >> n >> m;
rep(i,1,n)
{
cin >> a[i];
mp[a[i]]++;
}
sort(a+1,a+1+n);
rep(i,1,n)
{
int k = m - a[i];
if(mp[k])
{
if(k == a[i])
{
if(mp[k] > 1)
{
cout << a[i] << " " << k;
return 0;
}
}
else
{
cout << a[i] << " " << k;
return 0;
}
}
}
cout << "No Solution";
}
Day13 1341. 十三号星期五
acw链接
模拟,还好我有板子嘎嘎嘎
int ReturnWeekDay( unsigned int iYear, unsigned int iMonth, unsigned int iDay )
{
int iWeek = 0;
unsigned int y = 0, c = 0, m = 0, d = 0;
if ( iMonth == 1 || iMonth == 2 )
{
c = ( iYear - 1 ) / 100;
y = ( iYear - 1 ) % 100;
m = iMonth + 12;
d = iDay;
}
else
{
c = iYear / 100;
y = iYear % 100;
m = iMonth;
d = iDay;
}
iWeek = y + y / 4 + c / 4 - 2 * c + 26 * ( m + 1 ) / 10 + d - 1; //蔡勒公式
iWeek = iWeek >= 0 ? ( iWeek % 7 ) : ( iWeek % 7 + 7 ); //iWeek为负时取模
if ( iWeek == 0 ) //星期日不作为一周的第一天
{
iWeek = 7;
}
return iWeek;
}
int d[10];
int main()
{
int n;
cin >> n;
rep(i,1900,1900+n-1)
{
rep(j,1,12)
{
int nd = ReturnWeekDay(i,j,13);
++d[nd];
}
}
cout << d[6] << " " << d[7] << " ";
rep(i,1,5) cout << d[i] << " ";
}
Day14 754. 平方矩阵 II
acw链接
模拟
const int N = 110;
int a[N][N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int n;
while(cin >> n && n)
{
rep(i,1,n)//(i,i)为对角
{
int k = 1;
rep(j,i,n)
{
// cout << "de: "<< i << " " << j << endl;
a[i][j] = k;
a[j][i] = k++;
}
}
rep(i,1,n)
{
rep(j,1,n)
{
cout << a[i][j] << " \n"[j==n];
}
}
cout << endl;
}
}
Day14 754. 平方矩阵 II
const int N = 110;
int a[N][N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int n;
while(cin >> n && n)
{
rep(i,1,n)//(i,i)为对角
{
int k = 1;
rep(j,i,n)
{
// cout << "de: "<< i << " " << j << endl;
a[i][j] = k;
a[j][i] = k++;
}
}
rep(i,1,n)
{
rep(j,1,n)
{
cout << a[i][j] << " \n"[j==n];
}
}
cout << endl;
}
}
Day15 1432. 棋盘挑战
哈哈,N皇后,一生之敌。
链接
constexpr int N = 105;
int cnt,n;
bool vis1[N],vis2[N],vis3[N];
int a[N];
void dfs(int ceng)
{
if(ceng == n + 1)
{
++cnt;
if(cnt <= 3)
{
rep(i,1,n) cout << a[i] << " ";
cout << endl;
}
return;
}
rep(j,1,n)
{
if(!vis1[j] && !vis2[ceng + j] && !vis3[ceng - j + n])
{
vis1[j] = 1;
vis2[ceng + j] = 1;
vis3[ceng - j + n] = 1;
a[ceng] = j;
dfs(ceng + 1);
vis1[j] = 0;
vis2[ceng + j] = 0;
vis3[ceng - j + n] = 0;
}
}
}
int main()
{
cin >> n;
dfs(1);
cout << cnt;
}
Day16 1371. 货币系统
这题务必掌握,"恰好背包"的一种。
链接
const int N = 1e4+4;
ll dp[N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int v,n;
cin >> n >> v;
dp[0] = 1;
rep(i,1,n)
{
int w;
cin >> w;
rep(j,w,v)
{
dp[j] += dp[j-w];
}
}
cout << dp[v];
}
Day17 1381. 阶乘
链接
这题注意2比5多就行了。
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int n;
cin >> n;
int cnt2 = 0,cnt5 = 0;
ll fac = 1;
rep(i,1,n)
{
ll tmp = i;
while(!(tmp % 2)) tmp /= 2, ++cnt2;
while(!(tmp % 5)) tmp /= 5, ++cnt5;
fac = fac * tmp % 1010;
}
//肯定是2比5多,所以把不能消0的全部加回来
int nn = cnt2 - cnt5;
rep(i,1,nn)
{
fac = fac * 2 % 1010;
}
cout << fac % 10;
}
Day18 1353. 滑雪场设计
链接
直接枚举最大高度maxh是一个很好的思路。
const int N = 1010;
int a[N];
int n;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
cin >> n;
rep(i,1,n) cin >> a[i];
ll cost = 0x3f3f3f3f3f3f3f3f;
rep(maxh,17,100)
{
ll res = 0;
rep(j,1,n)
{
if(a[j] > maxh) res += pow((a[j] - maxh), 2);
else
{
if(maxh - a[j] > 17) res += pow((maxh - 17 - a[j]), 2);
}
}
cost = min(cost,res);
}
cout << cost;
}
Day19 1603. 整数集合划分
const int N = 1e5+5;
int a[N];
bool cmp(int a,int b)
{
return a > b;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int n;
cin >> n;
rep(i,1,n) cin >> a[i];
int sum = 0;
sort(a+1,a+1+n,cmp);
if(n & 1)
{
rep(i,1,n)
{
if(i <= n / 2 + 1) sum += a[i];
else sum -= a[i];
}
cout << 1 << " " << sum;
}
else
{
rep(i,1,n)
{
if(i <= n / 2) sum += a[i];
else sum -= a[i];
}
cout << 0 << " " << sum;
}
}
Day20 482. 合唱队形
const int N = 110;
int a[N];
int dp[N],dp2[N],dp3[N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int n;
cin >> n;
rep(i,1,n) cin >> a[i];
rep(i,1,n)
{
dp[i] = 1;
rep(j,1,i)
{
if(a[i] > a[j])
dp[i] = max(dp[i],dp[j] + 1);
}
}
per(i,n,1)
{
dp2[i] = 1;
per(j,n,i)
{
if(a[i] > a[j])
dp2[i] = max(dp2[i],dp2[j] + 1);
}
}
rep(i,1,n)
{
dp3[i] = dp[i] + dp2[i] - 1;
}
int ans = 0;
rep(i,1,n)
{
ans = max(ans,dp3[i]);
}
cout << n - ans;
}
Day21 420. 火星人
链接
我直接库函数了。。。
const int N = 1e4+4;
int a[N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int n,m;
cin >> n >> m;
rep(i,1,n) cin >> a[i];
while(m--)
{
next_permutation(a+1,a+1+n);
}
rep(i,1,n) cout << a[i] << " ";
}
Day22 1015. 摘花生
const int N = 110;
int dp[N][N],a[N][N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int T;
cin >> T;
while(T--)
{
MEM(dp,0);
MEM(a,0);
int n,m;
cin >> n >> m;
rep(i,1,n) rep(j,1,m) cin >> a[i][j];
dp[1][1] = a[1][1];
rep(i,1,n)
{
rep(j,1,m)
{
int k = max(dp[i-1][j],dp[i][j-1]) + a[i][j];
dp[i][j] = max(dp[i][j],k);
}
}
cout << dp[n][m] << endl;
}
}
Day23 126. 最大的和
使用
d
p
dp
dp思路的话,还是挺好的
d
p
dp
dp题目的,会收录到《每日
d
p
dp
dp》里面
链接
const int N = 110;
int a[N][N], sum[N][N], dp[N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int n;
cin >> n;
rep(i, 1, n)
{
rep(j, 1, n)
{
cin >> a[i][j];
sum[i][j] = sum[i][j - 1] + a[i][j];
}
}
int ans = -INF;
rep(j, 1, n)
{
rep(k, 0, j - 1) //列数k~j 枚举区间
{
rep(i, 1, n)
{
if (dp[i - 1] > 0)
dp[i] = dp[i - 1] + (sum[i][j] - sum[i][k]);
else
dp[i] = (sum[i][j] - sum[i][k]);
ans = max(ans, dp[i]);
}
}
}
cout << ans;
}
Day24 703. 数独检查
链接
不知道为啥我的程序本低跑是对的,交上去疯狂segment fault,心态小炸,找了份别人的代码贴上去了。
#include<iostream>
#include<cstring>
using namespace std;
int t,n;
const int N=100;
int g[N][N];
bool st[N];
bool check(){
for(int i=0;i<n*n;i++){//判断每行是否合法
memset(st,0,sizeof st);
for(int j=0;j<n*n;j++){
if(g[i][j]>n*n||g[i][j]<1)return false;
if(st[g[i][j]])return false;
st[g[i][j]]=true;
}
}
for(int i=0;i<n*n;i++){//判断每列是否合法
memset(st,0,sizeof st);
for(int j=0;j<n*n;j++){
if(g[j][i]>n*n||g[j][i]<1)return false;
if(st[g[j][i]])return false;
st[g[j][i]]=true;
}
}
for(int x=0;x<n*n;x+=n){//判断每个n*n矩阵是否合法
for(int y=0;y<n*n;y+=n){
memset(st,0,sizeof st);
for(int dx=0;dx<n;dx++){
for(int dy=0;dy<n;dy++){
int t=g[x+dx][y+dy];
if(t<1||t>n*n)return false;
if(st[t]) return false;
st[t] = true;
}
}
}
}
return true;
}
int main(){
cin>>t;
for(int i=1;i<=t;i++){
cin>>n;
memset(g,0,sizeof g);
for(int i=0;i<n*n;i++)
for(int j=0;j<n*n;j++)
cin>>g[i][j];
if(check())cout<<"Case #"<<i<<": Yes"<<endl;
else cout<<"Case #"<<i<<": No"<<endl;
}
return 0;
}
Day25 1101. 献给阿尔吉侬的花束
const int N = 210;
char a[N][N];
int d[N][N];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//IOS;
int T;
scanf("%d", &T);
while (T--)
{
int n, m;
scanf("%d%d", &n, &m);
MEM(d, INF);
rep(i, 0, n - 1) scanf("%s", a[i]);
int sx, sy, ex, ey;
rep(i, 0, n - 1)
{
rep(j, 0, m - 1)
{
if (a[i][j] == 'S')
sx = i, sy = j;
if (a[i][j] == 'E')
ex = i, ey = j;
}
}
queue<pii> q;
d[sx][sy] = 0;
a[sx][sy] = '#';
q.push({sx, sy});
while (q.size())
{
auto t = q.front();
q.pop();
for (int i = 0; i < 4; ++i)
{
int x = t.first + dt[i][0];
int y = t.second + dt[i][1];
if (x < 0 || x >= n || y < 0 || y >= m || a[x][y] == '#')
continue;
d[x][y] = d[t.first][t.second] + 1;
a[x][y] = '#'; //标记已经走过!
q.push({x, y});
}
}
if (d[ex][ey] == INF)
cout << "oop!" << endl;
else
cout << d[ex][ey] << endl;
}
}
Day 26 89. a^b
#define int long long
int a,b,p;
int qpow(int a,int b,int p)
{
int res = 1;
while(b)
{
if(b & 1) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
signed main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
// int a,b,p;
cin >> a >> b >> p;
if(b == 0)
{
cout << 1 % p;
}
else
cout << qpow(a,b,p);
}
Day27 433. ISBN号码
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
string s;
cin >> s;
string res = "";
rep(i, 0, s.size() - 3)
{
if (s[i] >= '0' && s[i] <= '9')
{
res += s[i];
}
}
ll ans = 0;
int cnt = 0;
rep(i, 0, res.size() - 1)
{
ans = (ans + (i+1) * (res[i] - '0')) % 11;
// cout << "i: " << i << " " << res[i] << endl;
}
// cout << "De: " << ans << endl;
char mo;
if (ans == 10)
mo = 'X';
else
mo = ans + '0';
if (mo == s[s.size() - 1])
cout << "Right";
else
{
rep(i, 0, s.size() - 2) cout << s[i];
cout << mo;
}
}
Day28 428. 数列
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int k,n;
cin >> k >> n;
int res = 0,base = 1;
while(n)
{
res += n % 2 * base;
base *= k;
n /= 2;
}
cout << res;
}
Day29 421. 陶陶摘苹果
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int a[11];
rep(i,1,10) cin >> a[i];
int h;
cin >> h;
h += 30;
int ans = 0;
rep(i,1,10)
{
if(a[i] <= h) ++ans;
}
cout << ans;
}
总结
暂无。