刘汝佳第一章习题

1-1 输入3个整数,输出他们的平均值,保留3位小数

#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
    float a, b,c;
    cin >> a >> b >> c;
    printf("%.3f", (a+b+c)/3);
    return 0;
}

1-2 输入华氏温度,输出对应的摄氏温度,保留3位小数

#include <iostream>
#include <stdio.h>
using namespace std;

int main(void)
{
    float f;
    cin >> f;
    printf("%.3f", 5 * ( f - 32 ) / 9 );
    return 0;
}

1-3 输入正整数,输出1+2+…+n的值

#include <iostream>
using namespace std;

int main(void)
{
    int n;
    cin >> n;
    int sum = 0;
    for (int i = 1; i <= n; i++){
        sum += i;
    }
    cout << sum << endl;
}

1-4 正弦和余弦,输入正整数n(n<360),输出n度的正弦、余弦值

#include <stdio.h>
#include <math.h>
int main( void )
{
     while(1){
         int n;
         printf("Please give an angle\n");
         scanf("%d", &n);
         if ( n < 0 || n >= 360 ) {
             printf("error\n");
             continue;
         } else {
             printf("cos(a)=%f, sin(a)=%f\n", cos(n), sin(n));
         }
     }
}

1-5 打折 一件衣服95元,若消费300元,可打85折。输入购买衣服件数,输出需要支付金额,保留两位小数。

#include <stdio.h>

int main( void ) 
{
    while(1){
        printf("Give a number\n");
        int n;
        scanf("%d", &n);
        if (n <=0) {
            printf("error\n");
        } else {
            if ( n * 95 >= 300) {
                printf("%.2f\n", n * 95 * 0.85);
            } else {
                printf("%d\n", n * 95);
            }
        }

    }

}

1-6三角形 输入三角形三边长度,判断是否能构成直角三角形的3个边长

#include <iostream>
using namespace std;

int main(void)
{
    int a, b, c;
    cin >> a >> b >> c;
    if ( ( a + b <= c) || ( a + c <= b )|| ( b + c <=a ) ){
         cout << "no" << endl;
    } else {
        cout << "yes" << endl;
    } 
}

1-7年份 输入年份,判断是否闰年

#include <iostream>
using namespace std;

int main(void)
{
    int year;
    cin >> year;
    if ((y % 4 == 0 && y % 100 != 0) ||y % 400 ==0)
        cout << "yes" << endl;
    else 
        cout << "no"  << endl;
}

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注