2014第五届蓝桥杯C/C++本科B组试题及答案
解答:
#include<stdio.h>
int main()
{
int x,y;
double m=0;
for(x=0,y=42;x<35 || x>y;)
{
y = (int)(82.3-2.3*x)/1.9;
m= 2.3*x+1.9*y;
if(m==82.3)
break;
x = x+1;
y = y-2;
}
printf("%d\n%d\n",x,y);
return0;
}
解答:
#include<stdio.h>
#include<math.h>
int main()
{
printf("%d"pow( 2.0 ,10.0 ) + 1); return0;
}
解答:
#include<stdio.h>
int sum=0;
int f(int a,int b,int c){
if(a>0)
f(a-1,b,c*2);
if(b>0)
f(a,b-1,c-1);
if(a==0&&b==0&&c==1)
sum=sum+1;
return sum;
}
int main(){
f(5,9,2);
printf("%d",sum);
}
解答:
if(r>0)return i;
解答:
f(a, rank - 1, row,col+w/2);
解答:
#include<stdio.h>
int main(void)
{
int i,j,k,l;
int z;
for(i=1;i<10;i++)
for(j=1;j<10;j++)
for(k=1;k<10;k++)
for(l=1;l<10;l++)
if(i!=j&&k!=l)
if((float)i*k/(j*l)==(float(i*10+k)/(j *10+l)))
{printf("%d/%d,%d/%d\n",i,j,k,l);
z++;
}
printf("%d",z);
return0;
}
答案:10
解答:
//思路为将蚂蚁的碰撞理解为互相穿过#include<stdio.h>
int main(){
int n,m=1;
int i,j=0,k=0;
int a[100],b[100],c[100];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
if(a[i]<0){
b[j]=-a[i];
j++;
}
else{
c[k]=a[i];
k++;
}
}
if(a[1]<0)
a[1]=-a[1];
for(i=0;i<j;i++){
if(b[i]>a[1])
m++;
}
for(i=0;i<k;i++){
if(c[i]<a[1])
m++;
}
printf("%d\n",m);
}
解答:
#include <stdio.h>
int n, m, k, count = 0, map[51][51];
void dfs(int x, int y, int max, int num)
{
if(x == n || y == m || num > k) return;
else if(x == n - 1 && y == m - 1)
{
if(num == k) count++;
else if(num == k - 1 && map[n - 1][m - 1] > max) count++;
count %= 1000000007;
}
else
{
if(map[x][y] > max)
{
dfs(x + 1, y, map[x][y], num + 1);
dfs(x, y + 1, map[x][y], num + 1);
}
dfs(x + 1, y, max, num);
dfs(x, y + 1, max, num);
}
}
int main(void)
{
int i, j;
scanf("%d%d%d", &n, &m, &k);
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
scanf("%d", &map[i][j]);
}
}
dfs(0, 0, 0, 0);
printf("%d\n", count);
return0;
}
解答:
#include <stdio.h>
#include <stdlib.h>
struct children
{
int height;
int step;
}ch[100001], s_ch[100001], temp;
void swap(struct children *a, struct children *b)
{
a->step++;
b->step++;
temp = *a;
*a = *b;
*b = temp;
}
int cmp(const void*a, const void*b)
{
return(*(struct children *)a).height - (*(struct children *)b).height;
}
int main(void)
{
int n, i, j, k, count = 0;
scanf("%d", &n);
for(i = 0; i < n; i++)
{
scanf("%d", &ch[i].height);
ch[i].step = 0;
s_ch[i] = ch[i];
}
qsort(s_ch, n, sizeof(struct children), cmp);
for(i = 0; i < n; i++)
{
for(j = i; j < n; j++)
{
if(s_ch[i].height == ch[j].height) break;
}
for(k = j; k>i; k--)
{
swap(&ch[k], &ch[k - 1]);
}
}
for(int i = 0; i < n; i++)
count += (ch[i].step + 1) * ch[i].step / 2;
printf("%d\n", count);
return0;
}。