2017年4月11日 星期二

[ITSA Basic] 題目34. 標準體重計算

[ITSA Basic] 題目34. 標準體重計算

問題描述:
已知男生標準體重=(身高-80 )*0.7;女生標準體重=(身高-70)*0.6;試寫一個程式可以計算男生女生的標準體重。
輸入說明:
每個測資檔案會有多組測資案例。輸入兩個數值,依序代表為身高及性別(1代表男性;2代表女性)。
輸出說明:
輸出標準體重,浮點數取至第一位。

這題題目本身不難,上面那行紅字讓我錯誤不知道多少次。
意思是說,一個測資有多組資料,不能讓程式只輸入一筆測資就結束了
所以我寫了一個迴圈讓他可以一次測100筆的測資
然後就過了


import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.InputMismatchException;
 
public class BMI
{
    public static void main (String[] args) throws java.lang.Exception
    {
    int high, gender;  
        Scanner sc = new Scanner(System.in);
        for(int i = 0;i <100;i++) //就是這行
        {
        high = sc.nextInt();  
        gender = sc.nextInt();  
           
        if(gender == 1)  
        {  
            System.out.printf("%.1f", (long)(high-80 )*0.7);  
            System.out.println();
        }  
        if(gender == 2)  
        {  
            System.out.printf("%.1f", (long)(high-70)*0.6);  
            System.out.println();
        }
        }
    }
}

2017年3月20日 星期一

[瘋狂程設]M90H028:平面幾何:中點公式

import java.util.*;

public class main{
    public static void main(String[] args) {
       
        int x1, y1, x2, y2;
        double mid_x, mid_y;
       
        Scanner sc = new Scanner(System.in);
        x1 = sc.nextInt();
        y1 = sc.nextInt();
        x2 = sc.nextInt();
        y2 = sc.nextInt();
       
        mid_x = (x1 + x2)/2.0;
        mid_y = (y1 + y2)/2.0;
       
        if(Math.abs(mid_x) > Math.abs((int)mid_x))
        {
        if(Math.abs(mid_y) > Math.abs((int)mid_y))
       {
        //x小數  y小數
        System.out.printf("(" + mid_x + "," + mid_y + ")");
       }
       else
       {
        //x小數  y整數
        System.out.printf("(" + mid_x + "," + (int)mid_y + ")");
       }
        }
        else
        {
        if(Math.abs(mid_y) > Math.abs((int)mid_y))
       {
        //x整數  y小數
        System.out.printf("(" + (int)mid_x + "," + mid_y + ")");
       }
       else
       {
        //x整數  y整數
        System.out.printf("(" + (int)mid_x + "," +(int) mid_y + ")");
       }
        }
    }
};

[瘋狂程設]M90H011:整數商餘

import java.util.*;

public class main{
public static void main(String arge[]){
int m, n;
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
n = sc.nextInt();

System.out.println();

System.out.println(m + " / " + n + "=" + (m/n));
System.out.print(m + " mod " + n + "=" + (m%n));
}
}

2017年3月15日 星期三

[瘋狂程設]M90H007:考試調分(低成60高100)

import java.util.*;
import java.util.Arrays;

public class main{
public static void main(String arge[]){
int newGrade;
int[] a;
int min = 101;
int max = -1;
a = new int[60]; //存60個同學的成績
Scanner sc = new Scanner(System.in);

//輸入60個同學的成績
   for(int i = 0;i<a.length;i++)
{
a[i] = sc.nextInt();
}

//找出最大最小值,為了求方程式
for(int i = 0;i<60;i++)
{
if(a[i] < min)
{
min = a[i];
}
if(a[i] > max)
{
max = a[i];
}
}

//newGrade = A * oldGrade + B
float A = 40 / (float)(max - min);
float B = (60 - (A * (float)min));

for(int i = 0;i<a.length;i++)
{
//以線性方式提高分數,四捨五入
a[i] = Math.round(A * a[i] + B);
//如果分數高於100,以100紀錄
if(a[i]>100)
{
a[i] =100;
}
}
//輸出同學們的分數
for(int i = 0;i<a.length;i++)
{
System.out.println(a[i]);
}
}
}

[瘋狂程設]G001:長寬高算體積

import java.util.*;

public class main{
public static void main(String arge[]){
int x, y, z;
int m;
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();
m = x*y*z;

System.out.print(m);
}
}

[瘋狂程設]F020:計算BMI

import java.util.*;
import java.lang.*;

public class main{
public static void main(String arge[]){
double h, w;
Scanner sc = new Scanner(System.in);
h = sc.nextFloat();
w = sc.nextFloat();

double BMI;
BMI = w/(Math.pow(h, 2));

                //因為如果BMI算出來是10.0000時,他要求輸出10,所以多了一個步驟判斷
                //如果BMI>BMI的整數部分,代表他後面的小數點是有數字的
if(BMI > (int)Math.floor(BMI))
{
System.out.printf("%.4f", BMI);
System.out.println();
}
else
{
System.out.println((int)Math.floor(BMI));
                        //(int)Math.floor(BMI)可以讓浮點數變整數
}

if(BMI < 18.5)
{
System.out.print("too thin");
}
if(BMI >= 18.5 && BMI < 24)
{
System.out.print("standard");
}
if(BMI >= 24)
{
System.out.print("too fat");
}
}
}

[瘋狂程設]F019:長方形面積

import java.util.*;

public class main{
public static void main(String arge[]){
int l, w;

Scanner sc = new Scanner(System.in);
l = sc.nextInt();
w = sc.nextInt();

System.out.print(l*w);
}
}