Top

河南农业大学热身赛标程


河南农业大学热身赛标程

博主CSDN

A.宅宅的排位赛

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 10;
const int mod = 1e9 + 7;
int main(){
int t;
scanf("%d", &t);
while (t--){
int a, suma = 0, sumb = 0;
for (int i = 0; i <= 13; i++){ //suma记录A1~N1的和
scanf("%d", &a);
suma += a;
}
for (int i = 0; i <= 13; i++){ //sumb记录A2~N2的和
scanf("%d", &a);
sumb += a;
}
printf("%d\n", suma - sumb);
}
return 0;
}

B.冒泡排序

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
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
int a[120];
int main(){
int t;
scanf("%d", &t);
while (t--){
int n, min_ = 9999,id=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++){
min_ =a[i];
id = i;
for (int j = i; j < n; j++){
if (a[j] <= min_)min_ = a[j], id = j;
}
swap(a[i], a[id]); //把为排序的最小值换到当前i位
}
for (int i = 0; i < n; i++)printf("%d\n", a[i]);
}
return 0;
}

C.小刘数字母

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
#include<bits/stdc++.h> //万能头文件
using namespace std;
char s[10050];
int main(){
int t;
char ch;
scanf("%d",&t);
while(t--){
scanf("%s",s);
getchar(); //接收空格
scanf("%c",&ch);
int cnt=0; //记录出现次数
int len=strlen(s); //求出输入串长度
for(int i=0;i<len;i++){
if(s[i]==ch)cnt++; //如果ch出现就cnt++
}
printf("%d\n",cnt);
}
return 0;
}

D.HH的LCM

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
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
scanf("%d",&t);
//GCD解法
while(t--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a*b/__gcd(a,b)); //a,b的最小公倍数=a*b/(a,b的最大公因数)
}
// 暴力解法
/*while(t--){
int a,b;
scanf("%d%d",&a,&b);
for(int i=1;i<=a*b;i++){ //a*b肯定能被整除a,b,所以LCM不大于a*b
if(i%a==0&&i%b==0){ //第一个能被整除的a,b的便是他们的最小公倍数
printf("%d\n",i);
break;
}
}
}*/
return 0;
}

E.斐波那契数

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
29
30
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 10;
const int mod = 1e9 + 7;
int Fib[maxn];
int main(){
int cnt = 1;
Fib[0] = 0;
Fib[1] = 1;
for (int i = 2; Fib[i - 1] <= 100000; i++, cnt++){ //暴力打表求出所有小于100000的斐波那契数
Fib[i] = Fib[i - 1] + Fib[i - 2];
}
int t;
scanf("%d", &t);
while (t--){
int n, ans = 0; //ans用来记录数量
scanf("%d", &n);
for (int i = 26; i >= 1; i--){ //对于n从最大的Fib数列往前扫,
if (n >= Fib[i])n -= Fib[i], ans++; //如果发现不大于n的Fib数,就用n-Fib[i],ans++
}
/*
贪心证明:因为对于任何n,比n小的且最大的Fib[i]一定满足 Fib[i]<n<2*Fib[i]
因为 Fib[i-1]+Fib[i]=Fib[i+1]<2*Fib[i]
*/
printf("%d\n", ans);
}
return 0;
}

F.辉辉学长爱喝水

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 10;
const int mod = 1e9 + 7;
int main(){
double n, m;
scanf("%lf%lf", &n, &m);
int a = ceil(n / (m * 2));
if (n == 0)printf("0\n"); // 需要多少步 向上取整
else if (m == 0)printf("No answer!\n");
else if(n-a*m<=0){
printf("1\n");
}
else printf("%d\n", a + 1);
return 0;
}

G.打麻将

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
bool ok=0; //胡牌标记
bool vis[20]; //标记这张牌有没有被使用
char a[20];
int d[20][5];
int ttt[11];
void dfs(int cur,char *str,int cnt){
if(cur>12)return; //因为组成刻子和顺子的牌需要12张,如果超过了就return
if(cur==12&&cnt==4){ //如果刻子和顺子用了12张牌且刻子和顺子数一共4,则进一步判断另外两张牌是不是对子
int a=0,b=0;
for(int i=1;i<=14;i++)if(!vis[i]){vis[i]=true,a=i;break;} //遍历出剩余第一张牌
for(int i=1;i<=14;i++)if(!vis[i]){vis[i]=true,b=i;break;} //遍历出剩余第二张牌
if(str[a]==str[b]&&(str[a]!=' '&&str[b]!=' ')) //如果str[a]等于str[b]能胡牌 ,ok=true
ok=true;
vis[a]=vis[b]=false;
return ;
}
for(int i=1;i<=12&&!ok;i++) //dfs遍历这14张牌所有组合情况
if(!vis[i])
for(int j=i+1;j<=13&&!ok;j++)
if(!vis[j])
for(int k=j+1;k<=14&&!ok;k++){ //挑3个牌str[i],str[j],str[k]
if(!vis[k])
if((str[i]==str[j]&&str[j]==str[k])||(str[i]+1==str[j]&&str[j]+1==str[k])){ //看能否成顺子或刻子
vis[i]=vis[j]=vis[k]=true; //把选的牌标记为已用
dfs(cur+3,str,cnt+1);//成了的话,cur+3,cur代表选的牌数,cnt+1,cnt代表现在顺子跟刻子的总和
vis[i]=vis[j]=vis[k]=false; //把牌标记为未用
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%s",&a[1]);
//cnt++;
int tt_=0;
ok=false;
memset(ttt,0,sizeof(ttt));
for(int i=1;i<=14;i++){ //判断是否是合法牌
ttt[a[i]-'0']++;
if(ttt[a[i]-'0']>4){
printf("The data is error!\n");
tt_=1;
break;
}
}
if(tt_)continue;
sort(a+1,a+15); //把牌从小到大排好序
dfs(0,a,0);
if(!ok)printf("NO\n");
else printf("YES\n");
}
return 0;
}


未经允许不得转载: Anoyer's Blog » 河南农业大学热身赛标程