2018西工大计算机夏令营机试代码参考

(题目来源--西工大计算机微信公众号)

1.输入三角形的三边,判断是否构成三角形

Input:
2
3.1 4 5
1 2 3
Output:
yes
no
#include<stdio.h>
#include<stdlib.h>
int main(){
	int n;
	scanf("%d",&n);
	while(n--){
			double a,b,c;
	                scanf("%lf%lf%lf",&a,&b,&c);
	                if(a+b<=c||a+c<=b||b+c<=a)
				printf("no\n");
			else
				printf("yes\n");
	}
	system("pause");
	return 0;
}

2.给定n组数,每组m个,对每组数进行从小到大排序;

Input:
2 4
3 5 2 8
2 7 9 8

Output:
2 3 5 8
2 7 8 9
2018
2019
原题

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
	int n;
	scanf("%d",&n);
	while(n--){
			int yearB,monB,dayB,hourB,minB,yearA,monA,dayA,hourA,minA;
	                scanf("%d%d%d%d%d",&yearB,&monB,&dayB,&hourB,&minB);
	                minA=minB;
	                if(hourB>=16){
					yearA=yearB;
					monA=monB;
					dayA=dayB;
					hourA=hourB-16;
	                }
	                else{
					if(dayB==1){
							if(monB==1){
								yearA=yearB-1;
								monA=12;
								dayA=31;
								hourA=hourB+24-16;
							}

							if(monB==2||monB==4||monB==6||monB==8||monB==9||monB==11){
							        yearA=yearB;
								monA=monB-1;
								dayA=31;
								hourA=hourB+24-16;
							}

					                else if (monB == 5 || monB == 7 || monB == 10 || monB == 12){
					                	yearA=yearB;
								monA=monB-1;
								dayA=30;
								hourA=hourB+24-16;
					                }
					                else if(monB==3){
									if(yearB% 400 == 0 || (yearB % 100 != 0 && yearB % 4 == 0)){
										yearA=yearB;
									        monA=monB-1;
									        dayA=29;
									        hourA=hourB+24-16;
									}
									else{
										yearA=yearB;
									        monA=monB-1;
									        dayA=28;
									        hourA=hourB+24-16;
									}
					                }

					}

					else{
							yearA=yearB;
							monA=monB;
							dayA=dayB-1;
							hourA=hourB+24-16;
					}
	                }
	                printf("%d %02d %02d %02d %02d\n",yearA,monA,dayA,hourA,minA);
	}
	system("pause");
	return 0;
}

 4.利用求根公式求解一元二次方程,无解输出NaN,有解则输出结果(四舍五入,保留2位小数)

Input
3
1 2 1
1 -2 1
1 1 1
Output:
-1.00 -1.00
1.00 1.00 
NaN
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(){
	int n;
	scanf("%d",&n);
	while(n--){
			int a,b,c;
			double rest1,rest2;
	                scanf("%d%d%d",&a,&b,&c);
	                if(b*b-4*a*c<0)
				printf("NaN\n");
			else if(b*b-4*a*c==0){
					rest1=-b/(2*a);
			                rest2=-b/(2*a);
			                printf("%.2lf %0.2lf\n",rest1,rest2);
			}
			else{
				        rest1=(-b+sqrt(b*b-4*a*c))/(2*a);
				        rest2=(-b-sqrt(b*b-4*a*c))/(2*a);
					printf("%.2lf %0.2lf\n",rest1,rest2);
			}
	}
	system("pause");
	return 0;
}

5.输入行数,再在每行输入一个表达式,得出结果

Input:
3
1+1
2.2/3
1+2*3+2

Output:
2
0.7
9
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
        while(n--)
        {
            double a[100],sum=0;
            char c;
            int i=0;
            scanf("%lf",&a[0]);
            c=getchar();
            while(c!='\n')
            {
                double temp;
                scanf("%lf",&temp);
                switch(c)
                {
                    case '+':a[++i]=temp;break;
                    case '-':a[++i]=-temp;break;
                    case '*':a[i]*=temp;break;
                    case '/':a[i]/=temp;break;
                }
                c=getchar();
            }
            for(int j=0;j<=i;j++)
            {
                sum=sum+a[j];
            }
            if(sum-int(sum)<=1e-6) printf("%.0f\n",sum);
            else printf("%.1f\n",sum);
        }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lixiaohulife/article/details/89764341