A.判断三角形
C++ 代码
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
int sum = a + b + c;
int max = a > b ? (a > c ? a : c) : (b > c ? b : c);
int min = sum - max;
if (max < min) puts("yes");
else puts("no");
return 0;
}
B.超级楼梯
斐波那契数列
C++ 代码
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 50;
int f[N];
int n, m;
int main()
{
f[1] = 1;
f[2] = 1;
for (int i = 3; i < N; i ++ ) f[i] = f[i - 1] + f[i - 2];
scanf("%d", &n);
while (n -- )
{
scanf("%d", &m);
cout << f[m] << endl;
}
return 0;
}
C.回文数猜想
C++ 代码
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
vector<int> v;
int n, temp;
// 函数功能:判断参数n是否为回文数
// 参数n:需要判断是否为回文数的数
// 参数t:返回值,n的逆序数
bool isPalindrome(int n, int &t)
{
int temp1 = n;
t = 0;
while (n > 0)
{
t = t * 10 + n % 10;
n /= 10;
}
return temp1 == t;
}
int main()
{
while (cin >> n)
{
v.clear();
v.push_back(n);
while (1)
{
if (isPalindrome(n, temp)) break;
else
{
n += temp;
v.push_back(n);
}
}
cout << v.size() - 1 << endl;
for (int i = 0; i < v.size(); i ++ )
{
if (i == 0) printf("%d", v[i]);
else cout << "--->" << v[i];
}
puts("");
}
return 0;
}
D.最简单的计算机
C++ 代码
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int m1, m2, r1, r2, r3;
int main()
{
while (cin >> m1 >> m2)
{
r1 = r2 = r3 = 0;
string str;
cin >> str;
for (int i = 0; i < str.size(); i ++ )
{
char op = str[i];
if (op == 'A') r1 = m1;
else if (op == 'B') r2 = m2;
else if (op == 'C') m1 = r3;
else if (op == 'D') m2 = r3;
else if (op == 'E') r3 = r1 + r2;
else if (op == 'F') r3 = r1 - r2;
}
printf("%d,%d\n", m1, m2);
}
return 0;
}
E.钱币兑换问题
完全背包
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 32768;
int v[] = {1, 2, 3};
int f[N];
int n;
int main()
{
while (cin >> n)
{
memset(f, 0, sizeof f);
f[0] = 1;
for (int i = 0; i < 3; i ++ )
for (int j = v[i]; j <= n; j ++ )
f[j] += f[j - v[i]];
cout << f[n] << endl;
}
return 0;
}
F.确定比赛名次
拓扑排序
C++ 代码
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
#include <functional>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 510;
int n, m, cnt;
int h[N], e[N], ne[N], idx;
int d[N], ans[N]; // q[]存拓扑序列,d[]存每个点的入度
void add(int a, int b) // 添加一条边a->b
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
void topsort()
{
priority_queue<int, vector<int>, greater<int> > q;
// 第一步寻找入度为零的点并插入队列
for (int i = 1; i <= n; i ++ )
if (!d[i])
q.push(i);
while (q.size())
{
int t = q.top(); // 每次取出队头判断
q.pop();
ans[cnt ++ ] = t;
for (int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if (-- d[j] == 0)
q.push(j);
}
}
}
int main()
{
while (~scanf("%d%d", &n, &m))
{
memset(h, -1, sizeof h);
memset(d, 0, sizeof d);
idx = 0;
cnt = 0;
while (m -- )
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
d[b] ++ ; // b点的入度+1
}
topsort();
for (int i = 0; i < cnt; i ++ )
{
if (i != cnt - 1) printf("%d ", ans[i]);
else printf("%d\n", ans[i]);
}
}
return 0;
}
G.找新朋友
欧拉函数
C++ 代码
#include <iostream>
using namespace std;
int phi(int x)
{
int res = x;
for (int i = 2; i <= x / i; i ++ )
if (x % i == 0)
{
res = res / i * (i - 1);
while (x % i == 0) x /= i;
}
if (x > 1) res = res / x * (x - 1);
return res;
}
int main()
{
int n;
cin >> n;
while (n -- )
{
int x;
cin >> x;
cout << phi(x) << endl;
}
return 0;
}
H.破译密码
isupper(int x) 检查x是否为大写字母(A ~ Z)。是,则返回1;不是,则返回0;
对于源码a,加密字母x,密文字母v,根据题意满足:a^x=v,那么有a^x^x=v^x,得a=v^x。对于所有的v,用“A”到“Z”作为x去试算,满足v^x为“A”到“Z”即为加密字母x。
试探法寻找密码用大写字母是需要找到一个字母x,满足对于所有数据计算结果都为大写字母。
C++ 代码
#include <cstdio>
#include <iostream>
#include <cctype>
using namespace std;
const int N = 1010;
int n, v[N], x, j;
int main()
{
while (cin >> n)
{
for (int i = 0; i < n; i ++ ) cin >> v[i];
for (int i = 'A'; i <= 'Z'; i ++ ) // 试探法求加密字母x
{
for (j = 0; j < n; j ++ )
if (!isupper(v[j] ^ i)) // 密文字母v ^ 加密字母x = 原文字母a
break;
if (j == n) // 所有字母都为大写字母则满足题意
{
x = i;
break;
}
}
for (int i = 0; i < n; i ++ ) printf("%c", v[i] ^ x);
puts("");
}
return 0;
}
I.寒冰王座
完全背包
C++ 代码
#include <iostream>
#include <cstring>
using namespace std;
const int N = 10010;
int f[N];
int v[] = {150, 200, 350}, w[] = {150, 200, 350};
int max(int a, int b)
{
return a > b ? a : b;
}
int main()
{
int t;
cin >> t;
while (t -- )
{
memset(f, 0, sizeof f);
int n;
cin >> n;
for (int i = 0; i < 3; i ++ )
for (int j = v[i]; j <= n; j ++ )
f[j] = max(f[j], f[j - v[i]] + w[i]);
cout << n - f[n] << endl;
}
return 0;
}
J.Hat’s IEEE
一道float与double有区别的题目......
输入一个 32-bit 的数 n,输出指数和小数,满足 2^指数 × 小数 = 原数,并且小数的绝对值在 [1.0, 2.0) 的范围内。(注意 n 必须用 float 类型存储)
C++ 代码
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
float n;
int exponent; // 指数
while (cin >> n)
{
exponent = 0;
while (fabs(n) >= 2.0)
{
n /= 2.0;
exponent ++ ;
}
while (fabs(n) < 1.0)
{
n *= 2.0;
exponent -- ;
}
cout << exponent << ' ' << fixed << setprecision(6) << n << endl;
}
return 0;
}