一、在线测试
1、摘苹果
#include <stdio.h>
#include <math.h>
#define N 10
int GetApple(int a[], int height, int n);
int main()
{
int a[N];
int i, height;
for (i = 0; i < N; i++)
scanf("%d", &a[i]);
//getchar();
scanf("%d", &height);
printf("%d\n", GetApple(a, height, N));
return 0;
}
int GetApple(int a[], int height, int n)
{
int i, counter=0;
for (i=0; i<n; i++)
{
if (a[i] <= (height + 30))
counter++;
}
return counter;
}
2、好数对
#include <stdio.h>
#include <math.h>
#define N 1000
int GetNumbers(int a[], int n);
int main()
{
int n, i;
int a[N];
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("%d\n", GetNumbers(a, n));
return 0;
}
int GetNumbers(int a[], int n)
{
int i, j, x;
int temp, counter=0;
for (i = 0; i < n - 1; i++)
for (j = i + 1; j < n; j++)
{
temp = a[i] + a[j];
for (x = 0; x < n; x++)
if (a[x] == temp)
counter++;
}
return counter;
}
3、组合三位数
#include <stdio.h>
#include <math.h>
#define N 1000
void GetNumber(int a[]);
int main()
{
int a[10];
GetNumber(a);
return 0;
}
void GetNumber(int a[])
{
int temp;
int i,j, m,n;
for (i = 102; i <= 333; i++)
{
temp = i;
for (j = 0; j < 3; j++)
{
a[j] = temp % 10;
temp /= 10;
}
temp = 2 * i;
for (j = 3; j < 6; j++)
{
a[j] = temp % 10;
temp /= 10;
}
temp = 3 * i;
for (j = 6; j < 9; j++)
{
a[j] = temp % 10;
temp /= 10;
}
for(m =0 ; m<8; m++)
for (n = m + 1; n < 9; n++)
{
if (a[m] == a[n])
goto A;
}
printf("%d,%d,%d\n", i, i*2, i*3);
A: ;
}
}
4、求100以内的最大素数
#include <stdio.h>
#include <math.h>
int IsPrime(int n);
int main()
{
int a[10], n;
int i, count = 0, sum = 0 ;
printf("Input n(n<=500):");
scanf("%d", &n);
for (i = n; i >= 2; i--)
{
if (IsPrime(i))
{
a[count] = i;
count++;
sum += i;
if (count == 10)
break;
}
}
for (i = 0; i < count; i++)
printf("%6d", a[i]);
printf("\nsum=%d\n", sum);
return 0;
}
int IsPrime(int n)
{
int i, temp;
if (n == 2)
return 1;
else
{
for (i = 2; i <= sqrt(n + 1); i++)
if (n % i == 0)
return 0;
return 1;
}
}
网友评论