Chapter II section VI example of sequence structure

Exercise evaluation address: http://ybt.ssoier.cn:8088

1. Calculate the remainder of the division of floating-point numbers

[Title Description]

Calculate the remainder of the division of two double precision floating-point numbers a and b, a and b are both positive. Here, the definition of remainder (r) is: a = k * b + r, where k is an integer, 0 < = r < b.

[input]

Input only one line, including two double precision floating-point numbers a and b.

[output]

The output is only one line, the remainder of a ÷ b.

[input example]

73.263 0.9973

[sample output]

0.4601

[reference code]

#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    double a,b;
    cin>>a>>b;
    printf("%g",a-b*int(a/b));
    return 0;
}

2. Calculate the volume of the ball

Reference code

#include<iostream> 
#include<cstdio> 
using namespace std;
int main(){
    const double PI = 3.14;
    double r;
    cin>>r;
    printf("%.2lf\n",4/3.0*PI*r*r*r);   
    return 0;
}

3. Reverse output a three digit number

Reference code

#include <iostream>
using namespace std;
int main(){
    int n;
    cin>>n; 
    int a,b,c;
    a=n/100;
    b=n%100/10;
    c=n%100%10;
    cout<<c<<b<<a<<endl;
    return 0;
}

4 elephant drinking water

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    const double PI=3.1415926;
    int h,r;    // h depth r: radius 
    int l = 20000;
    int s;  //Output value
    cin>>h>>r;
    double v = PI*r*r*h;
    s=ceil(l/v);
    cout<<s<<endl;
    return 0;
}

5 calculate line length

Reference code

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    double xa,ya,xb,yb;
    cin>>xa>>ya>>xb>>yb;    
    printf("%.3lf",sqrt((xa-xb)*(xa-xb)+(ya-yb)*(ya-yb)));  
    return 0;
}

6 calculation of triangle area

Reference code

#include <iostream>
#include <cmath> 
using namespace std;
int main(){
    double x1,y1,x2,y2,x3,y3,s,p,l1,l2,l3;
    cin>>x1>>y1>>x2>>y2>>x3>>y3;
    l1=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    l2=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
    l3=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
    p=(l1+l2+l3)/2.0;
    printf("%.2lf",sqrt(p*(p-l1)*(p-l2)*(p-l3)));
    return 0;
}

7 A*B problem

Reference code

#include <iostream>
using namespace std;
int main(){
    long long a,b,c;
    cin>>a>>b;
    c=a*b;
    cout<<c<<endl;
    return 0;
}

8 calculate the power of 2

Reference code

#include <iostream>
using namespace std;
int main(){
    long n;
    cin>>n;
    cout<<(1<<n)<<endl;
    return 0;
}

9 apples and worms

Reference code

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    int n;
    float x,y;
    cin>>n>>x>>y;
    cout<<n-ceil(y/x)<<endl;
    return 0;
}

Tags: C++

Posted on Sat, 30 Nov 2019 01:34:44 -0800 by HyperD