Top

Henau-2018训练题1


小刘爱复读

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 10;
const int mod = 1e9 + 7;
//把诗歌存入字符串
char s[maxn] = "I am the bone of my language repeater plastic is my body and wire is my blood I have created over a thousand copies Unknown to Death Nor known to Life Have withstood pain to create copies Yet,those hands will never hold anything So as I pray,unlimited repeat works";
int main(){
int len = strlen(s), cnt = 0; //cnt表示字母个数
for (int i = 0; i < len; i++){//遍历一遍字符串
if ((s[i] >= 'a'&&s[i] <= 'z') || (s[i] >= 'A'&&s[i] <= 'Z'))cnt++; //如果当前字符是个字母,则cnt++;
}
printf("%d\n", cnt);
return 0;
}

洋洋爱模仿

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
char s[maxn], t[maxn];
int main(){
int T;
scanf("%d", &T);
while (T--){
int ans = 0; //ans表示s,t串相同位相同字符个数
scanf("%s", s);
scanf("%s", t);
int len = strlen(s); //获取串长度
for (int i = 0; i < len; i++) //for遍历字符串
if (s[i] == t[i])ans++; //如果发现一个s[i]和t[i]相同,ans++;
printf("%.2f\n", (double)ans / (double)len); //ans/总字符数=相似度
}
return 0;
}

小温爱蔬菜

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
int sum[maxn]; //前缀和
int main(){
int t;
scanf("%d", &t);
while (t--){
int n, a, m;
scanf("%d%d", &n, &m);
sum[0] = 0;
for (int i = 1; i <= n; i++){
scanf("%d", &sum[i]);
sum[i] += sum[i - 1]; //获取a数组的前缀和表
}
while (m--){
int l, r;
scanf("%d%d", &l, &r);
//前缀和性质:sum[r]-sum[l-1]=a[l]+....+a[r]
printf("%d\n", sum[r] - sum[l - 1]);
}
}
return 0;
}

洋洋爱密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
int a[maxn];
bool cmp(int a, int b){
return a > b;
}
int main(){
int t;
scanf("%d", &t);
while (t--){
int n, cnt = 0, ans = 0; //ans用来记录当前所花费金额
scanf("%d", &n);
for (int i = 0; i < n; i++){
scanf("%d", &a[i]);
}
sort(a, a + n, cmp); //先对物品价格从大到小排序
for (int i = 0; i < n; i++){
ans += a[i];
if (ans >= 30)cnt++; //如果sum>=30,则cnt++,输入密码次数cnt++
}
printf("%d\n", cnt);
}
return 0;
}


未经允许不得转载: Anoyer's Blog » Henau-2018训练题1