习题1-1 平均数(average)
输入三个整数,输出他们的平均值,保留三位小数
Java:
package suanfa; import java.util.Scanner; public class T1_1 {
public static void main(String[] args) {
float a,b,c;
float aver = 0;
Scanner reader = new Scanner(System.in);
a = reader.nextFloat();
b = reader.nextFloat();
c = reader.nextFloat();
aver = (a+b+c)/3;
String.format("%.3f", aver);
System.out.println("The answer is " + aver);
}
}
习题1-2温度(temperature)
输入华氏温度f,输出对应的摄氏温度c,保留3位小数
Java:
package suanfa; import java.util.Scanner; public class T1_1 {
public static void main(String[] args) {
float f,c;
Scanner reader = new Scanner(System.in);
f = reader.nextFloat();
System.out.println("The answer is " + String.format("%.3f ℃", (5*(f-32))/9));
}
}
习题1-3联系和(sum)
输入正整数n,输出1+2+......+n的值
Java:
package suanfa; import java.util.Scanner; public class T1_1 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
System.out.println("The answer is " + ((n+1)*n)/2);
}
}
习题1-4正弦和余弦(sin和cos)
输入正整数n(n<360),输出n度的正弦、余弦函数值。
Java:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<math.h>
#include <stdlib.h>
using namespace std; const double Pi=(180/3.14159265358979);
void main()
{
double x;
do{
cout<<"请输入角度:"<<endl; //角度制 °
cin>>x;
system("cls"); //清屏
cout<<"sin "<<x<<"°="<<sin(x/Pi)<<endl;
cout<<"cos "<<x<<"°="<<cos(x/Pi)<<endl;
cout<<"tan "<<x<<"°="<<tan(x/Pi)<<endl;
}while(1);
}
习题1-5打折(discount)
一件衣服95元,若消费满300元可打85折。输入购买衣服件数,输出需要支付的金额(单位:元),保留两位小数
C++:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main(){
double x,y;
int a;
while(scanf("%d", &a)!=EOF){
//买衣服超过3件就打85折
if(a > 3){
y = a*95*0.85;
}else{
y = 95*a;
}
printf("%.3lf\n", y);
}
return 0;
}