首页 > 试题广场 >

小乐乐改数字

[编程题]小乐乐改数字
  • 热度指数:56117 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

小乐乐喜欢数字,尤其喜欢01。他现在得到了一个数,想把每位的数变成01。如果某一位是奇数,就把它变成1,如果是偶数,那么就把它变成0。请你回答他最后得到的数是多少。


输入描述:

输入包含一个整数n (0 ≤ n ≤ 109)



输出描述:
输出一个整数,即小乐乐修改后得到的数字。
示例1

输入

222222

输出

0
示例2

输入

123

输出

101
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
            String a = in.next();
            StringBuffer sb =new StringBuffer();
            for(int i=0;i<a.length();i++){
                String tmp;
                if ((int)a.charAt(i)%2==0) {
                    tmp = "0";
                } else {
                    tmp="1";
                }
                sb.append(tmp);
            }
            System.out.print(Integer.parseInt(sb.toString()));
    }
}

发表于 2024-02-28 19:24:55 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int count = 0;
        while (n != 0) {
            int a = n % 10;
            if (a % 2 == 0) {
                list.add(0);
            } else {
                list.add(1);
            }
            n /= 10;
        }

        for (int i = list.size() - 1; i >= 0; i--) {
            count = 10 * count + list.get(i);
        }

        System.out.println(count);
    }
}
发表于 2023-10-24 09:13:12 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 利用正则表达式来替换
        // while (in.hasNextLong()) { // 注意 while 处理多个 case
        //     String str = in.nextLine().trim();
        //     String req1 = "[2468]";
        //     String req2 = "[3579]";
        //     String temp1 = str.replaceAll(req1, "0");
        //     String temp2 = temp1.replaceAll(req2, "1");
        //     System.out.println(Integer.parseInt(temp2));
        // }
        while(in.hasNextLine()){
            String str = in.nextLine().trim();
            String temp = "";
            for (char c: str.toCharArray()
                 ) {
                if (c % 2 == 1){
                    temp = temp + "1";
                }else {
                    temp = temp + "0";
                }
            }
            while(temp.startsWith("0") && temp.length() > 1){
                temp = temp.replaceFirst("0", "");
            }
            System.out.println(temp);
        }
    }
}

发表于 2022-10-30 22:14:07 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s=in.next();
//输入的数字转换为一个list
String[] sList=s.split("");
int[] iList=new int[s.length()];
for(int i=0;i<s.length();i++){
iList[i]=Integer.parseInt(sList[i]);
}
//置换数字
for(int i=0;i<s.length();i++){
if(iList[i]%2==0){
iList[i]=0;
}else{
iList[i]=1;
}
}
//list合并为一个整数
int num=0;
for(int i=0;i<s.length();i++){
num=num*10+iList[i];
}
System.out.print(num);
}
}
发表于 2022-10-26 19:06:55 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Integer num = scanner.nextInt();
        //将数字转化成字符
        String str = num.toString();
        int countNum = str.length();
        int[] toArray = new int[countNum];
        //将输入的数字放进数组中
        for(int i = 0;i < countNum;i++){
            toArray[i] = str.charAt(i);
        }
        //将数组中的数字进行0和1的变化
        for(int j = 0;j < countNum;j++){
            if(toArray[j] % 2 == 0){
                toArray[j] = 0;
            }else{
                toArray[j] = 1;
            }
        }
        //将数组调整回整数
        int k = 0;
        for(int l = 0;l < countNum;l++){
            k = k * 10 + toArray[l];
        }
        System.out.println(k);
    }
}

发表于 2022-06-24 11:45:04 回复(0)
import java.util.Scanner;
import java.util.Arrays;
public class Main{
    public static void main(String[]args){
        Scanner scanner = new Scanner(System.in);
        Integer integer = scanner.nextInt();
        String string = integer.toString();
        int L = string.length();
        int[] intArray = new int[L];
        //把输入的数字存入数组中(下)
        for(int i=0;i<L;i++){
            intArray[i] = string.charAt(i);
        }
        //把数组中的数字变成0或1
        for(int j=0;j<L;j++){
            if(intArray[j]%2==0){
                intArray[j]=0;
            }else{
                intArray[j]=1;
            }
        }
        //把数组变成整数
        int i=0;
        for(int l=0;l<L;l++){
            i = i*10+intArray[l]; 
        }
        System.out.print(i);  
    }
}              

编辑于 2022-08-21 22:34:42 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str=sc.nextLine();
        String outstr="";
        for (int i=0;i<str.length();i++)
        {
                outstr+=(((int)str.charAt(i))%2)==0?"0":"1";    
        }
        System.out.println(Integer.parseInt(outstr));
    }        
}
发表于 2022-05-20 17:28:50 回复(0)
import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner input=new Scanner(System.in);
        String s=input.nextLine();
        int result[]=new int[s.length()]; //利用整形数组接收赋值
        int output=0;
        for(int i=0;i<s.length();i++){
            if(s.charAt(i)=='0'|s.charAt(i)=='2'|s.charAt(i)=='4'|s.charAt(i)=='6'|s.charAt(i)=='8'){
                result[i]=0;//偶数赋0
            }
            else{
                result[i]=1;//奇数赋1
            }
        }
        for(int j=0;j<result.length;j++){//数组转整形
            output=output*10+result[j];
        }
        System.out.print(output);
    }
}

发表于 2022-04-29 09:59:26 回复(0)
这道题原来是不能在前面加零,所以要从个位数开始想。思路错了,捣鼓了半天
发表于 2022-03-29 16:09:25 回复(0)
import java.util.*;
public class Main{
    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int sum = 0;
        int flag = 1;
        while (n != 0) {
            if (n % 10 % 2 != 0) {
                sum += 1 * flag;
            }
            n /= 10;
            flag *= 10;
        }
        System.out.println(sum);
    }
}

发表于 2021-10-03 20:11:53 回复(1)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        String str=String.valueOf(n);
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<str.length();i++){
            int m=Integer.parseInt(String.valueOf(str.charAt(i)))%2;
            stringBuilder.append(m);
        }
        str=stringBuilder.toString();
        System.out.println(Integer.parseInt(str));
    }
}
发表于 2020-09-25 22:13:33 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String num=sc.nextLine();
        String[] arr=num.split("");
        int len=arr.length;
        StringBuffer s = new StringBuffer();
        int[] number=new int[len];
        for(int i=0;i<=len-1;i++){
            number[i]=Integer.parseInt(arr[i]);
            if(number[i]%2==0){
                number[i]=0;
            }else{
                number[i]=1;
            }
           s.append(number[i]);
          
        }
        int sum=Integer.valueOf(s.toString());
               System.out.print(sum);
        
        }
        
        }
    

发表于 2020-09-16 15:54:56 回复(0)

问题信息

上传者:牛客309119号
难度:
15条回答 4493浏览

热门推荐

通过挑战的用户

查看代码
小乐乐改数字