首页 > 试题广场 >

数字的情绪

[编程题]数字的情绪
  • 热度指数:2247 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解

每个整数都希望可以整除很多数字,特别是它自身包含的数字,我们将整数分为三类:

1. 数字可以整除它包含的一部分数字,比如72,由,72两种数字组成,72可以整除2,我们称这个数字是开心的,用”H”表示。

2. 数字不能整除它包含的任何数字,比如73,由,73两种数字组成,73不能整除任何数,我们称这个数字是沮丧的,用”S”表示。

3. 数字可以整除它包含的所有数字,比如12,既可以整除1又可以整除2,我们称它是一个非常棒的数,用“G”表示。  (0可以被任何数整除。)


输入描述:
输入第一行包含一个整数T,表示数据组数(1<=T<=100). 接下来T行,每行包含一个正整数n(1<=n<=10^12),表示需要你判断的数字。


输出描述:
对于每组测试数据输出一行,“H”,“S”或“G”。表示整数种类。
示例1

输入

3
72
73
12

输出

H
S
G
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            long n  = Integer.valueOf(sc.nextLine());
            for(int i =0;i<n;i++){
                String str = sc.nextLine();
                long num = Long.parseLong(str);
                int total = 0;
                int length = str.length();
                for(int j =0;j<length;j++){
                    int value = str.charAt(j)-48;
                    if(value==0){
                       total++;
                    }else if(num%value==0)total++;
                }
                outPut(total,length);
            }
        }
    }
    public static void outPut(int total,int length){
        if(total==length){
            System.out.println("G");
        }else if(total>0){
            System.out.println("H");
        }else{
            System.out.println("S");
        }
    }
}

发表于 2020-06-04 11:18:58 回复(0)
/*
思路:利用标志的方式判断,创建两个标志,当能够整除任一个部分数字,该标志为true,
当不能整除任一个部分数字,该标志为true
这样通过两个标志的组合便可以得到结果
注意除数为0的情况
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; 
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int m = Integer.parseInt(br.readLine());
        long[] arr = new long[m];
        for(int i = 0;i<m;i++)
            arr[i] = Long.parseLong(br.readLine());
        for(int i = 0;i<m;i++){
            boolean onenum = false;
            boolean nonum = false;
            long temp = arr[i];
            while(temp > 0){
                long divi = temp%10;
                if(divi == 0 || arr[i]%divi == 0){
                    onenum = true;
                }else{
                    nonum = true;
                }
                temp = temp/10;
            }
            if(onenum == true && nonum == true){
                System.out.println("H");
            }else if(onenum == false && nonum == true){
                System.out.println("S");
            }else if(onenum == true && nonum ==false)
                System.out.println("G");
        }
    }
}

发表于 2020-05-22 11:42:59 回复(0)