UVA 11332 Java

  1. import java.util.*;
  2.  
  3. public class main{
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. while(sc.hasNextLine())
  7. {
  8. String str = sc.nextLine();
  9. if(str.equals("0"))
  10. break;
  11. while(str.length() > 1)
  12. {
  13. int num = 0;
  14. for(int i = 0; i < str.length(); i++)
  15. num += str.charAt(i) - '0';
  16. str = Integer.toString(num);
  17. }
  18. System.out.println(str);
  19. }
  20. }
  21. };

  1. import java.util.*;
  2.  
  3. public class main{
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. while(sc.hasNextLine())
  7. {
  8. int num = Integer.parseInt(sc.nextLine());
  9. if(num == 0)
  10. break;
  11. while(num >= 10)
  12. {
  13. int temp = 0;
  14. while(num != 0)
  15. {
  16. temp += num % 10;
  17. num /= 10;
  18. }
  19. num = temp;
  20. }
  21. System.out.println(num);
  22. }
  23. }
  24. };

留言

這個網誌中的熱門文章

C# 井字遊戲