# 609 閏年
```
請撰寫一程式,讓使用者輸入三個正整數,分別為西元年、月、日,請計算此日期為當年的第幾天,需注意閏年;若輸入的日期有誤,請輸出「error」。
閏年:
西元年份除以4不可整除,為平年。
西元年份除以4可整除,且除以100不可整除,為閏年。
西元年份除以100可整除,且除以400不可整除,為平年。
西元年份除以400可整除,為閏年。
輸入說明 三個正整數,分別為西元年、月、日
輸出說明 此日期為當年的第幾天
範例輸入1 2012 3 7
範例輸出1 67
範例輸入2 2018 6 31
範例輸出2 error
#include <stdio.h>
#define N 3
int leap(int year){
return year%4==0 && (year%100 != 0 || year%400==0);
}
void date(int* x){
int t=x[2];
if (x[1]>2)
t+=leap(x[0]);
int y[]={31,28,31,30,31,30,31,31,30,31,30,31};
for (int i=0; i<x[1]-1; ++i)
t+=y[i];
printf("%d", t);
}
void msg(){ printf("%s", "error"); }
int main(){
int x[N];
for (int i=0; i<N; ++i)
scanf("%d", &x[i]);
if (x[1]>12||x[1]*x[2]==0)
msg();
else
if ((x[1]+ (x[1]>=8) ) % 2)
(x[2]>31) ? msg() : date(x);
else
if (x[1]==2)
(x[2]>28+leap(x[0])) ? msg() : date(x);
else
(x[2]>30) ? msg() : date(x);
}
//暫存(備用)
int find(int* x, int a){
for (int i=0; x[i] != '\0'; ++i)
if (x[i]==a) return 1;
return 0;
}
int find(int* x, int a, int* i){
for (; x[*i] != '\0'; ++(*i))
if (x[*i]==a) return 1;
return 0;
}
```
# java
```
// (1) 請撰寫程式,讓使用者輸入兩個8位數的整數作為日期,計算二者相差的天數並輸出其絕對值,若輸入文字或無法轉換,請輸出【error】。
import java.util.Scanner; //402 計算時間差
public class JPD03 {
static void msg(){System.out.print("error");}
static int leap(int n){
return (n%4==0 && (n%100!=0 || n%400==0)) ? 1 : 0;}
static int date(int[] n,int a){
int sum=n[2];
if (n[1]>2)
sum+=a;
int[] x = {31,28,31,30,31,30, 31,31,30,31,30,31};
for (int i=0; i<n[1]-1; i++)
sum += x[i];
return sum;
}
static int check(int[] x,int a){
if (x[1]>12||x[1]*x[2]==0)
return 0;
else {
int b=0;
if (x[1]>7) b=1;
if ((x[1]+b) % 2 == 1)
if (x[2]>31)
return 0;
else
return date(x,a);
else
if (x[1]==2)
if (x[2]>28+a)
return 0;
else
return date(x,a);
else
if (x[2]>30)
return 0;
else
return date(x,a);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
int a = sc.nextInt(); // 4-35
int b = sc.nextInt();
int[] y1 = {a/10000, (a%10000)/100, a%100};
int[] y2 = {b/10000, (b%10000)/100, b%100};
int L1 = leap(y1[0]), L2 = leap(y2[0]);
int d1=check(y1,L1), d2=check(y2,L2), t=0;
if (d1*d2==0) {
msg();
return;
}
// System.out.printf("%d %d\n",d1,d2);
if (y1[0]==y2[0]){
System.out.print(Math.abs(d1-d2));
} else {
int min = Math.min(y1[0], y2[0]);
int max = Math.max(y1[0], y2[0]);
for (int i=0; i<max-min; i++)
t += 365 + leap(min+i);
System.out.print(Math.abs(d1-d2)+t);
}
} catch (Exception ex) {
msg();
}
}
}
```
# 701 海龍公式
```
請撰寫一程式,讓使用者輸入三個正整數,做為三角形邊長,再利用海龍公式計算並出輸出三角形面積至小數點後第二位。
提示:海龍公式
提示:
輸入說明 三個正整數,為三角形邊長
輸出說明 三角形面積
範例輸入 3 4 5
範例輸出 6.00
#include <stdio.h>
#include <math.h>
int main(){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
float s = (a+b+c)/2.0;
printf("%.2f", sqrt(s*(s-a)*(s-b)*(s-c)));
}
```