1. A set of integers, sorted from small to large. There are n sets of test numbers, 8 numbers in each line, and the output is sorted.
Input example:
2
5 7 3 2 11 23 6 33 9 8
5 2 1 3 0 4
Output:
2 3 5 6 7 11 23 33
0 1 2 3 4 5 8 9
Reference Code:
#include<stdio.h>
sort(int a[8],int n)
{
int i,j,k;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
}
}
}
int main()
{
int n,i=0,j;
scanf("%d",&n);
while(i<n)
{
int b[8]={
0};
for(j=0;j<8;j++)
scanf("%d",&b[j]);
sort(b,8);
for(j=0;j<8;j++)
printf("%d ",b[j]);
i++;
}
}
2. Helen's formula calculates the area. There are n sets of test numbers. First determine whether it is a triangle. If it is not a triangle, it will output NaN. If it is a triangle, it will output the area of the triangle. (output two decimal places)
Input example:
2
1.0 2.0 3.0
3 4 5
Output:
NaN
6.00
Helen's formula:
There is a triangle with side lengths a, b, and c respectively. The area S is calculated as follows: where p is the semi-perimeter
p=(a+b+c)/2.
Reference code:
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,s,p;
int i=0,n;
scanf("%d",&n);
while(i<n)
{
scanf("%f%f%f",&a,&b,&c);
if((a+b>c)&&(a+c>b)&&(b+c>a))
{
p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
printf("%.2f\n",s);
}
else
printf("NaN\n");
i++;
}
}
3. Determine whether the IP address is legal. There are n sets of test numbers, which are input in the form of strings in the format abcd. Each is an integer. The output determines whether it is a valid IP. Each number is between [0,255]. The legal output is Yes, and the illegal output is No.
Input example:
2
1.2.3.4
172.168.0.300
Output:
Yes
No
Reference Code:
#include<stdio.h>
#include <string.h>
int main()
{
int n,i;
int flag=1,a=0;
char s[20];
scanf("%d",&n);
while(n--)
{
scanf("%s",&s);
for(i=0;s[i]!=0;i++)
{
if(s[i]!='.')
a=a*10+s[i]-'0';
else
{
if(a<0||a>255)
{
flag=0;
break;
}
a=0;
}
}
if(flag==0 || a<0 || a>255)
printf("No\n");
else
printf("Yes\n");
}
}
4. Find N prime numbers starting from M.
Input example:
4 3
Output:
5
7
11
Reference code:
Method 1:
#include<stdio.h>
int judge(int a)
{
int i,k=1;
for(i=2;i<a;i++)
{
if(a%i==0)
k=0;
}
return k;
}
int main()
{
int m,n,i=0,j=0;
scanf("%d%d",&m,&n);
for(i=m;j<n;i++)
{
if(judge(i)==1)
{
printf("%d\n",i);
j++;
}
}
}
Method Two:
#include<stdio.h>
#include<math.h>
int judge(int m)
{
int i,k;
k=(int)sqrt(m);
for(i=2;i<=k;i++)
{
if(m%i==0)
return 0;
}
return 1;
}
int main()
{
int n,m;
scanf("%d%d",&m,&n);
while(n--)
{
while(m++)
{
if(judge(m)==1)
{
printf("%d\n",m);
break;
}
}
}
return 0;
}
5. Find the difference between any two days in a year. Input N sets of test data, (make sure the date of the next day is later than the previous day) and output the number of days in the two-day period.
Input example:
2
2019 1 1 2019 1 2
2016 1 1 2016 3 1
Output:
2
61
Reference Code:
#include<stdio.h>
int sum(int y,int m,int d)
{
int i,s=0;
char a[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31};
for(i=1;i<y;i++)
{
if(i%4==0 && i%100!=0 || i%400==0)
s+=366;
else
s+=365;
}
if(y%4==0 && y%100!=0 || y%400==0)
a[2]=29;
for(i=1;i<m;i++)
s+=a[i];
s+=d;
return s;
}
int main()
{
int n,y1,y2,m1,m2,d1,d2,s1,s2;
scanf("%d",&n);
while(n--)
{
scanf("%d%d%d%d%d%d",&y1,&m1,&d1,&y2,&m2,&d2);
s1=sum(y1,m1,d1);
s2=sum(y2,m2,d2);
printf("%d\n",s2-s1+1);
}
}