题目链接:https://ac.nowcoder.com/acm/contest/9162#question
A. 最短逃生距离
当输入和输出的数据总数超过1000个时,建议不直接用C++的输入输出以免超时。
各种写法
# include <stdio.h>
# include <math.h>
int main() {
int n, m;
scanf("%d%d", &n, &m);
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", abs(x - y));
}
return 0;
} 第二种:如果不懂原理慎用
# include <iostream>
# include <cmath>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n, m;
std::cin >> n >> m;
while (m--) {
int x, y;
std::cin >> x >> y;
std::cout << abs(x - y) << "\n";
}
return 0;
} 第三种:手写输入输出
# include <iostream>
# include <cstdio>
# include <cmath>
template <typename T>
void read(T &val) {
T x = 0;
int bz = 1;
char c;
for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()) ;
if (c == '-') {
bz = -1;
c = getchar();
}
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - 48;
val = x * bz;
}
template <typename T>
void put(T x){
static char ss[20];
int bas;
if(x < 0) {
putchar('-');
x = -x;
}
if(x == (T)(0)) {
putchar('0');
return;
}
bas = 0;
for(;x;x/=10)
ss[bas++] = x % 10 + '0';
for(;bas--;)
putchar(ss[bas]);
}
int main() {
int n, m;
read(n);
read(m);
while (m--) {
int x, y;
read(x);
read(y);
put(abs(x - y));
puts("");
}
return 0;
} 第四种:直接读取数组(如果不懂原理慎用)
# include <iostream>
# include <cstdio>
# include <cmath>
const int BUFSIZE = 20 << 20; //40M
char Buf[BUFSIZE + 1], *buf = Buf;
template<class T>
void read(T &a) {
int sgn = 1;
for(a=0; *buf<'0'||*buf>'9'; buf++) if(*buf=='-') sgn = -1;
while(*buf>='0'&&*buf<='9') {
a=a*10+(*buf-'0');
buf++;
}
a *= sgn;
}
void put(int x) {
static char s[20];
int bas;
if(x < 0) {
putchar('-');
x = -x;
}
if(x == 0) {
putchar('0');
return;
}
bas = 0;
for(; x; x/=10)
s[bas++] = x%10+'0';
for(; bas--;)
putchar(s[bas]);
}
int main() {
fread(Buf, 1, BUFSIZE, stdin);
int n, m;
read(n);
read(m);
while (m--) {
int x, y;
read(x);
read(y);
put(abs(x - y));
printf("\n");
}
return 0;
} B. spj
这题没有啥好说的,举三个例子,还有其他的写法,随便选一种 (出题人写special judge时忘记验证换行,不换行也放过了。这题不是我出的,甩锅)
# include <cstdio>
int main() {
printf("1\n\kcxz");
return 0;
} # include <cstdio>
int main() {
printf("2\n\小少爷\n宇少");
return 0;
} # include <cstdio>
int main() {
printf("2\n宇少\n小少爷");
return 0;
} C. 突然想到一个算法
看见水群里有人斗图时发了这张图片,所以就拿这张图片当热身题。
方法一:,每一步都快速幂即可。复杂度
(斯特林公式)
# include <stdio.h>
const int mod = 1e9 + 7;
int pow(int x, int p) {
int ans = 1 % mod;
while (p) {
if (p & 1) {
ans = 1ll * ans * x % mod;
}
x = 1ll * x * x % mod;
p >>= 1;
}
return ans;
}
void solve() {
int n;
scanf("%d", &n);
int ans = n;
for (int i = 2; i <= n; i++) {
ans = pow(ans, i);
}
printf("%d\n", ans);
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
solve();
}
return 0;
} 方法二:欧拉函数
为质数,所以
和
互质,让阶乘模质数的欧拉函数,再快速幂。复杂度
# include <stdio.h>
const int mod = 1e9 + 7;
int phi;
int qpow(int x, int p) {
int ans = 1 % mod;
while (p) {
if (p & 1) {
ans = 1ll * ans * x % mod;
}
x = 1ll * x * x % mod;
p >>= 1;
}
return ans;
}
int fphi(int n) {
int ans = n;
for (int i = 2; i <= n / i; i++) {
if (n % i == 0) {
ans = ans / i * (i - 1);
while (n % i == 0) {
n /= i;
}
}
}
if (n > 1) {
ans = ans / n * (n - 1);
}
return ans;
}
void solve() {
int n;
scanf("%d", &n);
int ans = 1;
for (int i = 2; i <= n; i++) {
ans = 1ll * ans * i % phi;
}
printf("%d\n", qpow(n, ans));
}
int main() {
int T;
scanf("%d", &T);
phi = fphi(mod);
while (T--) {
solve();
}
return 0;
} D. 兀兀的请求
,
。C语言的库函数里面
写作
。部分编译器
无法通过,需要写成
# include <stdio.h>
# include <math.h>
typedef long long ll;
int main() {
double ans = acos(-1.0);
ans = ans * 100;
for (int i = 3; i <= 14; i++) {
ans = ans * 10;
ll t = ans;
printf("%lld", t % 10);
}
return 0;
} 用参考公式算到第14位极有可能会出现问题

京公网安备 11010502036488号