刷算法第三天—— P1014 Cantor 表+P1307 数字反转 题解

第三天

P1014 Cantor 表

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int r = scanner.nextInt();
        scanner.close();
        int sum = 0;
        int count = 1;
        for (; sum < r;) {
            sum += count;
            count++;
            // System.out.println("sum: " + sum);
            // System.out.println("count: " + count);
        }
        int a = 0;
        int b = 0;
        if (count % 2 == 1) {
            a = count + r - sum - 1;
            b = sum - r+1;
        } else {
            a = sum - r+1;
            b = count + r - sum - 1;
        }
        System.out.print(a+"/"+b);
    }
}

注:我的方法蠢得可以,完全可以转过45看。




P1307 数字反转 题解

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int r = scanner.nextInt();
        boolean flag = false;
        if (r < 0) {
            flag = true;
        }
        String k = r + "";// 换成String类型
        if (flag) {
            k = k.substring(1);// 只有k.substring(1),k是不变的
        } // 负数-减去
        char[] c = k.toCharArray();
        for (int i = 0; i < c.length / 2; i++) {// 倒转
            char temp = c[i];
            c[i] = c[c.length - i - 1];
            c[c.length - i - 1] = temp;// 注意还要减个1
        }
        int count = 0;
        for (int i = 0; i < c.length; i++) {
            if (c[i] != '0') {
                count = i;
                break;
            }
        }
        if (flag)//把-加上
            System.out.print('-');
        for (int i = count; i < c.length; i++) { // 输出
            System.out.print(c[i]);
        }
        scanner.close();
    }
}

注:1、char型数组转String

char[] ch=new char[]{'a','b','c'};
	    String string=new String();
	    string.valueOf(ch);

2、String类型转char型数组

		String string =new String("abc");
	    char[] ch=string.toCharArray();

3、s1.charAt(6)=‘s’;
是错的,不能出现在=左边

4、 k = k.substring(1);// 只有k.substring(1),k是不变的






太复杂了,看大佬再写一种:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int r = scanner.nextInt();
        int s = 0;
        while (r != 0)
        {
            s = s * 10 + r % 10;
            r /= 10;
        }
        System.out.print(s);
        scanner.close();
    }
}

注:
java没有,运算符,不能s=s*10+n%10,n/=10;





大佬的另一种答案

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        // 将数据存入
        StringBuffer sb=new StringBuffer(br.readLine().trim());
        sb.reverse();   // 反转
        //负号跑到最后
        if(sb.charAt(sb.length()-1)=='-'){
            // 删除负号并且在头插入负号
            sb.delete(sb.length()-1, sb.length());
            sb.insert(0, '-');

        }
        System.out.println(Integer.parseInt(sb.toString()));
    }

}```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值