1.跳步,第一步向右跳a格,第二步向左跳b格,第三步向右跳a格,第四步向左跳b格,以此往复
输入:
第一行为组数
下面就是a,b,k;//k为跳的次数
如
2
2 3 5
100000000 1 3
3 5 2
输出:
0
199999999
-2
import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here Scanner in=new Scanner(System.in); int n=in.nextInt(); for (int i=0;i<n;i++){ long a=in.nextInt(); long b=in.nextInt(); long cnt=in.nextInt(); if (cnt%2==0){ long res=(a-b)*(cnt/2); System.out.println(res); } else{ long sum=a*(cnt/2+1)-b*(cnt/2); System.out.println(sum); } } } }2.输入一个数二进制数S,(0<=s<=2^100);将他转换成十进制,然后算出错过地铁的次数,比如1,4,16,4^k的时间都会来地铁
输入:100000000(256)
输出:4
错过了1,4,16,64这四趟地铁
错过了1,4,16,64这四趟地铁
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here Scanner in=new Scanner(System.in); String s=in.next(); char[] a=s.toCharArray(); BigInteger sum=new BigInteger("0"); for (int i=0;i<a.length;i++){ if (a[i]=='1'){ sum=sum.add(helper(a.length-i-1)); } } BigInteger cnt=new BigInteger("1"); int k=0; while(cnt.compareTo(sum)==-1){ k++; cnt=help(k); } System.out.println(k); } public static BigInteger helper(int n){ if (n==0){ return new BigInteger("1"); } BigInteger a=new BigInteger("2"); return a.multiply(helper(n-1)); } public static BigInteger help(int n){ if (n==0){ return new BigInteger("1") ; } BigInteger a=new BigInteger("4"); return a.multiply(help(n-1)); } }
全部评论
(1) 回帖