P1518
关于效率:
实际上小规模的函数调用,可能因为编译优化而得到更好的效果,同时,通常较大的模块的函数封装意味着更清晰的程序结构。
更能明显降低运行时间的是输入函数的优化。
初试:还有一个将init()
和find()
封装起来的,耗时相当均在180~200 ms。
#include <iostream>
using namespace std;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int fx, fy, fd, cx, cy, cd, cnt = 0;
char map[12][12];
int vis[11][11][11][11];//这个比起特征值来说,空间复杂度更小,但是如果用来判定不满足的情况,会消耗更多的时间。
const int k = 10;
int main()
{
for (int i = 0; i <= k; i++)
map[i][0] = map[0][i + 1] =
map[i + 1][k + 1] = map[k + 1][i] = '*';
for (int i = 1; i <= k; i++)
for (int j = 1; j <= k; j++)
{
cin >> map[i][j];
if (map[i][j] == 'F')
fx = i, fy = j;
else if (map[i][j] == 'C')
cx = i, cy = j;
}
int tmpx, tmpy;
f1:
tmpx = fx + dx[fd], tmpy = fy + dy[fd];
if (map[tmpx][tmpy] == '*')
fd = (fd + 1) % 4;
else
fx = tmpx, fy = tmpy;
tmpx = cx + dx[cd], tmpy = cy + dy[cd];
if (map[tmpx][tmpy] == '*')
cd = (cd + 1) % 4;
else
cx = tmpx, cy = tmpy;
++cnt;
if (++vis[fx][fy][cx][cy] > 4)
{
cout << "0";
return 0;
}
if (cx == fx && cy == fy);
else goto f1;
cout << cnt;
return 0;
}
#include<cstdio>
#include<algorithm>
using namespace std;
const int dx[]={-1,0,1,0};
const int dy[]={0,1,0,-1};
struct one { int x,y,face; };
char map[12][12];
int vis[11][11][11][11];
void operator++(one& a) {
int nx=a.x+dx[a.face];
int ny=a.y+dy[a.face];
if(map[nx][ny]!='\*') a.x=nx,a.y=ny;
else a.face++,a.face%=4;
}
bool operator!=(const one&a,const one&b) {
return a.x!=b.x||a.y!=b.y;
}
int main(){
one jhon,cow;
fill(map[0],map[0]+12,'\*');
fill(map[11],map[11]+12,'\*');
for(int i=1;i<=10;i++) {
scanf("%s\n",&map[i][1]);
map[i][0]=map[i][11]='\*';
for(int j=1;j<=10;j++) {
if(map[i][j]=='C') cow=(one) {i,j,0};
if(map[i][j]=='F') jhon=(one) {i,j,0};
}
}
int ans=0;
while(jhon!=cow) {
if(vis[jhon.x][jhon.y][cow.x][cow.y]++==4) {
printf("0");
return 0;
}
ans++,++jhon,++cow;
}
printf("%d\n",ans);
return 0;
}
使用结构+重载的思路,如果是C语言的输入函数,那么9个点效率会平均提高1 ms,如果使用cin
则相当
P1009
模板题:加法应该相对足够了。乘法待以后优化。
#include <bits/stdc++.h>
using namespace std;
void add(string &str1, string str2)
{
int len1 = str1.size(), len2 = str2.size();
if (len1 > len2)
for (int i = 0; i < len1 - len2; i++)
str2 = "0" + str2;
else if (len1 < len2)
for (int i = 0; i < len2 - len1; i++)
str1 = "0" + str1;
int cb = 0, l = str1.size();
for (int i = l - 1; i >= 0; i--)
{
str1[i] = str1[i] - '0' + str2[i] - '0' + cb;
cb = str1[i] / 10;
str1[i] %= 10;
str1[i] += '0';
}
if (cb)
str1 = char(cb) + str1;
}
void mul(string &str1, string str2)
{
int len1 = str1.size(), len2 = str2.size();
int len = len1 + len2, tmp = 0;
string res;
res.resize(len);
for (int i = 1; i <= len1; i++)
{
int n1 = str1[len1 - i] - '0';
for (int j = 1; j <= len2; j++)
{
tmp = n1 * (str2[len2 - j] - '0');
res[len - i - j + 1] += tmp;
res[len - i - j] += res[len - i - j + 1] / 10;
res[len - i - j + 1] %= 10;
}
}
int s = res.find_first_not_of('\0'), times = res.size() - s;
str1.resize(times);
for (int i = 0; i < times; i++)
str1[i] = res[s+i] + '0';
}
void fact(int n)
{
string ans = "1", tmp = "1";
stringstream ss;
for (int i = 2; i <= n; i++)
{
ss << i;
mul(tmp, ss.str());
add(ans, tmp);
ss.str("");
}
cout << ans;
}
int main()
{
#ifdef _LOC_
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
int tmp;
cin >> tmp;
fact(tmp);
#ifdef _LOC_
fclose(stdin);
fclose(stdout);
#endif
return 0;
}```