---加總
public class t0529 {
public static void main(String[] args) {
int a[] = { 10, 20, 30, 40, 50 };
int b[] = { 1, 2, 3, 4, 5, 6, 7 };
int c[] = new int[1000];
// c[]={11,22,33,44,55,6,7};
int sum = 0;
if (b.length >= a.length) {
for (int i = 0; i < a.length; i++) {
c[i] = a[i] + b[i];
System.out.println(c[i]);}
for (int j = a.length; j < b.length; j++) {
c[j] =b[j] ;}
for (int j = 0; j < b.length; j++) {
System.out.print(c[j] +"," );}
//for (int j = a.length; j < b.length; j++) {System.out.println("加總等於 ");}
}
else {
}
}
}
--Console
11
22
33
44
55
11,22,33,44,55,6,7,
------------------------------
public class t0529 {
public static void main(String[] args) {
int a[] = { 10, 20, 30, 40, 50,60,73, };
int b[] = { 1, 2, 3, 4, 5, 6, 7 };
int c[] = new int[1000];
// c[]={11,22,33,44,55,6,7};
int sum = 0;
if (b.length >= a.length) {
for (int i = 0; i < a.length; i++) {// 先加總到a陣列長度a-1
c[i] = a[i] + b[i];
System.out.println(c[i]);}
for (int j = a.length; j < b.length; j++) {// a陣列後要做加總
c[j] = b[j];}
for (int j = 0; j < b.length; j++) {
System.out.print(c[j] + ",");}
// for (int j = a.length; j < b.length; j++)
// {System.out.println("加總等於 ");}
}
else { //b.length<=a.length
for (int i = 0; i < b.length; i++) {// 先加總到a陣列長度a-1
c[i] = a[i] + b[i];
System.out.print(c[i]);}
for (int j = a.length; j < a.length; j++) {// a陣列後要做加總
c[j] = a[j];}
for (int j = 0; j < a.length; j++) {
System.out.print(c[j] + ",");}
}
}
}
===題目
題目:身份證的驗證
說明:輸入一個身份證字號,判斷該身份證號碼是否合法。身份證號碼最後一個數字是檢查碼,檢查碼的算法如下:
(1)英文代號以下表轉換成數字
A=10 台北市 |
J=18 新竹縣 |
S=26 高雄縣 |
B=11 台中市 |
K=19 苗栗縣 |
T=27 屏東縣 |
C=12 基隆市 |
L=20 台中縣 |
U=28 花蓮縣 |
D=13 台南市 |
M=21 南投縣 |
V=29 台東縣 |
E=14 高雄市 |
N=22 彰化縣 |
W=32 金門縣 |
F=15 台北縣 |
O=35 新竹市 |
X=30 澎湖縣 |
G=16 宜蘭縣 |
P=23 雲林縣 |
Y=31 陽明山 |
H=17 桃園縣 |
Q=24 嘉義縣 |
Z=33 連江縣 |
I=34 嘉義市 |
R=25 台南縣 |
|
(2)英文轉成的數字,個位數乘9再加上十位數
(3)各數字從右到左依次乘1、2、3、4....8
(4)求出(2),(3)之和
(5)求出(4)除10後之餘數,用10減該餘數,結果就是檢查碼,若餘數為3,則檢查碼就是3
註:程式中必須有須有判斷範圍的程式,若是超出題目所訂定的數值範圍則要求重新輸入。當輸入一字元“0”時即結束此程式。
例
當輸入身分證號碼是 W100232736
輸出:正確
當輸入身份證號碼是 D021839328
輸出:錯誤
---
public class test0529 {
public static void main(String[] args) {
String str="A123456789";
int num1=0;
if(str.charAt(0)=='A')num1=10;
if(str.charAt(0)=='R')num1=25;
int num2=(num1%10)*9 +num1/10;
int num3 =(str.charAt(8)-48)*1+
(str.charAt(7)-48)*2+
(str.charAt(6)-48)*3+
(str.charAt(5)-48)*4+
(str.charAt(4)-48)*5+
(str.charAt(3)-48)*6+
(str.charAt(2)-48)*7+
(str.charAt(1)-48)*8;
int num4=num2+num3;
int num5=10-(num4%10); //檢查碼
if(num5==str.charAt(9)-48)
System.out.println("正確");
else
System.out.println("錯誤");
}
}
---測試 (輸入己方身分證)
public class test0529 {
public static void main(String[] args) {
String str="D222XXXXXX";
int num1=0;
if(str.charAt(0)=='A')num1=10;
if(str.charAt(0)=='R')num1=25;
if(str.charAt(0)=='D')num1=13;
int num2=(num1%10)*9 +num1/10;
int num3 =(str.charAt(8)-48)*1+
(str.charAt(7)-48)*2+
(str.charAt(6)-48)*3+
(str.charAt(5)-48)*4+
(str.charAt(4)-48)*5+
(str.charAt(3)-48)*6+
(str.charAt(2)-48)*7+
(str.charAt(1)-48)*8;
int num4=num2+num3;
int num5=10-(num4%10); //檢查碼
if(num5==str.charAt(9)-48)
System.out.println("正確");
else
System.out.println("錯誤");
}
}
---鍵盤輸入
import java.io.*;
public class test0529 {
public static void main(String[] args)throws Exception {
String str="A123456789";
BufferedReader buf= new BufferedReader(new InputStreamReader(System.in));
str=buf.readLine();
int num1=0;
if(str.charAt(0)=='A')num1=10;
if(str.charAt(0)=='R')num1=25;
if(str.charAt(0)=='D')num1=13;
int num2=(num1%10)*9 +num1/10;
int num3 =(str.charAt(8)-48)*1+
(str.charAt(7)-48)*2+
(str.charAt(6)-48)*3+
(str.charAt(5)-48)*4+
(str.charAt(4)-48)*5+
(str.charAt(3)-48)*6+
(str.charAt(2)-48)*7+
(str.charAt(1)-48)*8;
int num4=num2+num3;
int num5=10-(num4%10); //檢查碼
if(num5==10)num5=0;
if(num5==str.charAt(9)-48)
System.out.println("正確");
else
System.out.println("錯誤");
}
}
---魔術方塊
留言列表