#include
#include
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
string str;
cin >> str;
stack s;
for(int i = 0; i < str.size(); i++) {
if(s.empty()) s.push(str[i]);
else if(s.top() == '[' && str[i] == ']') s.pop();
else if(s.top()=='(' && str[i]==')') s.pop();
else
s.push(str[i]);
}
if(s.empty()) cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
#include
int main() {
int n, i;
while(scanf("%d", &n) != EOF) {
i = 0;
while(n != 1) {
if(n%2 != 0) {
n = 3*n + 1;
i++;
} else {
n = n / 2;
i++;
}
}
printf("%d\n", i%3);
}
return 0;
}
#include
#include
int main() {
int t;
scanf("%d", &t);
getchar();
while(t--) {
char s[10005], k[15];
scanf("%s%s", k, s);
int lens = strlen(s), lenk = strlen(k), count = 0;
for(int i = 0; i < lens; i++) {
int temp = 1;
for(int j = 0; j < lenk; j++)
if(s[i+j] != k[j]) temp = 0;
count += temp;
}
printf("%d\n", count);
}
return 0;
}
解法:勾股定理,即每个圆所能覆盖的也就里面的一个矩形而已,将里面的矩形分成4个来看,半径则为三角形的斜边,高为1是一条边,所求a[i]为长这条边,因每个矩形包括两个长的这条边,所以l=20/2,只用算总长一半就行。
代码:
#include
#include
#include
using namespace std;
int main() {
int m;
scanf("%d", &m);
while(m--) {
int n, sum = 0;
scanf("%d", &n);
double a[n], l = 20 / 2;
for(int i = 0; i < n; i++) {
scanf("%lf", &a[i]);
a[i] = sqrt(a[i]*a[i] - 1);//勾股定理
}
sort(a, a+n);
int k = n-1;
while(l >= 0) {
l -= a[k--];//先放范围大的
sum++;
}
printf("%d\n", sum);
}
return 0;
}
解法:二维的投影成一维,住户到邮局距离最小,投影到x和y轴上也就是住户到邮局的x距离和y距离都要最小(构成的斜边也就是两点距离最小) 。
代码:
#include
#include
int find(int a[], int n) {
int min = 0, c[25];
for(int i = 0; i < n; i++) {
c[i] = 0;
for(int j = 0; j < n; j++) {
c[i] += abs(a[j] - a[i]);//i轮流作为邮局点,数组c来统计去i点的距离总和
}
}
for(int i = 0; i < n; i++) {
if (c[i] < c[min]) min = i;//比较出最小距离总和
}
return c[min];
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
int a[25], b[25], n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d%d", &a[i], &b[i]);
printf("%d\n", find(a, n) + find(b, n));
}
return 0;
}
#include
#include
using namespace std;
struct cfx {
int n;
int l;
int w;
} a[1005];
int cmp(cfx x,cfx y) {
if(x.n < y.n) return 1;
if(x.n == y.n && x.l < y.l) return 1;
if(x.n == y.n && x.l == y.l && x.w < y.w) return 1;
else
return 0;
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
int m, b, c, i;
scanf("%d", &m);
for(i = 0; i < m; i++) {
scanf("%d%d%d", &a[i].n, &b, &c);
if(b > c) {
a[i].l = b;
a[i].w = c;
} else {
a[i].l = c;
a[i].w = b;
}
}
sort(a, a+m, cmp);
for(i = 0; i < m-1; i++)
if(a[i+1].n == a[i].n && a[i+1].l == a[i].l && a[i+1].w == a[i].w) a[i].n = -1;
for(i = 0; i < m; i++)
if(a[i].n != -1) printf("%d %d %d\n",a[i].n, a[i].l, a[i].w);
}
return 0;
}
#include
int fib(int n) {
int f1, f2, f;
if (n == 1 || n == 2) return 1;
f1 = f2 = 1;
for (int i = 3; i <= n; i++) {
f = f1 + f2;
f1 = f2;
f2 = f;
}
return f;
}
int main() {
int m;
scanf("%d", &m);
while(m--) {
int n, f, f1, f2;
scanf("%d", &n);
f = fib(n);
printf("%d\n", f);
}
return 0;
}
#include
#include
int max(int x, int y) {
return x > y ? x : y;
}
int main() {
int t;
scanf("%d", &t);
getchar();
while(t--) {
int dp[10005] = {0};
char a[10005];
scanf("%s", a);
for(int i = 0; i < strlen(a); i++) {
dp[i] = 1;
for(int j = 0; j < i; j++)
if(a[i] > a[j]) dp[i] = max(dp[i], dp[j]+1);
}
int max = 0;
for(int i = 0; i < strlen(a); i++)
if(dp[i] > max) max = dp[i];
printf("%d\n", max);
}
return 0;
}
#include
#include
using namespace std;
int dp[105][105];
int main() {
int n;
while(cin >> n) {
int a[105][105];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= i; j++)
cin >> a[i][j];
for(int i = 1; i <= n; i++)
dp[n][i] = a[n][i];
for(int i = n-1; i >= 1; i--)
for(int j = 1; j <= i; j++)
dp[i][j] = max(dp[i+1][j], dp[i+1][j+1]) + a[i][j];
cout << dp[1][1] << endl;
}
return 0;
}
解法:DFS。
#include
#include
int a[10], b[10], n, m;
void dfs(int pn, int pm) {
if(!pm) {
for(int i = m; i >= 1; i--)
printf("%d", b[i]);
printf("\n");
return;
} else {
for(int i = 1; i <= n; i++) {
if(a[i] != 1) {
b[pm] = i;
a[i] = 1;
dfs(pn, pm-1);
a[i] = 0;
}
}
}
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
memset(a, 0, sizeof(a));
dfs(n, m);
}
return 0;
}
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static Vector[] m;
static Queue q;
static int[] pre;
public static InputReader in = new InputReader(new BufferedInputStream(System.in));
public static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt(), pos = in.nextInt();
pre = new int[n + 1];
m = new Vector[n + 1];
for (int i = 0; i < n + 1; i++) {
m[i] = new Vector<>();
}
q = new LinkedList<>();
for (int i = 0; i < n - 1; i++) {
int a = in.nextInt(), b = in.nextInt();
m[a].add(b);
m[b].add(a);
}
bfs(pos);
for (int i = 1; i <= n; i++) {
out.print(pre[i] + " ");
out.flush();
}
out.println();
out.flush();
}
}
private static void bfs(int pos) {
pre[pos] = -1;
q.add(pos);
while (!q.isEmpty()) {
int k = q.poll();
for (int i = 0; i < m[k].size(); i++) {
if (pre[m[k].get(i)] == 0) {
pre[m[k].get(i)] = k;
q.add(m[k].get(i));
}
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
}
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, m;
scanf("%d%d", &n, &m);
if(n < m) printf("Win\n");
else if(n % (m + 1) == 0) printf("Lose\n"); //给对手留下 m+1 的倍数 则会赢
else
printf("Win\n");
}
return 0;
}
#include
int primer(int t) {
for(int j = 2; j*j <= t; j++) {
if(t % j == 0) {
return 0;
}
}
return 1;
}
int main () {
int n;
scanf("%d", &n);
while(n--) {
int m, i, j, big, small;
scanf("%d", &m);
if(m == 0) printf("2 2\n");
else if(1 == m) printf("2 1\n");
else if(2 == m) printf("2 0\n");
else {
if(primer(m)) {//若输入的数为素数
printf("%d 0\n", m);
} else { //若输入的数不是素数
for(i = m+1; ; i++) {
if(primer(i)) {
big = i;
break;
}
}
for(i = m-1; ; i--) {
if(primer(i)) {
small = i;
break;
}
}
if(m-small <= big-m) printf("%d %d\n", small, m-small);
else
printf("%d %d\n", big, big-m);
}
}
}
return 0;
}
#include
int main() {
int n;
scanf("%d", &n);
while(n--) {
int a[3][3];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
scanf("%d", &a[i][j]);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++)
printf("%d ", a[j][i]);
printf("\n");
}
printf("\n");
}
return 0;
}
#include
int n, r;
int num[15];//用数组来存某次循环时的几位数
void dfs(int a, int b) {
if(b == 0) {//b递减到0,数组中有b个数存放着,停止递归,开始输出
for(int i = r; i > 0; i--)
printf("%d", num[i]);
printf("\n");
} else {
for(int i = a; i >= b; i--) {
num[b] = i;//b个数;第一次循环是第一个数,为最大值,后递减
dfs(i-1, b-1);//递归,同时改变a与b的值,依次用数组存放第n个数
}
}
}
int main() {
scanf("%d%d", &n, &r);
dfs(n, r);
return 0;
}
#include
int main() {
int s;
int a, b, c, f = 0;
scanf("%d%d%d", &a, &b, &c);
for(s = 10; s <= 100; s++) {
if((a==s%3)&&(b==s%5)&&(c==s%7)) {
f = 1;
}
if(f == 1)break;
}
if(f == 1)printf("%d", s);
else
printf("No answer");
return 0;
}
#include
#include
#include
using namespace std;
int dp[1005][1005];
int main() {
int t;
cin >> t;
while(t--) {
char str1[1005], str2[1005];
cin >> str1 >> str2;
int n = strlen(str1), m = strlen(str2);
for(int i = 0; i < n; i++)
dp[i][0] = 0;
for(int j = 0; j < m; j++)
dp[0][j] = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(str1[i-1] == str2[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
else
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
}
}
cout << dp[n][m] << endl;
}
return 0;
}
#include
int gcd(int m, int n) {
if(m % n == 0)
return n;
else
return gcd(n,m%n);
}
int main() {
int m, n, N;
scanf("%d", &N);
while(N--) {
scanf("%d%d", &m, &n);
printf("%d %d\n", gcd(m, n), m * n / gcd(m, n));
}
return 0;
}
#include
using namespace std;
int ditu[1005][1005] = {0}, tot, p;
int vis[1005] = {0};
void dfs(int k) {//从1号顶点开始dfs
vis[k] = 1;
tot++;
for(int i = 1; i <= p; i++)
if(ditu[k][i] == 1 && !vis[i]) dfs(i);
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
memset(ditu, 0, sizeof(ditu));
memset(vis, 0, sizeof(vis));
int q, a[1005] = {0}, d1, d2, sum = 0;
scanf("%d%d", &p, &q);
tot = 0;
for(int i = 0; i < q; i++) {
scanf("%d%d", &d1, &d2);
ditu[d1][d2] = ditu[d2][d1] = 1;
a[d1]++;
a[d2]++;
}
for(int i = 1; i <= p; i++)
if(a[i] % 2) sum++;
if(sum == 0 || sum == 2) {
dfs(1);
if(tot == p) printf("Yes\n");
else
printf("No\n");
}else{
printf("No\n");
}
}
return 0;
}
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int a, n, max, sum = 0;
scanf("%d", &n);
scanf("%d", &a);
max = a; //先定义初始最大值为第一个数
for(int i = 1; i < n; i++) {
sum += a;//sum先加上第一个数因为后续同一变量输入会覆盖前面的数,开头数字为负或者正数加了一个负数,使sum可能为负
if(sum > max) max = sum;//更新最大值
else if(sum < 0) sum = 0;//相加的过程中遇到负数使sum变小不要紧,一旦sum为负则重新开始计和(sum为负则之前一串的数都可以不用再加来参与计和了)
scanf("%d", &a);
}
printf("%d\n", max);
}
return 0;
}
DP做法:
#include
#include
using namespace std;
int a[1000002] = {0}, dp[1000002] = {0};//dp[i]表示取第i个元素时子串的和,即考虑a[i]是作为最大串的开头元素还是结尾元素
int main() {
int n,m;
scanf("%d", &n);
while (n--) {
scanf("%d", &m);
for (int i = 0; i < m; i++)
scanf("%d", &a[i]);
dp[0] = a[0];//初始化dp为第一项最大
for (int i = 1; i < m; i++) {
dp[i] = max(a[i], dp[i-1]+a[i]);//max(让a[i]为新起点重新组串,a[i]与前面的串组合在一起)
}
sort(dp, dp + m);//排序后取最后一个即是最大的子串和
printf("%d\n", dp[m - 1]);
}
return 0;
}
#include
#include
#include
using namespace std;
int dp[30005];
int main() {
int t;
cin >> t;
while(t--) {
memset(dp, 0, sizeof(dp));
int v[30], w[30], a[30], n, m;
cin >> n >> m;
for(int i = 0; i < m; i++) {
cin >> v[i] >> w[i];
a[i] = v[i] * w[i];
}
for(int i = 0; i < m; i++)//外层是物品
for(int j = n; j >= v[i]; j--)//里层是资金,算每个资金能包含多少物品
dp[j] = max(dp[j-v[i]] + a[i], dp[j]);//拿v[i]跟不拿比较dp大小
cout << dp[n] << endl;
}
return 0;
}
解法:贪心策略,排序后,先统计完一个区间的,再通过改变后一区间的起点或终点来统计后面的。
吐槽:新版OJ好像不能编译algorithm了......
#include
using namespace std;
struct moveq {
int start, end;
}s[105];
bool cmp(moveq a, moveq b) {
if(a.start < b.start) return true;
else if(a.start == b.start && a.end > b.end) return true;
else
return false;
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
int l, m, count = 0;
scanf("%d%d", &l, &m);
for(int i = 0; i < m; i++)
scanf("%d%d", &s[i].start, &s[i].end);
sort(s, s+m, cmp);
int max = -1;//不断变换终点位值,定义初始终点位置为-1,使得第一区间能统计到
for(int i = 0; i < m; i++) {
if(s[i].end < max) continue;//如果后一区间的终点位置小于前一区间终点位置,则后一区间不需要重复算了
if(s[i].start > max) count += s[i].end - s[i].start + 1;//如果后一区间的起点大于前一区间终点位置,则直接算后一区间的
else
count += s[i].end - max;
max = s[i].end;//不断更新为前一区间的终点位置
}
printf("%d\n", l - count + 1);
}
return 0;
}
区间数组做法:
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int l, m, sum = 0, a[10005] = {0};
scanf("%d%d", &l, &m);
while(m--) {
int start, end;
scanf("%d%d", &start, &end);
for(int i = start; i <= end; i++)
a[i] = 1;
}
for(int i = 0; i <= l; i++)
if(a[i] == 0) sum++;
printf("%d\n", sum);
}
return 0;
}
#include
int main() {
int n;
scanf("%d", &n);
while(n--) {
int a[7], b[7], k, max = 0, t = 0;
for(int i = 0; i < 7; i++) {
scanf("%d%d", &a[i], &b[i]);
if(a[i] + b[i] > max) {
k = i + 1;
max = a[i] + b[i];
}
if(a[i] + b[i] > 8) t = 1;
}
if(t == 0) printf("0\n");
else
printf("%d\n", k);
}
return 0;
}
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int sum = 0, a[15], k, temp = 0, flag = 0;
for(int i = 0; i < 12; i++)
scanf("%d", &a[i]);
for(int i = 0; i < 12; i++) {
temp = temp + 300 - a[i];
if(temp < 0) {
flag = 1;
k = i + 1;
break;
} else {
sum += temp / 100;
temp = temp % 100;
}
}
if(flag) printf("-%d\n", k);
else
printf("%.lf\n", (double)(sum * 120 + temp));
}
return 0;
}
#include
#include
using namespace std;
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, s[4], k, count = 1;
scanf("%d", &n);
k = n;
while(k != 6174) {//由题目给出的特定值,并不是所有数都能结束循环
s[0] = k % 10;
s[1] = k % 100 / 10;
s[2] = k / 100 % 10;
s[3] = k / 1000;
sort(s, s + 4);
int a = 0, b = 0;
for(int i = 3; i >= 0; i--) {
a = a * 10 + s[i];
}
for(int i = 0; i < 4; i++) {
b = b * 10 + s[i];
}
k = a - b;
count++;
if(k == n) break;
}
printf("%d\n", count);
}
return 0;
}
#include
#include
using namespace std;
int dir[4][2]= {0, 1, 0, -1, 1, 0, -1, 0}, x1, y1, x2, y2, minn, sum;
int visited[110][110]; // 访问标记
int map[9][9] = {1,1,1,1,1,1,1,1,1,
1,0,0,1,0,0,1,0,1,
1,0,0,1,1,0,0,0,1,
1,0,1,0,1,1,0,1,1,
1,0,0,0,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,0,0,0,1,
1,1,1,1,1,1,1,1,1
};
int CheckEdge(int x, int y) {
if(!visited[x][y] && !map[x][y] && x > 0 && x <= 9 && y > 0 && y <= 9) return 1;
else
return 0;
}
void dfs(int x, int y) {
if(x == x2 && y == y2) {//到达终点
if(sum < minn) minn = sum;
return;
}
if(!visited[x][y]) {//该点没被搜索过
visited[x][y] = 1;
sum++;
for(int i=0; i<4; i++)
if(CheckEdge(x+dir[i][0], y+dir[i][1])) dfs(x+dir[i][0],y+dir[i][1]);// 按照规则生成下一个节点
visited[x][y] = 0;//四个方向都不能走,该点为死路
sum--;
}
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &x1, &y1);
scanf("%d%d", &x2, &y2);
memset(visited, 0, sizeof(visited));
minn = 81;
sum = 0;
dfs(x1, y1);
printf("%d\n", minn);
}
return 0;
}
#include
struct student {
char name[25];
int score;
int bscore;
char g;
char b;
int l;
int sum;
} st[105];
int main() {
int n;
scanf("%d", &n);
while(n--) {
int x;
scanf("%d", &x);
getchar();
int max = 0, zong = 0, t = x;
while(x--) {
scanf("%s%d%d", st[x].name, &st[x].score, &st[x].bscore);
getchar();
scanf("%c %c%d", &st[x].g, &st[x].b, &st[x].l);//要想达到多组数组输入,即使两个字符输入时有空格了,在代码时两个字符输入之间必须多一个空格
getchar();
st[x].sum = 0;
if(st[x].score > 80 && st[x].l >= 1) st[x].sum += 8000;
if(st[x].score > 85 && st[x].bscore > 80) st[x].sum += 4000;
if(st[x].score > 90) st[x].sum += 2000;
if(st[x].score > 85 && st[x].b == 'Y') st[x].sum += 1000;
if(st[x].bscore > 80 && st[x].g == 'Y') st[x].sum += 850;
zong += st[x].sum;
if(st[x].sum > max) {
max = st[x].sum;
t = x;//记录最高奖学金学生的下标
}
}
printf("%s\n%d\n%d\n", st[t].name, st[t].sum, zong);
}
return 0;
}
#include
#include
#include
using namespace std;
bool prime(int n) {
if(n <= 1) return false;
for(int i = 2; i * i <= n; i++)
if(n % i == 0) return false;
return true;
}
int main() {
int t;
scanf("%d", &t);
getchar();
while(t--) {
int maxn = 0, minn = 0, a[26] = {0};
char s[100];
gets(s);
for(int i = 0; i < strlen(s); i++) {
a[(s[i] - '0') - 49]++;//先变成字母的ASCII码
}
sort(a, a + 26);
maxn = a[25];
for(int i = 0; i < 26; i++) {
if(a[i] != 0) {
minn = a[i];
break;
}
}
if(prime(maxn - minn)) {
printf("Lucky Word\n%d\n", maxn - minn);
} else {
printf("No Answer\n0\n");
}
}
return 0;
}
#include
#include
int main() {
int x1, x2, x3, y1, y2, y3;
while(~scanf("%d%d%d%d%d%d", &x1, &y1, &x2, &y2, &x3, &y3)) {
double a1, a2, a3, s, p;
if(x1==0&&x2==0&&x3==0&&y1==0&&y2==0&&y3==0) break;
a1 = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
a2 = sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3));
a3 = sqrt((x3-x2)*(x3-x2) + (y3-y2)*(y3-y2));
p = (a1 + a2 + a3) / 2;
s = sqrt(p * (p - a1) * (p - a2) * (p - a3));
printf("%.1lf\n", s);
}
return 0;
}
#include
#include
int main() {
int n;
scanf("%d", &n);
while(n--) {
int N;
double t = 0;
scanf("%d", &N);
for(int i = 1; i <= N; i++)
t += log10(i);
printf("%d\n", (int)t + 1);
}
return 0;
}
题目意思:
n的阶乘中最多能分解出多少个m相乘
例如:n=100 m=5;
100当中有
5,10,15,20,25,30,35,40,45,50,
55,60,65,70,75,80,85,90,95,100
这些数能够整除5,其中25,50,75,100能够整除5^2,即能够整除25,因此可以分解出2个5。又因为第一遍时已经加过一次了,所以最多能够分解出20+4个5。
#include
int main() {
int s;
scanf("%d", &s);
while(s--) {
int n, m, sum = 0;
scanf("%d%d", &n, &m);
while(n) {
sum += n / m;//计算n中有多少个能整除m的数;
n /= m; //计算n中有多少个能够整除m^2的数;
}
printf("%d\n",sum);
}
return 0;
}
#include
#include
using namespace std;
int main() {
int t;
scanf("%d", &t);
while(t--) {
int w, n, a[305], count;
scanf("%d%d", &w, &n);
count = n;
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
int j = 0;
for(int i = n - 1; i > j; i--) {
if(a[i] + a[j] <= w) {//小的先不动,让大的逐一来凑
count--;
j++;
}
}
printf("%d\n", count);
}
return 0;
}
#include
int main() {
double a[12], s = 0;
for(int i = 0; i < 12; i++) {
scanf("%lf", &a[i]);
s += a[i];
}
printf("%.2lf", s / 12);
return 0;
}
#include
int main() {
int m, n;
while(scanf("%d%d", &m, &n)) {
if(m == 0 && n == 0) break;
int a, b, c, d, e, f, g = 0, k = 0, i = 0;
a = m / 100;
b = m / 10 % 10;
c = m % 10;
d = n / 100;
e = n / 10 % 10;
f = n % 10;
if(c + f >= 10) {
i++;
g = 1;
}
if(b + e + g >= 10) {
i++;
k = 1;
}
if(a + d + k >= 10) i++;
printf("%d\n", i);
}
return 0;
}
解法:找规律,其实就是斐波那契数列,反过来看,假设从第n阶往第1阶走。f(n)代表可能的次数,可以走1阶也可以走2阶,所以f(n)=f(n-1)+f(n-2)。
注意不能用递归,因为时间会超,m可以取40,计算时间太长了,只能先打表,再对应打印出来。
递归超时, 迭代不会。这个就是斐波那契数列的递推公式了。
#include
int main() {
int n;
scanf("%d", &n);
while(n--) {
int m, a[45];
a[1] = 0;
a[2] = 1;
a[3] = 2;
scanf("%d", &m);
for(int i = 4; i <= m; i++) {
a[i] = a[i-1] + a[i-2];
}
printf("%d\n", a[m]);
}
return 0;
}
解法:动态规划,单调递减子序列长度。
#include
#include
using namespace std;
int dp[25];
int main() {
int t;
scanf("%d", &t);
while(t--) {
int m, a[25], maxn = 0;
scanf("%d", &m);
for(int i = 0; i < m; i++)
scanf("%d", &a[i]);
for(int i = 0; i < m; i++) {//终点位置
dp[i] = 1;
for(int j = 0; j < i; j++)
if(a[i] < a[j]) dp[i] = max(dp[i], dp[j]+1);
maxn = max(dp[i], maxn);
}
printf("%d\n", maxn);
}
return 0;
}
解法:对于汉诺塔求移动次数公式为f(n+1)=f(n)*2+1。
#include
long long Solve(int n) {
if(n == 1) return 2;
long long t;
t = Solve(n/2);
if(n % 2 == 0) return ((t%1000000) * (t%1000000)) % 1000000;
else
return (2 * (t%1000000) * (t%1000000)) % 1000000;
}
int main() {
int n;
scanf("%d", &n);
while(n--) {
int m;
scanf("%d", &m);
printf("%lld\n", Solve(m)-1);
}
return 0;
}
#include
using namespace std;
int dp[20][20];
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
for(int i = 1; i <= n; i++) {//基层状态初始化
dp[i][1] = 1;
dp[1][i] = 1;
}
for(int i = 2; i <= n; i++) {
for(int j = 2; j <= n; j++) {
if(i == j) dp[i][j] = dp[i][j-1] + 1;//dp[i][j]=dp[i][j-1]+1,加上i由单独一个i构成的这一种情况。
else if (i < j) dp[i][j] = dp[i][i];//j超过i的数没有用,所以dp[i][j]=dp[i][i];
else
dp[i][j] = dp[i][j-1] + dp[i-j][j];//可以分为取j和不取j的情况,若不取j,dp[i][j]=dp[i][j-1],取j时(i可能由多个相同的j构所以j不用变化),dp[i][j]=dp[i-j][j]
}
}
cout << dp[n][n] << endl;
}
return 0;
}
解法:9的阶乘是36万多,题目给出的n小于100万,所以只能是由10以下的数阶乘想加组成的。
#include
using namespace std;
int jiecheng(int i) {
int sum = 1;
for(int j = 1; j <= i; j++)
sum *= j;
return sum;
}
int main() {
int m;
cin >> m;
while(m--) {
int n;
cin >> n;
for(int i = 9; i > 0; i--) {
if(n >= jiecheng(i)) n -= jiecheng(i);//逐一阶乘相减,减到最后为0则刚好是由某些阶乘组成的
}
if(n == 0) cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
#include
#include
using namespace std;
int main() {
int n;
cin >> n;
while(n--) {
stack s[3];//数组栈,模仿汉诺塔的三根针,三个栈
int p, q;//层数与指令条数
int a, b;//接收指令
bool flag = 0;//若有非法指令,就标记为1
cin >> p >> q;
for(int i = p; i > 0; i--)
s[0].push(i);//压栈
while(q--) {
cin >> a >> b;
if(s[a-1].empty()) flag = 1;//如果栈s[a-1]即针是空的,那指令就是非法
else if(!s[b-1].empty() && s[a-1].top() > s[b-1].top()) flag=1;//栈s[b-1]也不是空的,而且从大往小移,也是非法指令
if(flag == 0) {
s[b-1].push(s[a-1].top());//栈s[a-1]的栈顶赋值到栈s[b-1]
s[a-1].pop();//并删除栈s[a-1]的栈顶
}
}
if(flag) cout << "illegal\n";
else
cout << "legal\n";
}
return 0;
}
#include
int main() {
int n;
scanf("%d", &n);
while(n--) {
int m, k;
scanf("%d %d", &m, &k);
printf("%d\n", m + (m-1) / (k-1));
}
return 0;
}
#include
int main() {
int N;
scanf("%d\n", &N);
while(N--) {
int n, a[100], k = 0;
scanf("%d", &n);
while(n) {
a[k++] = n % 2;
n = n / 2;
}
int t = 0;
for(int i = k - 1; i >= 0; i--)
if(a[i] == 1) t++;
printf("%d\n", t);
}
return 0;
}
#include
#include
int main() {
int n;
double a, b, c, d, x, y;
scanf("%d", &n);
while(n--) {
scanf("%lf%lf%lf%lf", &a, &b, &c, &d);
x = a - c;
y = b - d;
printf("%.2lf\n", sqrt(x*x + y*y) );
}
return 0;
}
/*
*
* UM.
* J@B@1 iO@1
* Y@@@B@BB. 7B@B@B@
* :@B@i,B@B@O ,Z@B@B@B@Br
* @B@q i@B@BS 7@B@@@O5vMB@q
* 8@@B LB@B@i FB@@@BNjYjLE@B@
* ,B@B: 0@@@Z P@B@BM1JJ125JPB@B
* B@BB :@B@B XB@B@Z2LuU52F2u2@B@.
* :@B@ @@@B: v@B@B8uJj51F1525uUB@B7
* @B@O 0@@B. ..::ir7vvYUuU777r::. B@B@OULU2F2F151F11Y@B@S
* B@B, 8B@B :ruXMB@B@B@B@B@B@B@B@B@B@@@B@B@B@B@B@B@5Jj1211F1F1F2FUJO@BB
* U@B@ @B@B@B@B@B@@@B@B@B@MMqPS5JuYL7rq@B@OBB@B@B8Yu211F1515251515YGB@@
* @B@u v@@@@MSur:. LB@MvvjJuU5YU252F1F1F25251F2uX@@@
* @@@. N@BML2U2UUU12F15252525251515Jk@@B
* r@B@ YB@Bju52121252515252F15251F2u5@B@
* PB@B @@@PYUF151F25151F152F2F1F15jF@@B
* @@BS N@@@UJ2F25252F251525151F1F1u5@B@
* @@@7 B@B@5Yj12F152F1F1F25252515jFB@B
* B@Bi M@B@O2Luu52525212F151121UY1@B@7
* O@B@: v@B@BMSuYJJuuUu2u2uujjYJJXB@B@M
* 7B@B@, 1B@@@B@GPF1uujuu21PNMB@B@B@B@@
* qB@B2 i8B@B@B@B@B@@@@@B@B@B@q: @@@B
* MB@B: 7SBB@B@B@B@B@Zu: @B@B
* ZB@B. ,v. @B@L
* LB@B, Y7 @B@Bu 7@B@
* :B@B@@B2: @@B7 @B@Z r@B@B@BP: B@BE
* BB@@@B@B@B@BE r@B@B 7@B@B@B@Ou: iB@B
* :uM@@B@@2. :7::::ivk@B@B@0 :5B@B@B@B@B@B@G. @B@i
* BB@@@B@@ :@B@B@@@B@B@@1 .i5M@B@B@@@5 M@@2
* B@B ,@B1 L0EZZG0F7: .:, uB@MrP@M7
* 2@B@ ,O@B@B@B@B
* @B@1 :@B@@@r :@@@@B@BL:,,
* B@Bi :2ZS; :@B@B@B@r L@B@B@BU
* @B@. @@@B@B@ vB@B@B@B5 @B@i
* B@B 7B@B@B@BM OB@B@B@ ,B@B
* @B@ @B@B@@@i rL7. B@BM
* B@B7.: NB@@M. .@B@.
* .;JEB@@@B@B@B@B@. . @B@u
*@@@B@B@B@B@@@B@18U :B@B@B@BU,
*7@BOui. ,@@B SP@B@B@B@B@Or
* @@@U B@BJ.YO@B@B@i
* r@B@ :B@Bk .k@B@
* B@B@ LB@@k 2i
* B@BM .7jXEGqF7: OB@@L
* .B@BM .B@B@B@B@B@B@. :@B@B:
* .B@B@ @@MYr::ivG@B .M@B@G
* B@@@S ,MB@B@,
* v@@@BF .1B@B@Br
* 2@@B@BL ,FB@@@B8,
* r@B@B@BF, :YBB@B@B@B
* L@B@B@B@P7, .ivXB@B@B@B@B@M@B@
* ,1B@B@B@B@@@BOP2L7i:,. ..,:i7LSNB@@B@B@@@B@B@B@Z5v;.LB@@
* @B@OEB@B@@@B@B@B@B@B@B@B@B@@@B@B@B@B@B@@@B@B@B@B@BM0SJ7i::::i:,u@B@
* B@Bu ::i;7vu2XNGOMB@B@BMB@B@B@B@B@B@@@B1UFuj77ii:::::::iir;r;i.YB@B
* @B@L.:i:i:i::::::::::..Y@B@BMYi:i;SB@B@N:.::i:iirir;r;rii::::ivO@B@
* B@@X::,::::iirir;riri:E@B@1 ,@B@Br:;;r;rii:i::::i7JEB@@@@@B
* @@@B@BBq5v7ii:::::::.2@@@i ..,.. @B@@,,:::::irv2XMB@B@B@B@2@B@:
* .B@BBB@@@B@B@B@BMNP5u7@B@1 .,,:,, :. @B@P50MB@B@B@B@B@@@BS: @@B1
* E@B@ ijGB@B@B@B@B@B@B@Bi .,:,,..@@B@7 B@B@B@B@B@B@BM57. kB@B
* .@B@: .,ivu5Nq@B@u ..,.. SB@B@@@B@PL7i, ,@B@
* @@@8 i@B@: . :B@B@@ B@@2
* i@@@@ 0@B@u B@@B. vB@B
* ,@B@G L@B@BOv:.:iFB@B@M @B@Bi
* vNi S@@B@B@B@B@BM: MB@N
* 758BMqJ,
*
* . YO. vq :G Z:
* SqOMBB@B@Br @@r rBE @B B@@@@@B@ONX8k i::::.OB1.:::.u@O.::::i @B@B@U:@@B@@BPEBu
* B@@NB@k. 5@i uB@E. BM 1U2uUJvirB@@Z r@@B@B@@@B@B@B@B@B@@@B@Bi LB@B@1 BX :@k uLLLvr@BJ:
* iB iBi 7@ .@M8@BGMZZ @@F ,B Pi v@ Bq @i v@ B@
* vuL7r8@S7vJL7N@Z7LLri;72. F7@Bvvv@@ @BX @@@B@B@@@@@B@@@B@B@B 7@ @F Bi @q @B@Bu @B
* N@B@G@@@8@BBOMB@G@BMNXG@, B@ @@ .Bk .:u; i@: Zv 7@ Bk @,;@ ,BY @B B@
* r@ @G 5. ,@v BZ :::,.r@E .::i, @B B@ .@BL 7@ @F B:i@. .@ @M @B
* 7B: ,vO, @@ iB@: @B 7@:MB@B@B@@@B@B@BM @@. B: 2@q 7@ BS @i 0@ B. @O B@
* ,r2EBB@B@B@Bi G@ @BB B@ @B @S : r@ .. 7B @F @7 B7 @ @B @B
* E@B@UOBr @B@Bi L@0PB .BZ .@B@B@B@B@B@B@B@B@B@B@B@, r@ BF @i @G B@B@B B@
* 7@, kB@U ;r @@@. .@Z GBuL@iBBi vB@B@q BP:5@7 @u,. @B
* LBi YB@BrB@ @@ @B:L@Br BM .M@B rB rB@J v@. Pi @XZ8r . B@
* . G@i B@BM. ,B@, @B iB@B N, 7r..q@k ,LB@B8 J@, i@B@B1r Br @@
* MB@B@B ,i B@B@B, B@: @B@B@F .@BB: P@i :OBZ .@U B@B@B:
* .ll rB. :
*
*/
import org.omg.CosNaming.BindingHelper;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
public static InputReader in = new InputReader(new BufferedInputStream(System.in));
public static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int t = in.nextInt();
while (t-- > 0) {
BigInteger a = in.nextBigInteger();
BigInteger b = in.nextBigInteger();
BigInteger c = in.nextBigInteger();
out.println(a.modPow(b, c));
out.flush();
}
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
}
#include
struct beibao {
int v, w;
} b[15];
int main() {
int n;
scanf("%d", &n);
while(n--) {
int s, m, sum = 0;
struct beibao t;
scanf("%d%d", &s, &m);
for(int i = 0; i < s; i++)
scanf("%d%d", &b[i].v, &b[i].w);
for(int i = 0; i < s; i++) {
for(int j = i + 1; j < s; j++) {
if(b[j].v > b[i].v) {//排序,将该物品单位重量的价值从大到小排
t = b[j];
b[j] = b[i];
b[i] = t;
}
}
}
for(int i = 0; i < s; i++) {
if(m < b[i].w) {
sum += b[i].v * m;//超重时,加部分该物品,即单位重量价值乘以剩余空间 (物品可以分割)
break;
} else {
sum += b[i].v * b[i].w;//还未超重时,加全部该物品,即单位重量价值乘以总重量
m -= b[i].w;
}
}
printf("%d\n", sum);
}
return 0;
}
#include
int main() {
int N, M, n, m;
scanf("%d%d", &N, &M);
int a[N], sum[N];
for(int i = 0; i < N; i++) {
scanf("%d", &a[i]);
if(i == 0) sum[i] = a[i];
else if(i >= 1) sum[i] = sum[i-1] + a[i];
}
while(M--) {
scanf("%d%d", &m, &n);
if(m == 1) printf("%d\n", sum[n-1]);
else
printf("%d\n", sum[n-1]-sum[m-2]);
}
return 0;
}
#include
int gcd(int m, int n) {
return n == 0 ? m : gcd(n, m%n);
}
int main() {
int a, b, c, d;
char ch;
while(~scanf("%d/%d%c%d/%d", &a, &b, &ch, &c, &d)) {
int fenzi, fenmu;
fenmu = b * d / gcd(b, d);
if(ch == '+') {
fenzi = a * fenmu / b + c * fenmu / d;
if(fenzi == 0) printf("0\n");
else if(fenzi % fenmu == 0) printf("%d\n", fenzi / fenmu);
else
printf("%d/%d\n", fenzi / gcd(fenzi, fenmu), fenmu / gcd(fenzi, fenmu));
} else {
fenzi = a * fenmu / b - c * fenmu / d;
if(fenzi == 0) printf("0\n");
else if(fenzi % fenmu == 0) printf("%d\n", fenzi / fenmu);
else {
if(fenzi < 0) {
fenzi = -fenzi;
printf("-%d/%d\n", fenzi / gcd(fenzi, fenmu), fenmu / gcd(fenzi, fenmu));
} else
printf("%d/%d\n", fenzi / gcd(fenzi, fenmu), fenmu / gcd(fenzi, fenmu));
}
}
}
return 0;
}
#include
#include
int main() {
char s[1005];
while(gets(s)) {
int l = strlen(s);
for(int i = 0; i < l - 2; i++) {
if(s[i] == 'y'&& s[i + 1] == 'o'&& s[i + 2] == 'u') {
s[i] ='w';
s[i + 1] = 'e';
s[i + 2] = '0';
}
}
for(int i = 0; i < l; i++) {
if(s[i] != '0') printf("%c", s[i]);
}
printf("\n");
}
return 0;
}
解法:count计数第几次
W(n) = SUM[k = 1…n; k * T(k + 1)]即W(n)=1*T(2)+2*T(3)+...+n*T(n+1)
T(n) = 1 + … + n ,等差数列求和也就是 T(n)=n*(n+1)/2
#include
int main() {
int n, count = 1;
scanf("%d", &n);
while(n--) {
int a, sum = 0;
scanf("%d", &a);
for(int i = 1; i <= a; i++)
sum += i * (i + 1) * (i + 2) / 2;//W(n)
printf("%d %d %d\n", count++, a, sum);
}
return 0;
}
#include
#include
using namespace std;
int main() {
int t;
scanf("%d", &t);
while(t--) {
int m, a[1005];
scanf("%d", &m);
for(int i = 0; i < m; i++)
scanf("%d", &a[i]);
sort(a, a + m);
printf("%d\n", a[(m - 1) / 2]);
}
return 0;
}
#include
int zhuanhua(int s, int r) {//转化为现实世界中的时间
s = s * 60;//先转换为秒,防止下面除20时要为整型舍去了一部分时间
for(int i = 1; i <= r; i++)
s /= 20;
return s;
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
int m, r = 0, s;//r为梦境的层数
char c[5];
long long sum = 0;
scanf("%d", &m);
for(int i = 0; i < m; i++) {
scanf("%s", c);
if(c[0] == 'I') r++;
if(c[0] == 'O') r--;
if(c[0] == 'S') {
scanf("%d", &s);
sum += zhuanhua(s, r);
}
}
printf("%lld\n", sum);
}
return 0;
}
解法:在一个n阶完全图的所有生成树的数量为n的n-2次方(图论)。
#include
int f(int a, int b, int c) {
int sum = 1;
for(int i = 1; i <= b; i++) {
sum *= a;
sum %= c;
}
return sum;
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
printf("%d\n", f(n, n-2, 10003));
}
return 0;
}
#include
const int a[] = {39916800, 3628800, 362880, 40320, 5040, 720, 120, 24, 6, 2, 1, 1};
//11到0分别的阶乘
int cator(char *s, int n) {
int sum = 0, t;
for(int i = 0; i < n; i++) {
t = 0;
for(int j = i + 1; j < n; j++) {
if(s[j] < s[i]) t++;//找出比其小的并且还没有出现过的个数
}
sum += a[i] * t;
}
return sum + 1;//上述所求数是比目标值小的种数目,所以加一
}
int main() {
int n;
scanf("%d", &n);
while(n--) {
char s[13];//数组必须开大一点
scanf("%s", s);
printf("%d\n", cator(s, 12));
}
return 0;
}
#include
int gcd(int a, int b) {
if(a%b==0)
return b;
else
return gcd(b,a%b);
}
int main() {
int N;
scanf("%d", &N);
while(N--) {
int a, b, n;
scanf("%d%d%d", &a, &b, &n);
if(n % gcd(a, b) == 0) printf("Yes\n");//s = gcd(a, b) n%s shi赋值
else
printf("No\n");
}
return 0;
}
#include
#include
using namespace std;
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, a[1005];
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
if(n % 2 != 0) printf("%d\n", a[(n - 1) / 2]);
else {
if(a[n / 2] > a[n / 2 - 1]) printf("%d\n", a[n / 2]);
else
printf("%d\n", a[n / 2 - 1]);
}
}
return 0;
}
#include
int main() {
double n;
while(scanf("%lf", &n), n) {
double count = 0, sum = 0, i = 1.00;
while(sum < n) {
i++;
sum += 1.00 / i;
count++;
}
printf("%d card(s)\n", (int)count);
}
return 0;
}
#include
#include
#include
#include
int fenzi(int a, int b) {
long long s1 = 1;
for(int j = a; j > a - b; j--)
s1 *= j;
return s1;
}
int fenmu(int a, int b) {
long long s2 = 1;
for(int k = b; k > 1; k--)
s2 *= k;
return s2;
}
int main() {
int m, n;
while(~scanf("%d%d", &m, &n)) {
long long sum = 1, d1, d2;
for(int i = m; m > 0; m -= n) {
d1 = fenzi(m, n);
d2 = fenmu(m, n);
sum *= (d1 / d2);
}
printf("%lld\n", sum % 2013);
}
return 0;
}
#include
#include
using namespace std;
struct dna {
char str[55];
int num;
} d[105];
void Count(int i, int n, int m) {
for(int j = 0; j < n-1; j++)
for(int k = j+1; k < n; k++)
if(d[i].str[j] > d[i].str[k])
d[i].num++;
}
bool cmp(const dna &a, const dna &b) {
if(a.num < b.num) return true;
return false;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++) {
scanf("%s", d[i].str);
Count(i, n, m);
}
stable_sort(d, d+m, cmp);
for(int i = 0; i < m; i++)
printf("%s\n", d[i].str);
return 0;
}
解法:本题关键在于这个东西if((ch>='a'&&ch<='z')||(ch>='0'&&ch<='9')||ch=='_'),
如果打入一个 int intline(intline是一个变量),那么应该只有一个关键字!!!
题目中说了严格按照c++标准,所以可以根据以上的判断条件把程序分成一段一段的,挨个判断!!!
还存在__int64这样的也是关键字,所以需要储存'_'和数字以及字母,遇到其他符号或者空格等就停止存入str2
转而进行判断,而后再存下一个单词。
问题:'_'字符存不存都能过,_int64并不算是出现了关键字。
#include
#include
#include
using namespace std;
struct strin {
char str1[10];//用来存储关键字
} s[10];
int main() {
int t;
cin >> t;
while(t--) {
memset(s, 0, sizeof(s));
int m, count = 0, k = 0;
char str2[1000], ch;
cin >> m;
for(int i = 0; i < m; i++)
cin >> s[i].str1;
while(scanf("%c", &ch) && ch != '@') {//为什么cin>>ch就错了
if((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_') str2[k++] = ch; //逐个获取单词
else {
for(int i = 0; i < m; i++)
if(strcmp(str2, s[i].str1) == 0) count++;//比较有没有和关键字相同的单词
memset(str2, 0, sizeof(str2));
k = 0;
}
}
cout << count << endl;
}
return 0;
}
#include//算出每天所需房间数
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
int a[n], b[n], c[n], sum[190] = {0}, max = 0;//用 sum数组来记录房间
for(int i = 0; i < n; i++)
scanf("%d%d%d", &a[i], &b[i], &c[i]);
for(int i = 0; i < n; i++) {
for(int j = b[i]; j < b[i]+c[i]; j++)//j代表某天
sum[j] += a[i];//某天所需要的房间数
}
for(int i = 1; i <= 190; i++) {
if(max < sum[i]) max = sum[i];//某天的最大房间数也就是最合理的安排
}
printf("%d\n", max);
}
return 0;
}
#include
#include
int isPrime(int n) {
if(n < 2) return 0;//1和0既不是素数也不是合数
int i = 2;
for(i = 2; i*i < n; i++)
if(n % i == 0) return 0;
return 1;
}
int main() {
int N;
scanf("%d", &N);
while(N--) {
int x, len = 0;
scanf("%d", &x);
while(1) {//死循环,直到找到素数
if(isPrime(x + len)) {
printf("%d\n", x + len);
break;//先判断大的是不是素数
} else if(x - len > 1 && isPrime(x - len)) {
printf("%d\n", x - len);
break;
}
len++;
}
}
return 0;
}
#include
#include
using namespace std;
int dp[25][25];
int main() {
int a[25][25], n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d", &a[i][j]);
for(int i = 1; i <= n; i++)//从1开始,因为dp[i-1][j]和dp[i][j-1]不存在
for(int j = 1; j <= m; j++)
dp[i][j] = max(dp[i-1][j]+a[i][j], dp[i][j-1]+a[i][j]);
printf("%d\n", dp[n][m]);
return 0;
}
#include
#include
using namespace std;
int main() {
string str;
while(getline(cin, str) && str[0] != '#') {
int sum = 0;
for(int i = 0; str[i] != '\0'; i++) {
if(str[i] == ' ') continue;
else
sum += (i+1) * (str[i]-64);
}
cout << sum << endl;
}
return 0;
}
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
long long sum = 1;
for(int i = 2; i <= n; i++) {
long long s = 1;
for(int j = i; j > 1; j--)
s *= j;
if(i % 2 == 0) sum += s;
else
sum -= s;
}
printf("%lld\n", sum);
}
return 0;
}
#include
#include
int a[2000005];
void primer() {//打表筛选素数
a[2] = 1;
for(int i = 1; i <= 2000001; i++)
if(i % 2) a[i] = 1;//素数一定是奇数,所以把奇数都标记为1
for(int i = 3; i <= sqrt(2000001); i += 2) {//跳开偶数
if(a[i]) {
for(int j = i * 2; j <= 2000001; j += i)//j跟i有倍数关系,所以是合数,标为0
a[j] = 0;
}
}
}
int main() {
int n;
primer();
while(~scanf("%d", &n), n) {
for(int i = 2; i <= n; i++)
if(a[i]) printf("%d ", i);
printf("\n");
}
return 0;
}
解法:最大公约数就相当于它一次能跳过几个洞,如果为1,就说明它最后肯定会遍历到所有的数,假如不为1而是m,就说明它一次可以跳过m-1个洞(注意是跳过),其中肯定有漏过而没有进去的洞,所以兔子能逃得一命。
#include
int gcd(int m, int n) {
int k = m, i;
if(n < k) k = n;
for(i = k ; i >= 1; i--)
if(m % i == 0 && n % i == 0)
return i;
}
int main() {
int m, n;
while(~scanf("%d%d", &m, &n)) {
if(gcd(m, n) == 1) printf("NO\n");
else
printf("YES\n");
}
return 0;
}
#include
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
double l, d, r, len, ans;
scanf("%lf%lf%lf", &l, &d, &r);
len = sqrt(r*r - (d/2)*(d/2));
if(r <= d * 1.0 / 2 || len < 0.5) printf("impossible\n");
else {
ans = l / (len*2);
if(ans == (int)ans) printf("%d\n",(int)ans);//如果刚好除尽即刚好够,就不需要再加一台
else
printf("%d\n",(int)ans + 1);
}
}
return 0;
}
#include
int gcd(int m, int n) {
return n == 0 ? m : gcd(n, m%n);
}
int main() {
int n, f = 0, u = 0, k;
scanf("%d", &n);
getchar();
char s[n];
scanf("%s", s);
for(int i = 0; i < n; i++) {
if(s[i] == 'S') f = 1;
if(s[i] == 'U') u++;
}
k = u / n;
if(f == 1) printf("Bingo\n");
else if(k < 0.003 && k > 0.5) printf("Fail\n");
else
printf("%d/%d\n", u/gcd(u, n), n/gcd(u, n));
return 0;
}
#include
int Find(int k) {
int ans[1000] = {0}; //记录数据
int count = 0,s = 1;
while(s < 1000) { // 遍历出第一个大于1000的数
s *= k;
count++;
}
s %= 1000;
k %= 1000;
ans[s] = count;
while(true) {// 需找出现的第二个数,即尾数相等。
s = s * k % 1000;
count++;
if(!ans[s]) ans[s] = count;
else
return ans[s] + count;
}
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
int k;
scanf("%d", &k);
printf("%d\n", Find(k));
}
return 0;
}
#include
#include
int a[100005], b[100005];
int search(int num, int len) {//二分查找
int m;
int x = 1, y = len;//x为下界(左边界)固定从位置1即a[0]开始,y是上界
while(x <= y) {
m = x + (y-x) / 2;//中间位置
if(b[m] > num) y = m - 1;//如果发现所找的数即传进来的num小于中间,就让上界-1
else if(b[m] < num) x = m + 1;
else
return m;
}
return x;//返回下界
}
int main() {
int n;
while(~scanf("%d", &n)) {
memset(b, 0, sizeof(b));
int max = 1, len;
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
b[1] = a[0];//把b[1]赋值最小,这样最小的序列就为1,而不是0
for(int i = 1; i < n; i++) {
len = search(a[i], max);//查找一个序列
b[len] = a[i];//此时,为了进行下一步查找,b[i]要被赋值
if(max < len) max = len;//查找更新最大长度
}
printf("%d\n", max);
}
return 0;
}
解法:双重循环肯定超时的 直接用一个判断条件能减少时间 ,i*j+i+j =n 经过观察,可以变形为i*j+i+j+1=n+1,也就是说,可以进一步变形为(i+1)*(j+1)=n+1,所以i从2判断到sqrt(n+1)即可。
#include
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
long long n, t, count = 0;
scanf("%lld", &n);
t = sqrt(n+1);
for(int i = 2; i <= t; i++) {//i相当于(i+1),所以这个i从2开始
if((n + 1) % i == 0) count++;
}
printf("%lld\n", count);
}
return 0;
}
#include
#include
int main() {
int T;
scanf("%d", &T);
while(T--) {
char x;
int y;
getchar();
scanf("%c%d", &x, &y);
if(x >= 'A'&& x <= 'Z') printf("%d\n", x - 'A' + y + 1);
else
printf("%d\n", y - (x - 'a' + 1));
}
return 0;
}
#include
int LeapYear(int y) {
return (y%4 == 0 && y%100 != 0) || (y%400 == 0);
}
int DatetoInt(int year, int month, int day) {
int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int s = 0, k = year - 1;
s += k * 365 + k / 4 - k / 100 + k / 400;
for (int i = 1; i < month; i++) s += a[i];
if (LeapYear(year) && month > 2) s++;
s += day;
return s;
}
int main() {
int y, m, d;
while(~scanf("%d%d%d", &y, &m, &d)) {
printf("%d\n", (DatetoInt(y, m, d) - DatetoInt(1600, 1, 1)-1) % 7);
}
return 0;
}
#include
#include
char s1[30], s2[30], s3[30];//s3储存后序序列
int t;
void getPost(int l1, int r1, int l2, int r2) {
if(l1 > r1) return;
s3[t++] = s1[l1];//先储存根结点
int k = 0;
while(s1[l1] != s2[l2+k]) k++;//k记录属于左子树的元素个数
getPost(l1+k+1, r1, l2+k+1, r2);//先对右子树递归,分别提取两个s1和s2子串(最左边和最右边位置)
getPost(l1+1, l1+k, l2, l2+k-1);//再对左子树递归,分别提取两个s1和s2子串(最左边和最右边位置)
}
int main() {
while(~scanf("%s%s", s1, s2)) {
int len = strlen(s1);
t = 0;
getPost(0, len-1, 0, len-1);
for(int i = len-1; i >= 0; i--)//后序序列要逆序输出
printf("%c", s3[i]);
printf("\n");
}
return 0;
}
#include
int main() {
int T;
scanf("%d", &T);
while(T--) {
char s[11];
getchar();
for(int i = 0; i < 11; i++)
scanf("%c", &s[i]);
for(int i = 0; i < 11; i++) {
if(s[i] >= 'a'&& s[i] <= 'c') printf("2");
else if(s[i] >= 'd'&& s[i] <= 'f') printf("3");
else if(s[i] >= 'g'&& s[i] <= 'i') printf("4");
else if(s[i] >= 'j'&& s[i] <= 'l') printf("5");
else if(s[i] >= 'm'&& s[i] <= 'o') printf("6");
else if(s[i] >= 'p'&& s[i] <= 's') printf("7");
else if(s[i] >= 't'&& s[i] <= 'v') printf("8");
else if(s[i] >= 'w'&& s[i] <= 'z') printf("9");
}
printf("\n");
}
return 0;
}
#include
int main() {
int n, t, a[100005] = {0}, count = 0;
scanf("%d%d", &n, &t);
while(t--) {
char s[10];
int m;
scanf("%s", s);
if(s[0] == 'C') {
scanf("%d", &m);
a[m] = !a[m];
if(a[m]) count++;
else
count--;
}
if(s[0] == 'Q') printf("%d\n", count);
}
return 0;
}
#include
#include
int isprime(int n) {
if (n == 1) return 0;
int k = sqrt(n);
for (int i = 2; i <= k; i++)
if (n % i == 0) return 0;
return 1;
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
int N;
long long s = 1;
scanf("%d", &N);
for(int n = 2; n <= N; n++)
if (isprime(n))
s = s * n % 1000000;
printf("%lld\n", s);
}
return 0;
}
#include
#include
using namespace std;
int main() {
int N;
while(~scanf("%d", &N)) {
int a[1005], count = 0;
for (int i = 0; i < N; i++) scanf("%d", &a[i]);
for (int i = 1; i < N; i++)
for (int j = 0; j < N - i; j++)
if (a[j] > a[j+1]) {
swap(a[j], a[j+1]);
count++;
}
printf("%d\n", count);
}
return 0;
}
#include
int main() {
int T;
scanf("%d", &T);
while(T--) {
int a, max, min, N;
scanf("%d", &N);
scanf("%d", &a);
max = min = a;
for(int i = 1; i < N; i++) {
scanf("%d", &a);
if(a > max) max = a;
if(a < min) min = a;
}
printf("%d\n", max-min);
}
return 0;
}
#include
#include
using namespace std;
struct stu {
int number;
int xuehao;
int score;
int paiming;
} a[100005];
int cmp(stu b,stu c) {
if(b.score > c.score) return 1;
if(b.score == c.score && b.number < c.number ) return 1;
if(b.score == c.score && b.number == c.number && b.xuehao < c.xuehao ) return 1;
else
return 0;
}
int main() {
int t, m;
scanf("%d%d", &t, &m);
int n, b, sum = 0, k = 0, count, h, temp;
for(int i = 1; i <= t; i++) {
scanf("%d", &n);
sum += n;
for(int j = 1; j <= n; j++) {
scanf("%d", &b);
a[k].number = i;//k记录学生总数,方便遍历及存数据
a[k].xuehao = j;
a[k].score = b;
k++;//这个不能放在前面数组括号里,因为执行了第一个后k就会改变
}
}
sort(a, a+sum, cmp);
count = 1;//排名开始为1,即第一名
h = a[0].score; //比较时的变量,从第一个开始跟后面的比较
for(int i = 0; i < k; i++) {
if(a[i].score != h) {
count++;
h = a[i].score;
}
a[i].paiming = count;
}
for(int j = 1; j <= m; j++) {
scanf("%d", &temp);
for(int i = 0; i < k; i++) {
if(a[i].paiming == temp) printf("%d %d\n",a[i].number, a[i].xuehao);
}
}
return 0;
}
#include
int main() {
int T;
scanf("%d", &T);
getchar();
while(T--) {
int s[26] = {0};
char a[1010];
gets(a);
for(int i = 0; a[i] != '\0'; i++)
++s[a[i] - 'a'];
int k = 0;
for(int i = 1; i < 26; i++)
if(s[i] > s[k]) k = i;
printf("%c\n", k + 'a');
}
return 0;
}
#include
#include
#define PI 3.1415926
int main() {
double r, v;
while(scanf("%lf", &r)!=EOF) {
v = (4.0/3.0)*PI*r*r*r;
printf("%.lf\n", v);
}
return 0;
}
#include
int main() {
int T;
scanf("%d", &T);
while(T--) {
int a, b;
char c;
scanf("%x%c%x", &a, &c, &b);
if(c == '+') printf("%o\n", a + b);
else
printf("%o\n", a - b);
}
return 0;
}
解法:打表找规律。
#include
int fib(int n) {
int f3 = 5, f2 = 3, f;
if (n == 2) return 3;
if (n == 3) return 5;
for (int i = 4; i <= n; i++) {
f = f3 + f2;
f2 = f3;
f3 = f;
}
return f;
}
int main() {
int n;
scanf("%d", &n);
while(n--) {
int m;
scanf("%d", &m);
printf("%d\n", fib(m));
}
return 0;
}
#include
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, a[200000];
cin >> n;
mapb;
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);//先排序,可以保证得到的第一个最大是编号最小的
for (int i = 0; i < n; i++)
b[a[i]]++;
int maxn = 0, ans = 0;
for (int i = 0; i < n; i++) {
if (maxn < b[a[i]]) {
maxn = b[a[i]];
ans = a[i];
}
}
cout << ans << endl;
}
return 0;
}
解法:桶排。
#include
int main() {
int T;
scanf("%d", &T);
while(T--) {
int a[1005] = {0}, t, N, count = 0;
scanf("%d", &N);
for(int i = 0; i < N; i++) {
scanf("%d", &t);
a[t] = 1;
}
for(int i = 0; i < 1005; i++)
if(a[i] == 1) count++;
printf("%d\n", count);
for(int i = 0; i < 1005; i++)
if(a[i] == 1) printf("%d ", i);
printf("\n");
}
return 0;
}
#include
int main() {
int N;
scanf("%d", &N);
while(N--) {
int n, s = 0, a = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
a = a + i;
s = s + a;
}
printf("%d\n", s);
}
return 0;
}
#include
int main() {
int n;
int a[4][4];
scanf("%d", &n);
while(n--) {
for(int i = 1; i < 4; i++) {
for(int j = 1; j < 4; j++)
scanf("%d", &a[i][j]);
}
for(int i = 1; i < 4; i++) {
for(int j = 1; j < 4; j++)
printf("%d ", a[j][i]);
printf("\n");
}
}
return 0;
}
#include
int main() {
int N;
scanf("%d", &N);
while(N--) {
int m, x, y, z;
scanf("%d", &m);
int a = 0, b = 10000000, c = 0;
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &x, &y, &z);
if(x > a||x == a&&y < b||x==a&&y == b&&z > c) {
a = x;
b = y;
c = z;
}
}
printf("%d\n", c);
}
return 0;
}
#include
#include
int main() {
int n;
scanf("%d", &n);
while(n--) {
char s[1000];
int a[3] = {0, 0, 0};
scanf("%s", s);
int l = strlen(s);
for(int i = 0; i < l; i++) {
if(s[i] == 'R') a[0]++;
if(s[i] == 'W') a[1]++;
if(s[i] == 'B') a[2]++;
}
for(int i = 0; i < a[0]; i++)
printf("R");
for(int i = 0; i < a[1]; i++)
printf("W");
for(int i = 0; i < a[2]; i++)
printf("B");
printf("\n");
}
return 0;
}
解法:考虑到像400这样的大数,答案应该是20*20,再分解应该是4位数,所以要4层循环,因为每个数都只有唯一的解,可以打表,存进数组 。
#include
int main() {
int n, a[455], m;
for(int i = 1; i < 455; i++)
a[i] = -1;//先全部初始化为不存在情况时应该输出的-1
a[0] = 10; //特殊情况
for(int i = 1; i <= 9; i++)
for(int j = 1; j <= 9; j++)
for(int k = 1; k <= 9; k++)
for(int l = 1; l <= 9; l++)
for(int m = 1; m <= 450; m++)//数字结果放在最里层
if(i*j*k*l == m) {
a[m] = i + j*10 + k*100 + l*1000;//因为后循环的是较大结果,如1125和5211,最后确定的a[10]是5211循环,为保证小即1125,应该逆序表达这个数
if(l == 1) a[m] = a[m] % 1000;//对于存在1的应该求余以去掉1数字位
if(k == 1) a[m] = a[m] % 100;
if(j == 1) a[m] = a[m] % 10;
break;//找到一个数字结果可以对应的相乘,则跳出挑数循环
}
while(~scanf("%d", &n))
printf("%d\n", a[n]);
return 0;
}
#include
#include
int main() {
int n;
scanf("%d", &n);
getchar();
while(n--) {
int k = 0;
char a[200];
gets(a);
int j = strlen(a);
for(int i = 0; i < j; i++)
if(a[i] >= 'a'&& a[i] <= 'z') k++;
int s = (k % 26);
if(s == 0) printf("z\n");
else
printf("%c\n", s - 1 + 'a');
}
return 0;
}
#include
int main() {
int n;
while( scanf("%d", &n) != EOF) {
if(n == 0) printf("%d", n);
int a[100], k = 0;
while (n > 0) {
a[k++] = n % 2;
n /= 2;
}
for(int i = k - 1; i >= 0; i--)
printf("%d", a[i]);
printf("\n");
}
return 0;
}
#include
int main() {
int T;
scanf("%d", &T);
while(T--) {
char a, b;
scanf(" %c %c", &a, &b);
if(a > b) printf("%c<%c\n", a, b);
if(a == b) printf("%c=%c\n", a, b);
if(a < b) printf("%c>%c\n", a, b);
}
return 0;
}
#include
#include
#include
using namespace std;
struct st {
char str[30];//定义成string类型的也可以
int len;
} s[20];
bool cmp(st a, st b) {
return a.len < b.len;
}
int main() {
int n, count = 0;
while(cin >> n) {
if(n == 0) break;//n为0结束程序
for(int i = 0; i < n; i++) {
cin >> s[i].str;
s[i].len = strlen(s[i].str);
}
sort(s, s+n, cmp);//不写成stable_sort也行
cout << "SET " << ++count << endl;
for(int i = 0; i < n; i += 2)
cout << s[i].str << endl;
if(n % 2) {
for(int i = n-2; i > 0; i -= 2)
cout << s[i].str << endl;
} else {
for(int i = n-1; i > 0; i -= 2)
cout << s[i].str << endl;
}
}
return 0;
}
#include
#include
int main() {
int n, count[10005] = {0};
scanf("%d", &n);
getchar();
char name[n][15];//二维字符串
for(int i = 0; i < n; i++) {
scanf("%s", name[i]);
for(int j = 0; j <= i; j++) {
if(strcmp(name[i], name[j]) == 0) count[i]++;
}
}
int max = 0, f;
for(int i = 0; i < n; i++) {
if(count[i] > max) {
max = count[i];
f = i;
}
}
printf("%s %d\n", name[f], max);
return 0;
}
解法:三重循环,最外面控制行(最外面是套行),最里面控制列,中间控制数字。
#include
using namespace std;
char digit[3][31] = {" _ _ _ _ _ _ _ _ ",
"| | | _| _||_||_ |_ ||_||_|",
"|_| ||_ _| | _||_| ||_| _|"
};
int main() {
int a[4];
while(cin >> a[0] >> a[1] >> a[2] >> a[3]) {
for (int i = 0; i < 3; i++) {//表示行,每个数字3行
for (int j = 0; j < 4; j++)//表示输入的数字
for (int k = 0; k < 3; k++)//每个列,每个数字3列
cout << digit[i][3*a[j]+k];
cout << endl;
}
}
return 0;
}
#include
#include
#include
using namespace std;
int dp[1005];
struct apple {
int c, w;
} a[1005];
int main() {
int n, v;
while (~scanf("%d%d", &n, &v)) {
if(n == 0 && v == 0) break;
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i++)
scanf("%d%d", &a[i].c, &a[i].w);
for (int i = 0; i < n; i++) {
for (int j = v; j >= a[i].c; j--) {
if(dp[j] < dp[j-a[i].c] + a[i].w) dp[j] = dp[j-a[i].c] + a[i].w;//如果取当前这个苹果的价值大于之前取得苹果的价值就更新一下
}
}
printf("%d\n", dp[v]);
}
return 0;
}
#include
int main() {
int n;
scanf("%d", &n);
while(n--) {
int m, k, f = 0, t = 0;
scanf("%d", &m);
k = m / 4181;//公式f(20)=2584*f(1)+4181*f(2)
for(int i = 0; i <= k; i++) {
if((m - i * 4181) % 2584 == 0) {//判断f(1)和f(2)是否为整数
f = 1;
t = i;
break;
}
}
if(f) printf("%d %d\n", (m - t * 4181) / 2584, t);
else
printf("No answer\n");
}
return 0;
}
#include
int main() {
char a[100];
double x, y, s = 0;
while(scanf("%s%lf%lf", a, &x, &y) != EOF)
s += x * y;
printf("%.1lf\n", s);
return 0;
}
解法:分割平面的个数=交点个数+顶点个数+1。
令f(n-1)为前n-1条折线分割的平面数,当添加第n条折线时。因为每一条边与前n-1条折线的两条边都相交,故增加的交点数为2*2*(n-1),顶点增加1,故f(n)=f(n-1)+4(n-1)+1,再累加。
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
printf("%d\n", 2 * n * n - n + 1);
}
return 0;
}
#include
#include
int dp[100000];
int main() {
int n;
while(~scanf("%d", &n)) {
int sum = 0, maxn, a[n];//int a[n], sum = 0, maxn;不能这样的顺序定义????
memset(dp, 0, sizeof(dp));
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
maxn = sum / 2;
for(int i = 0; i < n; i++) {
for(int j = maxn; j >= a[i]; j--) {
dp[j] = dp[j] > dp[j-a[i]]+a[i] ? dp[j] : dp[j-a[i]]+a[i];//不能for里j>=0,然后在这里判断if(j >= a[i])
}
}
printf("%d\n", sum - 2 * dp[maxn]);
}
return 0;
}
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int m, n, a, b, c, d;
scanf("%d%d", &m, &n);
if(n % 2 == 0 || m % 2 == 0) scanf("%d%d%d%d", &a, &b, &c, &d);//因为骨牌是一奇一偶,当棋盘长宽都是奇时,不能覆盖
else {
printf("NO\n");
continue;
}
if((a+b) % 2 == 0) {//就是判定 坐标差,只要(a+b)与(c+d)一奇一偶就可以完美覆盖
if((c+d) % 2) printf("YES\n");
else
printf("NO\n");
} else {
if((c+d) % 2 == 0) printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}
#include
#include
int main() {
int t;
scanf("%d", &t);
getchar();
while(t--) {
char s[205];
scanf("%s", s);
int len = strlen(s) - 2, i, j;
for(i = 1; i <= len; i++) {//最小长度,从1开始
if(len % i == 0) {//能刚好除尽才符合是循环小数,注意,该题严格控制了循环体是从第一个小数位开始的
for(j = 2; j < strlen(s)-i; j++) //j < strlen(s)-i 是为保证后一语句刚好能循环到最后一个
if(s[j] != s[j+i]) break;
if(j == strlen(s) - i) {//当完全执行完上面j的for后,j==strlen(s)-i
printf("%d ", i);
for(int p = 2; p < 2+i; p++)
printf("%c", s[p]);
printf(" %d\n", len / i);
break;
}
}
}
}
return 0;
}
#include
int fib2(int n) {
int f1, f2, f3, f;
if(n == 1) return 1;
if(n == 2) return 2;
if(n == 3) return 3;
f1 = 1;
f2 = 2;
f3 = 3;
for (int i = 4; i <= n; i++) {
f = f1 + f3;
f1 = f2;
f2 = f3;
f3 = f;
}
return f;
}
int main() {
int n;
while(scanf("%d", &n),n) {
int f;
f = fib2(n);
printf("%d\n", f);
}
return 0;
}
#include
#include
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int n, a[10];
cin >> n;
for(int i = 0; i < n; i++) {
a[i] = i + 1;
}
do {
for(int i = 0; i < n; i++)
cout << a[i];//最小字典序
cout << endl;
} while(next_permutation(a, a + n));
}
return 0;
}
#include
#include
int main() {
int t;
scanf("%d", &t);
getchar();
while(t--) {
char s[105], a;
int k = 0, x = 0, y = 0;
scanf("%s", s);
for(int i = 0; i < strlen(s); i++) {
if(s[i] == 'L') k = (k + 1) % 4;
if(s[i] == 'R') k = k >= 1 ? k - 1 : k + 3;
if(s[i] == 'M') {
if(k == 0) y++;
else if(k == 1) a = x--;
else if(k == 2) a = y--;
else if(k == 3) a = x++;
}
}
if(k == 0) a = 'N';
else if(k == 1) a = 'W';
else if(k == 2) a = 'S';
else if(k == 3) a = 'E';
printf("%d %d %c\n", x, y, a);
}
return 0;
}
#include
int main() {
long long n, b;
while(scanf("%lld%lld", &n, &b)!=EOF)
printf("%lld\n", n/b);
return 0;
}
#include
int main() {
int n;
while(~scanf("%d", &n)) {
int s = 1, count = 1;
while(s % n != 0) {
count++;
s = (s * 10 + 1) % n;//不取余会超时,当数太大时s超过int范围
}
printf("%d\n", count);
}
return 0;
}
#include
int main() {
int n;
while(scanf("%d", &n), n) {
int n1 = n, n2 =n, n3 = n;
int sum1 = 0, sum2 = 0, sum3 = 0;
while(n1) {
sum1 += n1 % 10;
n1 /= 10;
}
while(n2) {
sum2 += n2 % 12;
n2 /= 12;
}
while(n3) {
sum3 += n3 % 16;
n3 /= 16;
}
if(sum1 == sum2 && sum2 == sum3) printf("%d is a Sky Number.\n", n);
else
printf("%d is not a Sky Number.\n", n);
}
return 0;
}
#include
int main() {
int n, m;
while(~scanf("%d%d", &n, &m)) {
int k;
if(n % 2 == 1) k = (n+1) / 2;
else
k = n / 2;
if(m > k) printf("YES\n");//一定存在某数是另一个数的倍数
else
printf("NO\n");
}
}
#include
int pow(int a, int n) {
int result = 1;
while(n > 0) {
if(n % 2 == 1) result = result * a % 10003;
a = a * a % 10003;
n /= 2;
}
return result;
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, p;
scanf("%d%d", &n, &p);
int sum = 0;
for(int i = 1; i <= n; i++)
sum += pow(i, p);
printf("%d\n", sum % 10003);
}
return 0;
}
#include
int main() {
int n;
while(scanf("%d", &n), n) {
int t = 1;
for(int i = 1; i <= n; i++)
t = t * n % 9;
if(t == 0) printf("9\n");
else
printf("%d\n", t);
}
return 0;
}
解法:有规律,递推,斐波那契数列。
#include
int main() {
int n;
while(~scanf("%d", &n)) {
long long a[55];
a[0] = 0, a[1] = 1, a[2] = 2;
for(int i = 3 ; i <= n; i++)
a[i] = a[i-1] + a[i-2];
printf("%lld\n", a[n]);
}
return 0;
}
#include
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
char n[105];
int m, len, f, k = -1;
scanf("%s%d", n, &m);
len = strlen(n);
f = len - m;
for(int i = 0; i < f; i++) {//逐位求出
char max = '0';
for(int j = k + 1; j <= m + i; j++) {//从0开始,找到最大后(下一个数)从其后一个开始
//到m+i处停止是为了后面有足够位数组成新序列
if(max < n[j]) {
max = n[j];
k = j;
}
}
printf("%d", max - '0');//字符转数字
}
printf("\n");
}
return 0;
}
解法:一共有N个光棍,其中有M个没有抽到自己的纸条。从n个光混中取n-m个抽到自己名字的人(这是没有发生错排的光棍的情况),乘以另外m个人的错排个数。
#include
long long d(int n) {//错排
if(n == 1) return 0;
if(n == 2) return 1;
else
return (n-1)*(d(n-1)+d(n-2));
}
long long com(int x,int y) {//组合
int sum = 1;
for(int i = x; i > x - y; i--)
sum = sum * i / (x-i+1);
return sum;
}
int main() {
int n, m;
while(~scanf("%d%d", &n, &m))
printf("%lld\n", com(n, n-m) * d(m));
return 0;
}
#include
#include
int main() {
int m;
scanf("%d", &m);
while(m--) {
int n, a, b;
scanf("%d", &n);
a = n * (1 + sqrt(5)) / 2;
b = a + n;
printf("%d %d\n", a, b);
}
return 0;
}
#include
int main() {
int m,n;
scanf("%d", &m);
while(m--) {
scanf("%d", &n);
printf("%d\n", n);
}
return 0;
}
解法:背包问题变形。。。把这些数放在一个为所有数总和一半大的背包 中,使其尽量装的最多。则是两半差值最小
dp[j] = max{dp[j] , dp[j - a[i]] + a[i]}//如果容量足够装下a[i],则比较下dp。
#include
#include
#include
using namespace std;
int dp[100500];
int main() {
int t;
scanf("%d", &t);
while(t--) {
memset(dp, 0, sizeof(dp));
int n, num = 0, max;
scanf("%d", &n);
int a[n], sum = 0;
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
max = sum / 2;
for(int i = 0; i < n; i++) {
for(int j = max; j >= a[i]; j--) {//j相当于背包实际装的大小,放进去的数总和必须小于max
dp[j] = dp[j] > dp[j - a[i]] + a[i] ? dp[j] : dp[j - a[i]] + a[i];
}
}
printf("%d\n", sum - 2 * dp[max]);
}
return 0;
}
#include
#include
int main() {
int n, l;
scanf("%d", &n);
getchar();
while(n--) {
char s[1000];
gets(s);
l = strlen(s);
for(int i = 0; i < l; i++) {
if(s[i] >= 'a'&& s[i] <= 'z') printf("%c", s[i] - 32);
if(s[i] >= 'A'&& s[i] <= 'Z') printf("%c", s[i] + 32);
}
printf("\n");
}
return 0;
}
#include
int main() {
int n;
scanf("%d", &n);
while(n--) {
long long m, s;
scanf("%lld", &m);
s = 471 + 1000 * (m - 1);
printf("%lld\n", s);
}
return 0;
}
解法:递推,杨辉三角, 二维
0
1 1 1 1 1 1 1
1 2 3 4 5 6
1 3 6 10
1 4 10
#include
int main() {
int m, n;
while(scanf("%d%d", &n, &m)) {
if(m == 0 && n == 0) break;
long long f[33][33];
for(int i = 1; i < 33; i++)
f[i][0] = f[1][i] = 1;
for(int i = 2; i < 33; i++) {
for(int j = 1; j < 33; j++)
f[i][j] = f[i-1][j] + f[i][j-1];
}
printf("%lld\n", f[n][m]);
}
return 0;
}
题意:有若干袋饼干,每袋数量不同,要求拿走一袋,使总数量为偶数,求有多少种拿法?
#include
int main() {
int n;
while(~scanf("%d", &n)) {
int a[105], sum = 0, t = 0;
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum += a[i];
if(a[i] % 2 == 0) t++;
}
if(sum % 2 == 0) printf("%d\n", t);
else
printf("%d\n", n - t);
}
return 0;
}
#include
#include
#include
using namespace std;
int main() {
int n;
scanf("%d", &n);
getchar();
while(n--) {
char s[105];
scanf("%s", s);
if(s[0] == '0') {
printf("0 0\n");
continue;
}
int len = strlen(s);
for(int i = 0; i < len; i++)
s[i] = s[i] - '0';
sort(s, s + len);
for(int i = len - 1; i >= 0; i--)
printf("%d", s[i]);
printf(" ");
for(int i = 0; i < len; i++) {
if(s[i] != 0) printf("%d", s[i]);
}
printf("\n");
}
return 0;
}
#include
int main() {
int T;
scanf("%d", &T);
while(T--) {
double A, B, C;
scanf("%lf%lf%lf", &A, &B, &C);
if((A + B - C >= -0.0001)&&(A + B - C <= 0.0001))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
#include
int main() {
int t, sum[500005] = {0};//一定要初始化等于0
scanf("%d", &t);
for(int i = 1; i <= 250000; i++)//i为因子,从1开始遍历
for(int j = 2*i; j <= 500000; j += i)//当因子为i时,j为i的倍数则一定包含因子i,j从i的2倍开始,保证j的因子不包括他本身
sum[j] += i;//将j的各因子相加
while(t--) {
int n;
scanf("%d", &n);
printf("%d\n", sum[n]);
}
return 0;
}
#include
int fib(int n) {
int f1, f2, f;
if (n == 1) return 3;
if (n == 2) return 7;
f1 = 3;
f2 = 7;
for (int i = 3; i <= n; i++) {
f = f1 + f2;
f1 = f2;
f2 = f;
}
return f;
}
int main() {
int m;
while(scanf("%d", &m) && m != -1) {
int f, k = 0;
f = fib(m);
for(int i = 2; i < f; i++) {
if(f % i == 0) {
k = 1;
break;
}
}
if(k == 1) printf("No\n");
else
printf("Yes\n");
}
return 0;
}
#include
#include
int main() {
char s[5];
int t = 1;
while(~scanf("%s", s)) {
printf("Case %d: ", t++);
if(strcmp(s, "I") == 0) printf("1\n");
else if(strcmp(s, "II") == 0) printf("2\n");
else if(strcmp(s, "III") == 0) printf("3\n");
else if(strcmp(s, "IV") == 0) printf("4\n");
else if(strcmp(s, "V") == 0) printf("5\n");
else if(strcmp(s, "VI") == 0) printf("6\n");
else if(strcmp(s, "VII") == 0) printf("7\n");
else if(strcmp(s, "VIII") == 0) printf("8\n");
else if(strcmp(s, "IX") == 0) printf("9\n");
else if(strcmp(s, "X") == 0) printf("10\n");
else if(strcmp(s, "XI") == 0) printf("11\n");
else
printf("12\n");
}
return 0;
}
解法:余数定理。
分析:对于一个整数n,设f(n)为n的各个位之和,有这样一个等式 n%9=f(n)%9;
证明:同余定理(a+b)%c=(a%c+b%c)%c,还有显然成立和k*(10^M)%9=k,代入到n%9=f(n)%9中,
即可得证n%9=f(n)%9是成立的。还要注意我们所求的是一个一位数,而我们是对9取余的,当余数为0时,我们要的结果可能是0,可能是9。是0的情况,只有开始时两个乘数至少有一个为0。其他情况为9。
#include
int main() {
int n;
scanf("%d", &n);
while(n--) {
long long a, b, c;
scanf("%lld%lld", &a, &b);
if(a == 0 || b == 0) printf("0\n");
else {
c = (a%9) * (b%9) % 9;
if(c == 0) c = 9;
printf("%d\n", c);
}
}
return 0;
}
#include
int main() {
int t;
scanf("%d", &t);
getchar();
while(t--) {
int a, b, c;
char s[1];
scanf("%d%c%d=%d", &a, &s[0], &b, &c);
if(s[0] == '+') {
if(c == a + b) printf("Accept\n");
else
printf("Wrong Answer\n%d\n", a + b);
}
if(s[0] == '-') {
if(c == a - b) printf("Accept\n");
else
printf("Wrong Answer\n%d\n", a - b);
}
if(s[0] == '*') {
if(c == a * b) printf("Accept\n");
else
printf("Wrong Answer\n%d\n", a * b);
}
if(s[0] == '/') {
if(b == 0) printf("Input Error\n");
else if(c == a / b) printf("Accept\n");
else if(b != 0 && c != a / b) printf("Wrong Answer\n%d\n", a / b);
}
if(s[0] == '%') {
if(b == 0) printf("Input Error\n");
else if(c == a % b) printf("Accept\n");
else if(b != 0 && c != a % b) printf("Wrong Answer\n%d\n", a % b);
}
}
return 0;
}
坑点:需要两次剪枝。
剪枝1:n是奇数的话必定无法形成素数环!因为:奇、偶、奇、偶……奇, 头尾奇+奇=偶,不是素数。
剪枝2:素数的哈希表,防止再次判定素数浪费时间 。
#include
#include
using namespace std;
const int maxn = 25;
int vis[maxn], n, flag;
int ans[maxn];//存放环的顺序
int prime[45] = {0,0,1,1,0,1,0,1,0,0,0,
1,0,1,0,0,0,1,0,1,0,
0,0,1,0,0,0,0,0,1,0,
1,0,0,0,0,0,1,0,0,0
}; //0到40素数的哈希表
void dfs(int m, int n) { //m为已入环个数,n为目标入环个数即1~n全入环
if(m == n && prime[ans[0]+ans[n-1]]) {//头尾相加为素数
for(int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
cout << endl;
flag = 1;
} else {
for(int i = 2; i <= n; i++) {
if(!vis[i] && prime[i+ans[m-1]] == 1) {//如果i没有被访问并且i与前一个数相加是素数
ans[m] = i;//将i存入ans数组
vis[i] = 1;//并将其标记为已经访问
dfs(m+1, n);//递归求下一个数
vis[i]=0;//等找到所有的数时将标记,然后回溯
}
}
}
}
int main() {
int count = 0;
while(cin >> n, n) {
cout << "Case " << ++count << ":" << endl;
if(n == 1) cout << "1" <
497-排队打饭
#include
#include
using namespace std;
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, a[105], sum = 0;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
for(int i = 0; i < n; i++) {
sum += a[i] * (n - i);
}
printf("%d\n", sum);
}
return 0;
}
514-1的个数
解法:考虑数的构成,特别是权。个(一)位数的1次数是1,十(两)位数的1次数就是10,百(三)位数的1次数就是100,依次类推……
统计个位数,再把个位数消失掉,统计十位数(此时的十位数处于个位数的位置,但是权值是10),和刚才处理个位数一样的方法.
比如497,个位数会有个1(491),当个位数是1时,前面两位可以从0~48(49个,0相当于没有,此时是整数1)中随便取,都含有个位数的1。所以sum =1+49 = 50。如果个位数没1,则只需要加当前位数的权就行,如492,只需加sum=a[0]=1即前两位固定是49时只有491含有1,然后让个位数消失(497/10-1),减1的原因是(统计个位数后的工作是把4,9,1分别出现次数统计过了)。然后看48,8的位置上的1的个数是1*10(权值是10),然后4的位置上可以从0~3共4*10个。
所以sum = 50 + 10 + 40 = 100。然后统计百位数(48/10 - 1)就是3了。显然只有一个1,权值是100.所以sum = 100 + 1* 100 = 200。
#include
#include
using namespace std;
int a[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
int fun(int n) {
int i = 0, t = n, sum = 0, temp;
while(t) {
temp = t % 10;
if(temp > 1) sum += a[i];
else if(temp == 1) sum += n % a[i] + 1;
sum += temp * i * a[i-1];
t /= 10;
i++;
}
return sum;
}
int main() {
int a, b;
while(cin >> a >> b) {
if(a == 0 && b == 0) break;
if(a > b) swap(a, b);
cout << fun(b) - fun(a-1) << endl;//算出0~b之间1的个数,0~(a-1)之间1的个数,再相减
}
return 0;
}
519-密码发生器
#include
#include
int main() {
int n;
scanf("%d", &n);
getchar();
while(n--) {
char s[105];
scanf("%s", s);
int len = strlen(s), b[6] = {0}, i, j;//用数组a来记录6组每组的值
for(i = 0; i+5 < len; i += 6) {//没满6个一组则不执行里面的
b[0] += (int)s[i];
b[1] += (int)s[i+1];
b[2] += (int)s[i+2];
b[3] += (int)s[i+3];
b[4] += (int)s[i+4];
b[5] += (int)s[i+5];
}
for(int j = i; j < len; j++)//处理剩余不满一组的
b[j-i] += (int)s[j];
for(int i = 0; i < 6; i++) {
while(b[i] > 9) {//第二轮处理,看分解后得到的各位数字之和是否大于9,若是则继续第一步处理分解
int num = 0;
while(b[i]) {//第一轮处理,看b[i]是否分解成各位数字
num += b[i] % 10;
b[i] /= 10;
}
b[i] = num;
}
}
for(int i = 0; i < 6; i++)
printf("%d", b[i]);
printf("\n");
}
return 0;
}
520-最大素因子
#include
int a[1000005];
void primer() {
int i, j, count = 0;
for(i = 2; i < 1000001; i++) {
if (!a[i]) {
count++;//记录n的最大素因子在素数表里面的位置 ,如果输入的是素数则为它本身的位置
for (j = i; j < 1000001; j += i) {//能遍历所有数字并赋值,赋的值正是该数的最大素因子序数
a[j] = count;
}
}
}
}
int main() {
int n;
primer();
while(~scanf("%d", &n))
printf("%d\n", a[n]);
return 0;
}
525-一道水题
#include
#include
#include
#include
using namespace std;
int main() {
char s[5005], a[5005];
int num[5005];
while(cin >> s) {
string str;
int len, i, j, k = 0, begin;
len = strlen(s);
for(i = 0; i < len; i++)//遍历是否全为0
if(s[i] != '0') break;
for(j = i; j < len; j++)//遍历看0之后是否全为5
if(s[j] != '5') break;
if(i == len || j == len) {//如果s串为连续0和连续5,即000和555和000555全输出0
printf("0\n");
continue;
}
for(i = 0; i < len; i++) {
if(s[i] != '5') {
begin = 0;
memset(a, 0, sizeof(a));
while(s[i]!='5') {
a[begin++] = s[i];
i++;
if(i == len) break;
}
num[k++] = atoi(a);
}
}
sort(num, num+k);
for(int i = 0; i < k; i++)
cout << num[i] << " ";
cout << endl;
}
return 0;
}
527-AC_mm玩dota
#include
int fib(int n) {
int f0 = 1, f1 = 1, f;
if(n == 0) return f0;
if(n == 1) return f1;
for(int i = 2; i <= n; i++) {
f = (f0 % 1314520 + f1 % 1314520) % 1314520;
f0 = f1 % 1314520;
f1 = f % 1314520;
}
return f;
}
int main() {
int n;
while(~scanf("%d", &n)) {
int a = 0, b = 0;
while(n) {
if(n % 2 == 1) a++;
else
b++;
n /= 2;
}
printf("%d\n", fib(a*b));
}
return 0;
}
528-找球号(三)
#include
int main() {
int n;
while(~scanf("%d", &n)) {
int num, std = 0;
for(int i = 1; i <= n; i++) {
scanf("%d", &num);
std ^= num;//std = std ^ num;
}
printf("%d\n", std);//经过异或处理后为单的球会为答案
}
return 0;
}
553-字符出现的次数
#include
int main() {
char a[100];
int s = 0;
while(gets(a)) {
for(int i = 0; a[i] != '\0'; i++)
if(a[i] == 'a')s++;
printf("%d\n", s);
}
return 0;
}
556-最大公约数
#include
int gcd(int m, int n) {
int k = m, i;
if(n < k) k = n;
for(i = k ; i >= 1; i--)
if(m % i == 0 && n % i == 0)
return i;
}
int main() {
int m, n;
while(scanf("%d,%d", &m, &n) != EOF)
printf("%d\n", gcd(m, n));
return 0;
}
588-Money
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, count = 0;
scanf("%d", &n);
for(int i = 0; i <= (n * 10); i++) {
for(int j = 0; j <= (n * 5); j++) {
for(int k = 0; k <= (n * 2); k++) {
if(i + 2*j + 5*k == 10*n) count++;
}
}
}
printf("%d\n", count);
}
return 0;
}
589-糖果
解法:只要所输入的所有数中的最大的一个最多只能比其余数的总和多一(即第一颗和最后一颗都是吃最大数的那种),就会yes。
#include
int main() {
int n;
scanf("%d", &n);
while(n--) {
int m, a, sum = 0, max = 0;
scanf("%d", &m);
for(int i = 1; i <= m; i++) {
scanf("%d", &a);
if(a > max) max = a;
sum += a;//循环结束后多加了一个max值
}
if((2 * max - sum) <= 1) printf("Yes\n");
else
printf("No\n");
}
return 0;
}
594-还是A+B
#include
int zhuanhua(char x[]) {
if(x[0] == 'z') return 0;
else if(x[0] == 'o') return 1;
else if(x[0] == 't' && x[1] == 'w') return 2;
else if(x[0] == 't' && x[1] == 'h') return 3;
else if(x[0] == 'f' && x[1] == 'o') return 4;
else if(x[0] == 'f' && x[1] == 'i') return 5;
else if(x[0] == 's' && x[1] == 'i') return 6;
else if(x[0] == 's' && x[1] == 'e') return 7;
else if(x[0] == 'e') return 8;
else
return 9;
}
int main() {
char a[10];
while(~scanf("%s", a)) {
int sum = 0;
char b[10], c[10], d[10];
zhuanhua(a);
scanf("%s", &b);
if(b[0] == '+') {
scanf("%s%s", c, d);
if(d[0] == '=') sum = zhuanhua(a) + zhuanhua(c);
else {
scanf(" =");
sum += zhuanhua(a) + zhuanhua(c) * 10 + zhuanhua(d);
}
} else {
sum = zhuanhua(a) * 10 + zhuanhua(b);
scanf(" + %s%s", c, d);
if(d[0] == '=') sum += zhuanhua(c);
else {
scanf(" =");
sum += zhuanhua(c) * 10 + zhuanhua(d);
}
}
printf("%d\n", sum);
}
return 0;
}
595-乱七八糟
#include
int main() {
int n, m;
while(~scanf("%d%d", &n, &m)) {
int a[105] = {0}, t;
for(int i = 0; i < n; i++) {
scanf("%d",&t);
a[t]++;
}
while(m--) {
int k, b[105] = {0}, j = 1;
for(int i = 100; i >= 0; i--) {
if(a[i] != 0) {
b[j] = a[i];
j += a[i];
}
}
scanf("%d", &k);
printf("%d\n", b[k]);
}
}
return 0;
}
596-谁是最好的Coder
#include
int main() {
int n, a, b, s, max;
while(scanf("%d", &n), n) {
scanf("%d%d", &a, &b);
max = a + b;
for(int i = 1; i < n; i++) {
scanf("%d%d", &a, &b);
s = a + b;
if(s > max) max = s;
}
printf("%d\n", max);
}
return 0;
}
597-完数?
#include
int main() {
int n;
while(scanf("%d", &n)) {
if(n == -1) break;
int sum = 0;
for(int i = 1; i < n; i++) {
if(n % i == 0) sum += i;
}
if(sum == n) {
printf("%d=1", n);
for(int i = 2; i < n; i++)
if(n % i == 0) printf("+%d", i);
} else
printf("No");
printf("\n");
}
return 0;
}
599-奋斗的小蜗牛
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int h;
scanf("%d", &h);
if(h <= 10) printf("1\n");
else if(h % 5 == 0) printf("%d\n", h / 5 - 1);
else
printf("%d\n", h / 5);
printf("\n");
}
return 0;
}
//第一个白天10米,之后每一天爬5米
604-小明的难题
#include
#include
int main() {
int n;
scanf("%d", &n);
getchar();
while(n--) {
char s[105];
scanf("%s", s);
int len = strlen(s);
for(int i = 0; i < len; i += 2)
if(s[i] >= 'a' && s[i] <= 'z') s[i] -= 32;
for(int i = 0; i < len; i++)
printf("%c", s[i]);
printf("\n");
}
return 0;
}
615-最近点对
#include
#include
using namespace std;
int main() {
int n;
while(~scanf("%d", &n)) {
int a[1005], i;
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
int min = a[1] - a[0], k = 0;
for(i = 1; i < n - 1; i++) {//只能i到n-1
if((a[i + 1] - a[i]) < min) {
min = a[i + 1] - a[i];
k = i;
}
}
printf("%d %d\n", a[k], a[k + 1]);
}
return 0;
}
618-追击
解法:点(Xo,Yo)到直线的距离为最短距离 y=kx 即 kx-y=0 ,A=k,B=-1,C=0,距离 d=│AXo+BYo+C│ / sqrt(A*A + B*B)。
#include
#include
int main() {
double k, a, b, v;
while(~scanf("%lf%lf%lf%lf", &k, &a, &b, &v)) {
double b1, b2, s, v2;
s = fabs((a * k - b) * 1.0 / sqrt(1 + k * k));//最短距离
b2 = sqrt(a * a + b * b);//点到(0,0)的距离
b1 = sqrt(b2 * b2 - s * s);//豺狼人走的距离
v2 = (s * v) * 1.0 / b1;//距离与速度的比值(时间)相同,求出速度
printf ("%.3lf\n", v2);
}
return 0;
}
622-Vote
解法:需要超过一半的州支持,即人数最少的那一半州,那一半的州中,每个州要超过一半的人支持 ,一半加一。
#include
#include
using namespace std;
int main() {
int n;
while(scanf("%d", &n), n) {
int a[105], sum = 0, i;
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
for(i = 0; i < (n / 2) + 1; i++)
sum += (a[i] / 2 + 1);
printf("%d\n", sum);
}
return 0;
}
624-笨蛋的难题(一)
#include
int main() {
char s[10];
int n, k, count;
while(scanf("%s%d", s, &n) != EOF) {
if(s[0] == 'N') count = 0;//定义count代表的起始方向
if(s[0] == 'E') count = 1;
if(s[0] == 'S') count = 2;
if(s[0] == 'W') count = 3;
for (int i = 0; i < n; i++) {
scanf("%d", &k);
if(k) count++;
else
count--;
}
int t = count % 4;//转4次回到原方向,4为一个周期
if(t < 0) t += 4;//再向右转一个周期
if(t == 0) printf("North\n");
if(t == 1) printf("East\n");
if(t == 2) printf("South\n");
if(t == 3) printf("West\n");
}
return 0;
}
627-Increase and Decrease
解法:一个数组里的数通过数步的两个元素的加1,减1操作,最多个操作出x个相同的元素。sum%n==0,n个;否则n-1。
#include
int main() {
int n;
while(~scanf("%d", &n)) {
int a[100005], sum = 0;
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
if(sum % n == 0) printf("%d\n", n);
else
printf("%d\n", n - 1);
}
return 0;
}
628-小媛在努力
#include
const int N=1000005;
int a[N];
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, count = 1;//为什么不能在这定义a[10000005]
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for(int i = 1; i < n; i++) {
if(a[i] == a[i-1]) count++;
else {
printf("%d %d ", count, a[i-1]);
count = 1;
}
}
if(a[n-2] == a[n-1]) {
printf("%d %d\n", count, a[n-2]);//如果最后几个数相同,上面循环里就不会运行到else来输出
//所以需要在这输出
} else
printf("1 %d\n", a[n-1]);// 如果最后两个数不相同,上面循环里运行到else只能输出倒数第二个数
//所以最后一个不同的数需要在这输出
}
return 0;
}
636-世界末日
#include
int main() {
int n, i, a[10001];
while (scanf("%d", &n) != EOF) {
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("YES\n");
}
return 0;
}
637-我和你
#include
#include
using namespace std;
int main() {
string str, model="我";
while(cin>>str) {
if(str == "0") break;
int l;
l = model.size();
for(int i = 0; i < str.size(); i++)
if(str.substr(i, l) == "我") str.replace(i, l, "你");
else if(str.substr(i, l) == "你") str.replace(i, l, "我");
cout << str << endl;
}
return 0;
}
638-Take it easy II
#include
int main() {
int a, b, c, d, e;
while(scanf("%d%d%d%d%d", &a, &b, &c, &d, &e) != EOF) {
int n, sum = 0, s;
for(int i = 0; i < 5; i++) {
scanf("%d", &n);
sum += n;
}
s = 25 + (a + b + c + d + e) * 10;
if(sum > s) printf("Yes %d\n", sum - s);
else
printf("No !\n");
}
return 0;
}
639-找规律
#include
using namespace std;
double a[10];
int main() {
while(cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4]) {
int t;
if(a[4]-a[3]==a[3]-a[2] && a[3]-a[2]==a[2]-a[1] && a[2]-a[1]==a[1]-a[0]) {
t = a[4] - a[3];
cout << a[4]+t << " " << a[4]+2*t << " " << a[4]+3*t << " " << a[4]+4*t << " " << a[4]+5*t << endl;
} else if(a[4]*1.0/a[3]==a[3]*1.0/a[2] && a[3]*1.0/a[2]==a[2]*1.0/a[1] && a[2]*1.0/a[1]==a[1]*1.0/a[0]) {
t = a[4] * 1.0 / a[3];
cout << a[4]*t << " " << a[4]*t*t << " " << a[4]*t*t*t << " " << a[4]*t*t*t*t << " " << a[4]*t*t*t*t*t << endl;
} else if(a[4]==a[3]+a[2] && a[3]==a[2]+a[1] && a[2]==a[1]+a[0])
cout << a[4]+a[3] << " " << a[4]*2+a[3] << " " << a[4]*3+2*a[3] << " " << a[4]*5+a[3]*3 << " " << a[4]*8+a[3]*5 << endl;
else
cout << "None" << endl;
}
return 0;
}
649-Books
#include
int main() {
int n, m;
while(~scanf("%d%d", &n, &m)) {
int a[100005], sum, temp, max = 0;
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
for(int i = 0; i < n; i++) {
sum = 0, temp = m;
for(int j = i; j < n && temp > 0; j++) {//遇到有一个比他大(不够时间读)就不能读了(即停止该层循环)
temp -= a[j];
if(temp >= 0) sum++;
}
if(sum > max) max = sum;
}
printf("%d\n", max);
}
return 0;
}
650-Draw Something
#include
int main() {
int n;
while(scanf("%d", &n), n) {
int a[1005], sum = 0, t;
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
t = a[i] * a[i];
sum += t;
}
printf("%d\n", sum);
}
return 0;
}
655-光棍的yy
解法:本题思想就是将算的的每一位数放到数组里面,然后让前面两项的各个位数相加,处理好进位的问题就OK了。
#include
#include
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
char s[205];
int num[205][205] = {0};//定义二维数组储存斐波那契数列
cin >> s;
int n = strlen(s), k = 0, j;//k用来记录进位了多少次,长度代表第n项
num[1][0] = 1, num[2][0] = 2;//初始化前两项
for(int i = 3; i <= n; i++) {//i代表斐波那契数列第几项
for(j = 0; j <= k; j++) {//j代表数位
num[i][j] = num[i][j] + num[i-1][j] + num[i-2][j];//让前面两项的各个位数相加,把自身加上是算上前一位数进位了的情况即往下2行处
if(num[i][j] >= 10) {//处理进位,分别存放
num[i][j+1] = num[i][j] / 10;
num[i][j] %= 10;
}
}
if(num[i][k+1]) k++;//该项循环后,num[i][k+1]即最前面一位不为0,代表进过位了
}
for(int i = k; i>= 0; i--)//逐位输出,越左边的数(进位而来)的i越大,所以i从大到小输出
cout << num[n][i];
cout << endl;
}
return 0;
}
658-字符串右移
#include
#include
int main() {
int k;
char str[1005];
while(~scanf("%s%d", str, &k)) {
int l = strlen(str), j;
for(int i = 0; i < l; i++) {
if((i + k) % l == 0) j = i;
}
for(int i = j; i < l; i++)
printf("%c", str[i]);
for(int i = 0; i < j; i++) {
printf("%c", str[i]);
}
printf("\n");
}
return 0;
}
659-判断三角形
#include
#include
using namespace std;
int main() {
int t, count = 1;
scanf("%d", &t);
while(t--) {
int a[3];
for(int i = 0; i < 3; i++) {
scanf("%d", &a[i]);
}
sort(a, a + 3);
if((a[0] + a[1] > a[2]) && (a[2] - a[1] < a[0])) {
if((a[0] == a[1] && a[1] == a[2])) printf("Case #%d:\nYE YE YE\n", count);
else if(a[0] != a[1] && a[1] != a[2]) printf("Case #%d:\nYE\n", count);
else printf("Case #%d:\nYE YE\n", count);
} else {
printf("Case #%d:\nNO\n", count);
}
count++;
}
return 0;
}
663-弟弟的作业
#include
#include
int main() {
int a, b, count = 0;
char c, s[5];
while(~scanf("%d%c%d=%s", &a, &c, &b, s)) {
if(s[0] == '?') continue;
int t = atoi(s);
if(c == '+' && a + b == t) count++;
else if(c == '-' && a - b == t) count++;
}
printf("%d\n", count);
return 0;
}
673-悟空的难题
#include
#include
using namespace std;
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, m, a[101] = {0}, k;
scanf("%d%d", &n, &m);
while(n--) {
scanf("%d", &k);
a[k]++;
}
for(int k = 0; k < 101; k++) {
while(a[k]) {
printf("%d ", k);
a[k]--;
}
}
printf("\n");
}
return 0;
}
678-最小K个数之和
#include
#include
using namespace std;
int main() {
int count = 0;
while(count < 10) {
int n, k, a[100005], sum = 0;
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
for(int i = 0; i < k; i++)
sum += a[i];
count++;
printf("%d\n", sum);
}
return 0;
}
682-初学者的烦恼
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int m, j, n;
char s[55];
getchar();
scanf("%d%s%d", &m, s, &n);
n %= m ;
for(int i = n; i < m; i++)
printf("%c", s[i]);
for(int i = 0; i < n; i++)
printf("%c", s[i]);
printf("\n");
}
return 0;
}
685-查找字符串
#include
#include
#include
696-Course
解法:逐一比较课程,将比较过的课程数据置为1,之后的循环该课程不参与比较 (即同一课程只会i遍历一遍)。
#include
#include
using namespace std;
struct fun {
char name[35];
int score;
bool end;
} f[105];
int main() {
int t;
cin >> t;
while(t--) {
int n, sum = 0, maxs;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> f[i].name >> f[i].score;
f[i].end = 0;
}
for (int i = 0; i < n-1; i++) {
if(f[i].end == 1) continue;
maxs = f[i].score;
for (int j = i+1; j < n; j++) {
if(strcmp(f[i].name, f[j].name) == 0) f[j].end = 1;//比较过该课程,该组数据置为1
if(strcmp(f[i].name, f[j].name) == 0 && f[j].score > maxs) {
sum++;
maxs = f[j].score;
}
}
}
cout << sum << endl;
}
return 0;
}
709-异 形 卵
解法:异形卵周围的热量是ai到ai+l热量之和,(因为异形卵长度为l,所有需要占据l个点),再比较以不同i起点为寄生处的周围的热量中的最大值,输出相应起点下标即位置,若最大值小于零,这异型卵进入休眠,输出零。
#include
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
int l, n, a[1005], sum, max = -1, k = -1;
cin >> l >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n-l; i++) {//寄生处i从0开始遍历,最多只能到n-l处即(an-l-1到an-1热量之和)
sum = 0;
for (int j = 0; j < l; j++)
sum += a[i+j];
if(sum > max) {
max = sum;
k = i + 1;
}
}
if (max < 0) cout << "0" << endl;
else
cout << k << endl;
}
return 0;
}
721-纸牌游戏
解法:如果两张牌都不是王牌花色或者都是王牌花色,则牌面大的牌大,特别注意牌面10,牌面10的字符数组长度为3。
#include
#include
using namespace std;
int zhuanhuan(char s) {
if(s == 'J') return 11;
if(s == 'Q') return 12;
if(s == 'K') return 13;
if(s == 'A') return 1;
if(s == '1') return 10;
else
return s-'0';
}
int main() {
int t;
cin >> t;
while(t--) {
char wh, a[5], b[5];
int num1, num2;
cin >> wh >> a >> b;
num1 = zhuanhuan(a[0]);
num2 = zhuanhuan(b[0]);
if(a[strlen(a)-1] == wh && b[strlen(b)-1] != wh) cout << "YES" << endl;
else if(a[strlen(a)-1] != wh && b[strlen(b)-1] == wh) cout << "NO" << endl;
else {
if(num1 > num2) cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
return 0;
}
722-数独
解法:暴搜就行了。
坑点:注意每组数据的答案输出完之后都要再输出一个空行来分隔(尽管题目没有说)。
#include
using namespace std;
int map[9][9];
int flag = 0;
int check(int x, int y, int k) {
for(int i = 0; i < 9; i++) {
if(map[i][y] == k) return 0;//该行有重复的数
}
for(int j = 0; j < 9; j++) {
if(map[x][j] == k) return 0;//该列有重复的数
}
int a = x / 3 * 3, b = y / 3 * 3;//先除3是计算属于哪个宫内的,再乘3是计算该宫第一个数的下标
for(int i = a; i < a + 3; i++) {
for(int j = b; j < b + 3; j++) {
if (map[i][j] == k) return 0;//该3*3宫内有重复
}
}
return 1;
}
void dfs(int x, int y) {
if(flag) return;
if(x == 9 && y == 0) {//完整搜到了最后一行最后一列,特别注意搜完最后一列后是dfs(9,0)即跳到了第10行的第一列
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
cout << map[i][j] << " ";
}
cout << endl;
}
flag = 1;
return;
}
if(y == 9) dfs(x+1, 0);//到达最后一列,搜索下一行
if(map[x][y]) dfs(x, y+1);//该位置不为0即已经有事先填好的数字了,搜索下一列
if(!map[x][y]) {
for(int k = 1; k <= 9; k++) {
if(check(x, y, k)) {
map[x][y] = k;//保存搜到的数
dfs(x, y+1);//继续搜索下一列
map[x][y] = 0;//把该格重新标为0即没有放入数字,回溯
}
}
}
}
int main() {
int t;
cin >> t;
while(t--) {
for(int i = 0; i < 9; i++)
for(int j = 0; j < 9; j++)
cin >> map[i][j];
dfs(0, 0);//从第一行第一列开始搜
cout << endl;
flag = 0;//每组数据结束后都要重置为0,为下一组数据做好初始化
}
return 0;
}
739-笨蛋难题四
#include
int main() {
int n;
while(~scanf("%d", &n)) {
int a, max = 0, min;
scanf("%d", &a);
min = a;
for(int i = 2; i <= n; i++) {
scanf("%d", &a);
if(a < min) min = a;
if(a - min > max) max = a - min;
}
printf("%d\n", max);
}
return 0;
}
744-蚂蚁的难题(一)
解法:求区间[a, b] 之间的 异或 最大, 推几组数据,我们可以发现结果都是 2^n - 1;那么关键问题就是 求 n 的大小。
我们发现 n 的大小 与 a^b 有关。
#include
int main() {
long long a, b;
while(~scanf("%lld%lld", &a, &b)) {
long long t, res = 1;
int c = 0;
t = a ^ b;
while(t) {
c++;
t >>= 1;//右移一位相当于除以 2
}
for(int i = 0; i < c; i++)
res *= 2;
printf("%lld\n", res - 1);
}
return 0;
}
752-又见回文串
#include
#include
#include
using namespace std;
int main() {
char s[10005];
while(~scanf("%s", s)) {
int m = 1, n = 0, len = strlen(s);
sort(s, s + len);
for(int i = 0; i <= len-1; i++) {
if(s[i] == s[i+1]) m++;//计算同一字母相同有几个
else {//如果遇到不同的
if(m % 2) {// 若相同的字母是奇数个
n++;//则多出来的一个可能不能组成回文,n++用于计算先手删除个数
}
m = 1; //归一 ,重新计算下一同一相同的字母有几个
}
}
if(n % 2 && n != 1) printf("No\n");//如果不是只多出一个(只多出一个的话可以放中间也能组成回文),且为奇数(最后多出一个也可回文),后手赢
else
printf("Yes\n");
}
return 0;
}
756-重建二叉树
#include
#include
char s1[30], s2[30];
void getPre(int l1, int r1, int l2, int r2) {//两个串的最左和最右位置
if(l1 > r1) return;//每个子串如果连1个元素都没有就终止该次递归
int k = 0;
while(s1[r1] != s2[l2+k]) k++;//k记录属于左子树的元素个数
printf("%c", s1[r1]);//直接输出根结点
getPre(l1, l1+k-1, l2, l2+k-1);//对左子树递归,分别提取两个s1和s2子串(最左边和最右边位置)
getPre(l1+k, r1-1, l2+k+1, r2);//右子树递归,分别提取两个s1和s2子串(最左边和最右边位置)
}
int main() {
while(~scanf("%s%s", s1, s2)) {
int len = strlen(s1);
getPre(0, len-1, 0, len-1);
printf("\n");
}
return 0;
}
757-期末考试
#include
#include
using namespace std;
struct work {
int date;
int score;
} w[2005];
bool operator<(const work &a, const work &b) {//重载小于操作符,用于排序,按宽然后面积的顺序排序;<为从大到小排列
return a.score > b.score;
}
bool SetWork(bool f[], int n) {
for (int i = n; i > 0; i--)//i--,同一天交的作业,尝试在前面天数交
if (!f[i]) return f[i] = true;//遍历f数组,看是否该天有作业,如果该天没作业,返回true并把该天的f改为true
return false;//f遍历完,前面天数都有安排作业,则返回false,该作业无法完成
}
int main() {
int n;
while(~scanf("%d", &n)) {
int sum = 0;
bool f[2005] = {0};
for(int i = 0; i < n; i++) {
scanf("%d%d", &w[i].date, &w[i].score);
}
sort(w, w + n);//按score分数从大往小排, 如果分数相同date小的排前面
for (int i = 0; i < n; i++)
if (!SetWork(f, w[i].date)) sum += w[i].score;//如果函数返回false,则该天被占用,加上扣除的分数
printf("%d\n", sum);
}
return 0;
}
759-你知道这个规律吗?
解法:模拟除法 如1256/9,1%9=1,12%9=3,35%9=8,86%9=5 。
#include
#include
int main() {
char a[100005];
int b;
while(~scanf("%s%d", a, &b)) {
int len, k[100005], sum = 0;
len = strlen(a);
for(int i = 0; i < len; i++) {
k[i] = a[i] - 48;
sum = (sum * 10 + k[i]) % b;
}
printf("%d\n", sum);
}
return 0;
}
767-因子和
#include
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n, sum = 0, t;
scanf("%d", &n);
t = sqrt(n);
for(int i = 1; i <= t; i++)
if(n % i == 0) {
sum += i;
if(i*i == n) break;
sum += n / i;
}
if(sum == 1+n) printf("Yes\n");
else
printf("No %d\n", sum);
}
return 0;
}
768-移位密码
#include
#include
int main() {
char s[50];
while(~scanf("%s", s)) {
int k;
scanf("%d", &k);
int len = strlen(s);
for(int i = 0; i < len; i++) {
if(s[i] - k < 'A') s[i] = 'Z' - k + (s[i] - 'A' + 1);//B 移两位应该是 Z
else
s[i] -= k;
printf("%c", s[i]);
}
printf("\n");
}
return 0;
}
769-乘数密码
#include
int main() {
char s[55];
int k;
while(~scanf("%s%d", s, &k)) {
for(int i = 0; s[i] != '\0'; i++) {
for(int j = 0; j < 26; j++) {
if(s[i] == k * j % 26 + 'A') {
s[i] = 'A' + j;
break;
}
}
}
printf("%s", s);
printf("\n");
}
return 0;
}
770-仿射密码
#include
#include
int main() {
char s[55];
int k1, k2;
while(~scanf("%s%d%d", s, &k1, &k2)) {
int q = 26;
for(int i = 0; i < strlen(s); i++) {
for(int j = 0; j < q; j++) {
if(s[i] == (k1 * j + k2) % 26 + 'A') {
s[i] = 'A' + j;
break;
}
}
}
printf("%s\n", s);
}
return 0;
}
771-密钥解密
#include
#include
using namespace std;
int main() {
string m;
while(getline(cin, m)) {
string s;
getline(cin, s);
for(int i = 0; i < s.size(); i++) {
int t = s[i] + m[i%m.size()]-'0';//不能用s[i]等于,因为是s[i]是字符不能超过122但是t可以
if(t > 122) s[i] = t%122+31;
else
s[i] = t;
cout << s[i];
}
cout << endl;
}
return 0;
}
779-兰州烧饼
#include
int main() {
int n, k;
while(~scanf("%d%d", &n, &k)) {
if(n < k) printf("2\n");
else {
int sum, count;
sum = n * 2;//共有sum面
count = sum / k;//要多少分钟,可能有余数
if(sum - count * k > 0) count++;//如果有余再加一次 即一分钟
printf("%d\n", count);
}
}
return 0;
}
782-星期几?
#include
int main() {
int n, m, p;
while(~scanf("%d%d%d", &n, &m, &p)) {
int sum = 1;
for(int i = 1; i <= p; i++) {
sum = sum * m;
sum %= 7;//一边乘,一边对结果取余数
}
sum %= 7;
if((n + sum) % 7 == 0) printf("7\n");
else
printf("%d\n", (sum + n) % 7);
}
return 0;
}
783-司马光砸缸
解法:两个水桶的最大公约数,也就是一个水桶倒入另一个水桶后所剩余最小容量的水,再把最小容量的水,多次倒入水缸,看能不能倒满。
#include
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a%b);
}
int main() {
int a, b, c;
while(~scanf("%d%d%d", &a, &b, &c)) {
if(c % gcd(a, b) == 0) printf("YES\n");
else
printf("NO\n");
}
return 0;
}
784-Sigma
解法:a1, a2, a3, b1, b2;= a1 * (b1 + b2) + a2* (b1 + b2)+a3* (b1 + b2)=(a1+a2+a3)* (b1 + b2)
#include
int main() {
int n, m, s1 = 0, s2 = 0;
scanf("%d%d", &n, &m);
while(n--) {
int a;
scanf("%d", &a);
s1 += a;
}
while(m--) {
int b;
scanf("%d", &b);
s2 += b;
}
printf("%d\n", s1 * s2);
return 0;
}
793-又见 Lucky Number
#include
int a[1000005];
int main() {
int n;
while(~scanf("%d", &n)) {
int flag = 0;
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if(a[i] == i) flag = 1;
}
if(flag) printf("Yes\n");
else
printf("No\n");
}
return 0;
}
794-纪念邮票
解法:等差数列求和公式 s = a1*n + n * (n - 1) * d / 2
等差数列公式 an = a1 + (n - 1 ) * d;
[a,b]
b=a+(n-1),m=a*n+n*(n-1)/2,该n为i(项数),a为首项
#include
#include
using namespace std;
int main() {
int m, n, ant;
cin >> n >> m;
for(int i = sqrt(2 * m); i > 0; i--) {//i从大到小,则首项a从小到大输出
ant = m - i * (i - 1) / 2;//ant=a*i
if (ant % i == 0 && ant / i > 0 && ant / i + i - 1 <= n)//首项应为整数
cout << "[" << ant / i << "," << ant / i + i - 1 << "]" << endl;
}
return 0;
}
795-三角游戏
解法:过重心且平行于三角形任意一边的直线把这个三角形的面积分成4:5两个部分。
#include
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
double x1, x2, x3, y1, y2, y3;
scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3);
double a, b, c, p, s;
a = sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2));
b = sqrt((x1-x3) * (x1-x3) + (y1-y3) * (y1-y3));
c = sqrt((x3-x2) * (x3-x2) + (y3-y2) * (y3-y2));
p = (a + b + c) / 2;
s = sqrt(p * (p-a) * (p-b) * (p-c));
printf("%.1lf\n", 5 * s / 9);
}
return 0;
}
800-蛋蛋的问题(一)
#include
#include
using namespace std;
int main() {
int n;
while(~scanf("%d", &n)) {
int a[1005];
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
for(int i = 0; i < n; i++)
printf("%c ", 'A' + (a[i] % 26));
printf("\n");
}
return 0;
}
806-HEIHEI的心情
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
char s[10];
int a[5], l, sum = 0, n = 2;
getchar();
scanf("%s", s);
if(s[0] == 'S') n = 3;
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
printf("%d\n", sum);
}
return 0;
}
809-摸底
#include
#include
using namespace std;
int main() {
int n;
scanf("%d", &n);
while(n--) {
int a[7];
double s;
for(int i = 0; i < 7; i++)
scanf("%d", &a[i]);
sort(a, a + 7);
s = (a[0] + a[3] +a[6]) / 3.0;
printf("%.2lf\n", s);
}
return 0;
}
811-变态最大值
#include
int main() {
int n;
while(~scanf("%d", &n)) {
int a[n], max = 0, max1, min1, count;
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
for(int i = 0; i < n; i += 6) {
count = 1, max1 = a[i];
for(int j = i + 1; count < 3; j++, count++)
if(a[j] > max1) max1 = a[j];
if(max1 > max) max = max1;
if(n - i == 3) break;
count = 1, min1 = a[i+3];
for(int j = i + 4; count < 3; j++, count++)
if(a[j] < min1) min1 = a[j];
if(min1 > max) max = min1;
}
printf("%d\n", max);
}
return 0;
}
812-水题~~
#include
int pan1(int n) {
int k = 0, t = 0, b[100];
while(n) {
b[k++] = n % 2;
n = n / 2;
}
for(int i = k - 1; i >= 0; i--)
if(b[i] == 1) t++;
return t;
}
void jinzhi(int sum) {
int k = 0, b[100];
while(sum) {
b[k++] = sum % 2;
sum = sum / 2;
}
for(int i = k - 1; i >= 0; i--)
printf("%d", b[i]);
}
int main() {
int n;
while(~scanf("%d", &n)) {
int a[n], sum = 0;
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
for(int i = 0; i < n; i++) {
if(pan1(a[i]) % 2 == 0) sum += a[i];
}
if(sum == 0) printf("0");
else
jinzhi(sum);
printf("\n");
}
return 0;
}
813-对决
#include
int main() {
int n, k;
while(scanf("%d%d", &n, &k)) {
if(n == 0 && k == 0) break;
int f = 1;
for(int i = 1; i <= n / 2; i++) {//分两组,一个组从最少一个人开始,最多n/2人
if(i * (n - i) == k) {//对决场次,在k场时完成对决
f = 0;
break;
}
}
if(f == 0) printf("YES\n");
else
printf("NO\n");
}
return 0;
}
815-三角形
#include
#include
int main() {
int N;
scanf("%d", &N);
while(N--) {
double x1, y1, x2, y2, x3, y3;
double a, b, c, t, s;
scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3);
a = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
b = sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3));
c = sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3));
if((a+b)<=c||(a+c)<=b||(b+c)<=a) printf("Can not form a triangle.\n");
else {
t = (a + b + c) / 2.0;
s = sqrt(t*(t-a)*(t-b)*(t-c));
printf("%.3lf\n", s);
}
}
return 0;
}
816-它合法吗?
解法:标识符必须以字母a~z、 A~Z或下划线开头,后面可跟任意个(可为0)字符,这些字符可以是字母、下划线和数字,其他字符不允许出现在标识符中。
#include
#include
int main() {
char s[15];
while(~scanf("%s", s)) {
int len = strlen(s), f = 0;
if(!(s[0] == '_' || (s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z'))) {
printf("No\n");
continue;
}
for(int i = 1; i < len; i++) {
if(!((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] == '_')
|| (s[i] >= '0' && s[i] <= '9'))) {
f = 1;
break;
}
}
if(f == 0)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
819-奶牛
#include
#include
int main() {
char s[105];
while(~scanf("%s", s)) {
int count = 0, j, k, t, n;
for(int i = 0; i < strlen(s); i++) {
if(s[i] == 'c') count++;
}
if(count % 2 != 0) printf("0\n");
else if(count == 0) printf("%d\n", strlen(s) - 1);
else {
n = 0;//代表count重新计数
t = count / 2;
for(int i = 0; i < strlen(s); i++) {
if(s[i] == 'c') {
n++;
if(n == t) j = i;
if(n == t + 1) k = i;
}
}
if(k - j == 0) printf("0\n");//没空时分不了,只能在这同一个牛栏中
else {
printf("%d\n", k - j);
}
}
}
return 0;
}
821-简单求值
#include
#include
#include
using namespace std;
int main() {
int n;
scanf("%d", &n);
int a[n], p = 0, q = 0, t;
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a + n);
for(int i = 0; i < n; i += 2)
p += a[i];
for(int i = 1; i < n; i += 2)
q += a[i];
t = abs(p-q);
printf("%d\n", t);
return 0;
}
824-Greedy Mouse
#include
#include//保留小数位的头文件
#include
using namespace std;
struct huasheng {
int w, f;
} h[1005];
bool cmp(huasheng a, huasheng b) {
double t1, t2;
t1 = a.w * 1.0 / a.f;//必须先乘1.0变成浮点型再进行除运算
t2 = b.w * 1.0 / b.f;
if(t1 > t2) return true;
return false;
}
int main() {
int m, n;
while(cin >> m >> n) {
double sum = 0;
if(m == -1 && n == -1) break;
for(int i = 0; i < n; i++)
cin >> h[i].w >> h[i].f;
sort(h, h+n, cmp);
for(int i = 0; i < n; i++) {
if(m >= h[i].f) {
sum += h[i].w;
m -= h[i].f;
} else if(m >= 0) {
sum += h[i].w * 1.0 / h[i].f * m;
break;
}
}
cout << setiosflags(ios::fixed) << setprecision(3) << sum << endl;//保留3位小数
}
return 0;
}
830-旋转坐标系
解法:转换公式:x,y为转换前的原始坐标
x=cos(angle)*x1-sin(angle)*y1;
y=cos(angle)*y1+sin(angle)*x1;
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
double x1, y1, a1, a2, x, y;
scanf("%lf%lf%lf%lf", &x1, &y1, &a1, &a2);
x = a2 * x1 - a1 * y1;
y = a2 * y1 + a1 * x1;
printf("%.2lf %.2lf\n", x, y);
}
return 0;
}
831-签到
#include
int main() {
int n;
while(~scanf("%d", &n)) {
int a, s = 0;
for(int i = 1; i <= n; i++) {
scanf("%d", &a);
s = s + a;
}
s = s + 5 * (n / 5) + n;
printf("%d\n", s);
}
return 0;
}
833-取石子(七)
解法:当石子数为 1,2时第一个必胜;其他情况第二个人在不走错的情况下肯定是胜利的。
#include
int main() {
int a;
while(~scanf("%d", &a)) {
if(a == 1 || a == 2)
printf("Hrdv\n");
else
printf("Yougth\n");
}
return 0;
}
834-组队
#include
#include
using namespace std;
int main() {
int n;
while(~scanf("%d", &n)) {
int a[55], sum = 0, max, b[3], biao;
for(int i = 0; i < n; i++)
scanf("%d", &a[i]);
for(int i = 0; i < 3; i++) {
max = 0;
for(int j = 0; j < n; j++) {
if(max < a[j]) {
max = a[j];
biao = j;//记录最大值下标
}
}
sum += a[biao];
a[biao] = 0;//把已找到的最大值清0,下一轮找出比其小的最大值
b[i] = biao; //把最大值下标放进数组
}
sort(b, b + 3);
printf("%d %d %d %d\n", b[0]+1, b[1]+1, b[2]+1, sum);//输入时,从0开始,下标是从一开始
}
return 0;
}
835-吉他练习
#include
#include
int main() {
char a[205];
while(~scanf("%s", a)) {
int flg = 0, len = strlen(a), i = 0;
for(i = 0; i+4 < len; i += 5) {
if(a[i] == 'C' && a[i+1] == 'A' && a[i+2] == 'm' && a[i+3] == 'E' && a[i+4] == 'm') continue;
else {
flg = 1;
break;
}
}
switch(len - i) {
case 1:
if(a[len-1] != 'C') flg = 1;
break;
case 2:
if(a[len-2] != 'C' || a[len-1] != 'A') flg = 1;
break;
case 3:
if(a[len-3] != 'C' || a[len-2] != 'A' || a[len-1] != 'm') flg = 1;
break;
case 4:
if(a[len-4] != 'C' || a[len-3] != 'A' || a[len-2] != 'm' || a[len-1] != 'E') flg = 1;
break;
}
if(flg) printf("NO\n");
else
printf("YES\n");
}
return 0;
}
838-物理实验室
#include
int main() {
double a, b;
while(scanf("%lf%lf", &a, &b) != EOF)
printf("%.2lf\n", 1/(1/a+1/b));
return 0;
}
840-吃花生
#include
#include
#include
using namespace std;
int main() {
int t;
scanf("%d", &t);
getchar();
while(t--) {
int a[7];
char s[10];
for(int i = 0; i < 7; i++)
scanf("%d", &a[i]);
scanf("%s", s);
sort(a, a + 7);
if(strcmp(s, "Monday") == 0) printf("%d\n", a[0]);
else if(strcmp(s, "Tuesday") == 0) printf("%d\n", a[1]);
else if(strcmp(s, "Wednesday") == 0) printf("%d\n", a[2]);
else if(strcmp(s, "Thursday") == 0) printf("%d\n", a[3]);
else if(strcmp(s, "Friday") == 0) printf("%d\n", a[4]);
else if(strcmp(s, "Saturday") == 0) printf("%d\n", a[5]);
else
printf("%d\n", a[6]);
}
return 0;
}
841-最高位数字
解法:题目是这样转化的。
首先用科学计数法来表示 N^N = a*10^x; 比如N = 3; 3^3 = 2.7 * 10^1;
我们要求的最右边的数字就是(int)a,即a的整数部分;
OK, 然后两边同时取以10为底的对数 lg(N^N) = lg(a*10^x)
化简 N*lg(N) = lg(a) + x; 继续化 N*lg(N) - x = lg(a) a = 10^(N*lg(N) - x);
现在就只有x是未知的了,如果能用n来表示x的话,这题就解出来了。
又因为,x是N^N的位数。比如 N^N = 1200 ==> x = 3;
实际上就是 x 就是 lg(N^N) 向下取整数,表示为[lg(N^N)]
ok a = 10^(N*lg(N) - [lg(N^N)]); 然后(int)a 就是答案了。
#include
#include
int main() {
int n;
while(~scanf("%d", &n)) {
double k, ans;
k = (n * log10(n * 1.0) - (long long) (n * log10(n * 1.0)));
ans = pow(10 * 1.0, k);
printf("%d\n", (int)ans);
}
return 0;
}
842-整除的尾数
#include
int main() {
int a, b;
while(scanf("%d%d", &a, &b)) {
if(a == 0 && b == 0) break;
for(int i = 0; i < 100; i++)
if((a * 100 + i) % b == 0) printf("%02d ", i);
printf("\n");
}
return 0;
}
844-A+B Problem(V)
#include
int main() {
int m, n, a, b;
while(scanf("%d%d", &m, &n), m, n) {
int x = 0, y = 0;
while(m) {
a = m % 10;
m /= 10;
x = x * 10 + a;
}
while(n) {
b = n % 10;
n /= 10;
y = y * 10 + b;
}
printf("%d\n", x + y);
}
return 0;
}
846-ST And BE
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int b, e, key, count = 0, num;
scanf("%d%d%d", &b, &e, &key);
while(num != key) {
num = (b + e) / 2;
if(num > key) e = num;
if(num < key) b = num + 1;
count++;
}
if(count % 2 == 0) printf("Lose\n");
else
printf("Win\n");
}
return 0;
}
847-S + T
解法:要查找最大差值,先初始化最大差值为最小和最大值为第一个,然后从第二个数开始寻找最大差值不断更新,同时左边值要尽量取最大即寻找一个最大值。
#include
#define INF 9999999
using namespace std;
int main() {
int t;
cin >> t;
while(t--) {
long long n, a, index = 0, temp = -INF, left, right, max;
cin >> n;
cin >> a;
max = a;
for(long long i = 1; i < n; i++) {
cin >> a;
if(max - a > temp) {//寻找最大差值
temp = max - a;
left = index;
right = i;
}
if(a > max) {//寻找最大值
max = a;
index = i;
}
}
cout << temp << " " << left + 1 << " " << right + 1 << endl;
}
return 0;
}
848-STBE
#include
#include
char s1[30], s2[30], s3[30];//s3储存后序序列
int t;
void getPost(int l1, int r1, int l2, int r2) {
if(l1 > r1) return;
s3[t++] = s1[l1];//先储存根结点
int k = 0;
while(s1[l1] != s2[l2+k]) k++;//k记录属于左子树的元素个数
getPost(l1+k+1, r1, l2+k+1, r2);//先对右子树递归,分别提取两个s1和s2子串(最左边和最右边位置)
getPost(l1+1, l1+k, l2, l2+k-1);//再对左子树递归,分别提取两个s1和s2子串(最左边和最右边位置)
}
int main() {
while(~scanf("%s%s", s1, s2)) {
int len = strlen(s1);
t = 0;
getPost(0, len-1, 0, len-1);
for(int i = len-1; i >= 0; i--)//后序序列要逆序输出
printf("%c", s3[i]);
printf("\n");
}
return 0;
}
852-蛇形填数(二)
格式错误,一个通过的都没有,不知道有什么坑...
#include
#include
int a[1005][1005];
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
memset(a, 0, sizeof(a));
int k = 1, x = 0, y = 0, temp = n, p = n;
a[x][y] = k;
while(k < n * (n+1) / 2) {
while(a[x][y+1] == 0 && y+1 < p) a[x][++y] = ++k;//向右走
y--;//减少列数
while(a[x+1][y] == 0 && x+1 < n) {
a[++x][y] = ++k;//向左下走,列数减少,行数增加
y--;
}
y++;//将y重新变为0
while(a[x-1][y] == 0 && x-1 >= 0) a[--x][y] = ++k;//向上走
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < temp; j++)
printf(j == 0 ? "%d" : " %d", a[i][j]);
temp--;
printf("\n");
}
printf("\n");
}
return 0;
}
864-统计
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
long long n;
int count1 = 0, count2 = 0, count3 = 0, count4 = 0, a[32] = {0}, k = 0;
scanf("%lld", &n);
while(n) {
a[k++] = n % 2;//a[0]为2进制最低位
n /= 2;
}
for(int i = 0; i < 32; i++) {
if(a[i] == 1) count4++;
}
for(int i = 0; i < 32; i++) {
if(a[i] == 1) {
count1 = i + 1;
break;
} else
count3++;
}
for(int i = 31; i >= 0; i--) {
if(a[i] == 1) break;
else
count2++;
}
printf("%d %d %d %d\n", count1, count2, count3, count4);
}
return 0;
}
872-开会
#include
int main() {
int s, n, a;
while(scanf("%d%d", &s, &n) != EOF) {
while(n--) {
long long sum = 0, s1 = 1, s2 = 1;
scanf("%d", &a);
for(int i = s; i >= s - a + 1; i--)
s1 *= i;
for(int i = a; i >= 1; i--)
s2 *= i;
sum = s1 / s2;
printf("%lld ", sum);
s -= a;
}
printf("\n");
}
return 0;
}
874-签到
解法:1 1 1 1 6 1 1 1 6 1 1 1 6 ...
#include
int main() {
int m, n;
while(~scanf("%d%d", &m, &n)) {
if(n < 5) printf("%d\n", m + n);
else if((n - 5) % 4 == 0) printf("%d\n", (n - 5) / 4 * 9 + 10 + m);
else
printf("%d\n", (n - 5) / 4 * 9 + 10 + (n - 5) % 4 + m);
}
return 0;
}
880-还是打印菱形
#include
int main() {
int n;
scanf("%d", &n);
while(n--) {
int m, i, j, k, l;
char ch;
ch = 'A';
scanf("%d", &m);
m = m + 1;//先打印上半部分,行是m+1行
for(i = 1; i <= m; i++) { //行
for(j = m - i - 1; j >= 0; j--) //列,空格数量
printf(" ");//打印空格
for(k = 0; k < i; k++)
printf("%c", ch + k);//由小到大顺序打印字母
for(l = k - 2; l >= 0; l--)//上面k循环结束时k++进行到k=3(k==i),所以需要k-2
printf("%c", ch + l);
printf("\n");
}
i = i - 1;//需要少一行
while(i--) {//倒过来打印
for(j = m - i - 1; j >= 0; j--) //列,空格数量
printf(" ");//打印空格
for(k = 0; k < i; k++)
printf("%c", ch + k);//由小到大顺序打印字母
for(l = k - 2; l >= 0; l--)//上面k循环结束时k++进行到k=3(k==i),所以需要k-2
printf("%c", ch + l);
printf("\n");
}
}
return 0;
}
887-Repair the Wall
#include
#include
using namespace std;
int main() {
long long L;
int n, a[1005];
while(cin >> L >> n) {
int sum = 0, num = 0;
for(int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n);
if(a[n - 1] >= L) cout << "1" <= 0 && sum < L; --i, ++num) sum += a[i];
if(num == n) cout << "impossible" << endl;
else
cout << num << endl;
}
}
return 0;
}
889-求距离
890-分东西
解法:这个题目,装箱时这样1 2 4 8 ....是满足一个客人,这样正好覆盖所有的数1,2,3,4,5,6....而起这样不会漏数,因此只需考虑这样装,最多能装多少就行了一定要大于,因为第一个箱子是装2个商品,要留出一个商品来凑。
#include
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int m, n;
scanf("%d%d", &m, &n);
if(pow(2, n) > m) printf("Yes\n");
else
printf("No\n");
}
return 0;
}
892-买牛奶
#include
struct milk {
char name[10];
double prize;
int weight;
};
int main() {
int t;
scanf("%d", &t);
while(t--) {
int n;
double sum = 0;
scanf("%d", &n);
milk s[n];//以结构体名称定义结构体数组
for(int i = 0; i < n; i++) {
scanf("%s%lf%d", s[i].name, &s[i].prize, &s[i].weight);
}
for(int i = 0; i < n; i++) {
if(s[i].weight <= 300 && s[i].prize >= 1.5 && s[i].prize <= 5) {
printf("%s\n", s[i].name);
sum += s[i].prize;
}
}
if(sum == 0) //买不了牛奶则总价为0
printf("-1\n");
else
printf("%.1lf\n", sum);//保留一位小数
}
return 0;
}
893-十字架
解法:只有1和0出现,十字架的五个数相乘为一。
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int a[7][7], count = 0, i, j;
for(i = 0; i < 7; i++)
for(j = 0; j < 7; j++)
scanf("%d", &a[i][j]);
for(i = 1; i < 6; i++)
for(j = 1; j < 6; j++)
if(a[i][j] * a[i][j-1] * a[i][j+1] * a[i-1][j] * a[i+1][j] == 1) count++;
printf("%d\n", count);
}
return 0;
}
899-关于圆锥
解法:设母线长L,高h,底面半径r:
h*h=L*L-r*r,
pi*r*r+pi*r*L=s,表面积公式
L=s/(pi*r)-r,由上行转换而来
h*h=[s/(pi*r)-r]*[s/(pi*r)-r]-r*r=s*s/((pi*r)*(pi*r))-2*s/pi,
v=pi*r*r*h/3
=pi*r*r/3*√[s*s/((pi*r)*(pi*r))-2*s/pi]
令X=pi*r*r,
V=X/3*√[s*s/((pi*r)*(pi*r))-2*s/pi]
=1/3*√[s*s*X/pi-2*s*X*X/pi]
=1/3*√[-2*s*(X*X-s*X/2+s*s/16-s*s/16)/pi]
=1/3*√{[s*s*s/8-2*s*s(X-s/4)2]/pi}
-2*s*(X-s/4)2<=0,-2*s(X-s/4)2=0时,即X=s/4,V有最大值,
此时,pi*r*r=s/4,r=√s/(4*pi).
又因为:h*h=[s/(pi*r)-r]*[s/(pi*r)-r]-r*r=s*s/((pi*r)*(pi*r))-2*s/pi,
h = sqrt(2*s/pi);
#include
#include
#define pi 3.1415926
int main() {
double s;
while(~scanf("%lf", &s)) {
double v, h, r;
r = sqrt(s / (4*pi));
h = sqrt(2*s/pi);
v = (pi*r*r*h) / 3;
printf("%.2lf %.2lf %.2lf\n", v, h, r);
}
return 0;
}
905-卡片游戏
#include
#include
int main() {
int t;
scanf("%d", &t);
getchar();
while(t--) {
char s[300], a[300], min = '9';
int k = 0, left = 100, right = 100;
scanf("%s", s);
a[100] = s[0];
for(int i = strlen(s)-1; i >= 0; i--) {
if(s[i] != '0' && s[i] < min) {
min = s[i];//记录出现的最小非0值
k = i;//记录最后出现的最小非0值位置
}
}
for(int i = 1; i < strlen(s); i++) {
if(i < k) {//在最小非0值前面位置的卡片
if(s[i] <= a[left]) {
left--;
a[left] = s[i];
} else {
right++;
a[right] = s[i];
}
} else if(i == k) { //将最小非0值放在最左边,固定
left--;
a[left] = s[i];
} else {//其余全部放在右边
right++;
a[right] = s[i];
}
}
for(int i = left; i <= right; i++)
printf("%c", a[i]);
printf("\n");
}
return 0;
}
双端队列做法:
#include
#include
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
string s;
cin >> s;
int len = s.size(), m = len - 1;
char min = '9';
for (int i = 0; i < len; i++) {
if (s[i] != '0' && s[i] <= min) {
min = s[i];
m = i;
}
}
deque t;
for (int i = 0; i < m; i++) {
if (s[i] <= t[0]) t.push_front(s[i]);
else t.push_back(s[i]);
}
t.push_front(s[m]);
for (int i = m + 1; i < len; i++) t.push_back(s[i]);
deque::iterator p;
for(p = t.begin(); p != t.end(); p++) cout << *p;
cout << endl;
}
return 0;
}
906-杨辉三角
#include
int main() {
int N;
while(~scanf("%d", &N)) {
int a[N][N];
for (int i = 0; i < N; i++) {
a[i][0] = 1;
a[i][i] = 1;
}
for (int i = 1; i < N; i++)
for (int j = 1; j < i; j++)
a[i][j] = a[i-1][j-1] + a[i-1][j];
for (int i = 0; i < N; i++) {
for (int j = 0; j <= i; j++)
printf("%d ", a[i][j]);
printf("\n");
}
printf("\n");
}
return 0;
}
910-英语角
#include
#include
#include
using namespace std;
int main() {
string str;
set k;
while(cin >> str) { //以EOF结束
k.insert(str);
}
set::iterator iter;
for(iter = k.begin(); iter != k.end(); iter++)
cout << *iter << " ";
cout << endl << k.size() << endl;
return 0;
}
map容器做法:
#include
#include
912-领帽子
#include
long long Fun(int n) {
if(n == 1) return 0;
if(n == 2) return 1;
return (n-1) * (Fun(n-1) + Fun(n-2));
}
int main() {
int n;
while(~scanf("%d", &n))
printf("%lld\n", Fun(n));
return 0;
}
915-+-字符串
#include
#include
int main() {
char a[5005], b[5005];
while(~scanf("%s%s", a, b)) {
int sum = 0, la = strlen(a), lb = strlen(b), s1 = 0, s2 = 0;
char t;
if(la != la) printf("-1\n");
else {
for(int i = 0; i < la; i++) {
if(a[i] == '+') s1++;
if(b[i] == '+') s2++;
}
if(s1 != s2) printf("-1\n");
else {
for(int i = 0; i < la; i++) {
if(a[i] != b[i]) {//一旦发现某位置的字符不同
for(int j = i; j < la-1; j++) {//a串往后查找,每循环一次就代表交换了一次
sum++;
if(a[i] != a[j+1]) {//发现与当前位置不同的(即与b串相同的),则需要交换位置,交换后与b串当前位置字符相同
t = a[i];
a[i] = a[j+1];
a[j+1] = t;
break;
}
}
}
}
printf("%d\n", sum);
}
}
}
return 0;
}
918-一个奇葩的累积求和问题
#include
int main() {
int t;
scanf("%d", &t);
while(t--) {
int a, b, sum;
scanf("%d", &a);
b = a + 1;
sum = (a/2)*b + a%((a+1)/2) + a%2;
printf("%d\n", sum);
}
return 0;
}
921-分数矩阵
#include
int main() {
double n;//后面计算double型sum时会用到n,所以也要double型
while(scanf("%lf", &n), n) {
double sum = 0;
for(int i = 1; i < n; i++) {
sum += 2 *(n - i) / (i + 1);
}
printf("%.2lf\n", sum + n);
}
return 0;
}
925-国王的烦恼
解法:因为join函数是连桥,所以反过来想更好,拆桥就变成了建桥。该桥拆完后两点不连通等同于建好该桥后两点从不通变通,就变成每添加一个桥是否连通一个新的区域。要先排序,使用日期更久的要先建,因为断桥时使用日期久的会保留的更久。
#include
#include
#include
using namespace std;
int pre[100005];
struct land {
int a, b;
int date;
}t[100005];
int cmp(land a, land b) { //最后一天不管怎么样肯定要抗议因为唯一的桥最后也断了,所以日期从大到小排序
return a.date > b.date;
}
int find(int x) { //查找根节点(即最上级)
if(x == pre[x]) return pre[x];
return pre[x] = find(pre[x]);
}
int join(int x, int y) {
int fx = find(x), fy = find(y);
if(fx != fy) {
pre[fx] = fy;
return 1;
}
return 0;
}
int main() {
int n, m;
while(cin >> n >> m) {
int count = 0, lastday = -1;//lastday表示上一次是在第几天进行反抗的,同一天断桥就不用反复抗议(只抗议一次就行)
for(int i = 0; i < m; i++)
cin >> t[i].a >> t[i].b >> t[i].date;
sort(t, t+m, cmp);
for(int i = 1; i <= n; i++)
pre[i] = i; //初始化
for(int i = 0; i < m; i++) {
int way = join(t[i].a, t[i].b);//way=0表示之前有桥,是连通的
if(way && t[i].date != lastday) { //要那种之前不连通的(即没有其他路径到达的),这桥断了后就会抗议
//并且上一次暴动不是该同一天
cout << t[i].a << "和" << t[i].b << "断开,不能到达,抗议" << endl;
count++;
lastday = t[i].date;
}
}
cout << count << endl;
}
return 0;
}
上错
你可能感兴趣的:(ACM之路)