close
題目概要:
判斷數字是否為11的倍數。
解題方向:
1. 數字最大會有1000位數,故不是合用數字處理。
2.使用String儲存數字,並且使用11倍數的判別法( 奇數-偶數=11倍數(包括0),即是11倍數)。
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java | |
import java.util.Scanner; | |
class uva10929{ | |
public static void main(String args[]){ | |
Scanner sc=new Scanner(System.in); | |
while(sc.hasNext()){ | |
String st=sc.next(); | |
if(st.equals("0"))break; | |
int odd=0,even=0; | |
for(int i=0;i<st.length();i++){ | |
if(i%2==0) even=even+st.charAt(i)-48; | |
else odd=odd+st.charAt(i)-48; | |
} | |
if((odd-even)%11==0) | |
System.out.println(st+" is a multiple of 11."); | |
else | |
System.out.println(st+" is not a multiple of 11."); | |
} | |
} | |
} |
文章標籤
全站熱搜