2 条题解

  • 0
    @ 2025-10-23 23:49:01
    
    import java.io.*;
    import java.util.*;
    
    public class Main {
        static PrintWriter sout = new PrintWriter(System.out);
        static FastReader sc = new FastReader();
    
        public static void main(String[] args) {
           int n = sc.nextInt();
           int cnt = 0;
           for (; n>0; n-=lowbit(n)) 
               cnt++;
    
           sout.print(cnt);
           sout.close();
        }
        
        public static int lowbit(int x) {
            return x&-x;
        }
        
    }
    
    class FastReader{
        StringTokenizer st;
        BufferedReader br;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        
        public boolean hasNextInt() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    String line = br.readLine();
                    if (line == null) {  
                        return false;
                    }
                    st = new StringTokenizer(line);
                } catch (IOException e) {
                    return false;
                }
            }
            return true; 
        }
        
        String next(){
            while (st == null || !st.hasMoreElements()){
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        
        long nextLong() {
            return Long.parseLong(next());
        }
        
        double nextDouble() {
            return Double.parseDouble(next());
        }
        
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    
    
    • 0
      @ 2025-5-8 22:07:25

      JAVA ac 代码:

      import java.util.*;
      import java.util.StringTokenizer;
      import java.io.*;
      
      public class Main {
      
      	public static void main(String[] args) {
      		int n=in.nextInt();
      		int ans=0;
      		while(n>0){
      			ans+=n&1;//看n的最后一位是不是1
      			n>>=1;//右移一位,把最后一位去掉
      		}
      		out.println(ans);
      		out.close();
      	}
      	
      	
      
      	static FastReader in=new FastReader();
      	static PrintWriter out=new PrintWriter(System.out);
      	static class FastReader{
      		static BufferedReader br;
      		static StringTokenizer st;
      		FastReader(){
      			br=new BufferedReader(new InputStreamReader(System.in));
      		}
      		String next() {
      			String str="";
      			while(st==null||!st.hasMoreElements()) {
      				try {
      					str=br.readLine();
      				}catch(IOException e) {
      					throw new RuntimeException(e);
      				}
      				st=new StringTokenizer(str);
      			}
      			return st.nextToken();
      		}
      		int nextInt() {
      			return Integer.parseInt(next());
      		}
      		double nextDouble() {
      			return Double.parseDouble(next());
      		}
      		long nextLong() {
      			return Long.parseLong(next());
      		}
      	}
      	
      }
      
      
      • 1

      信息

      ID
      50
      时间
      1000ms
      内存
      256MiB
      难度
      1
      标签
      递交数
      59
      已通过
      38
      上传者