目录

1000—A + B Problem
1001—Sum Problem
1002—A + B Problem II
1004—Let the Balloon Rise
1005—Number Sequence
1008—Elevator
1012—u Calculate e
1013—Digital Roots
1032—The 3n + 1 problem

代码部分

1000—A + B Problem

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext()){
        int a=cin.nextInt();
        int b=cin.nextInt();
        System.out.println(a+b);
        }
    }
}

1001—Sum Problem


这么简单题错在换行上面好多次。。。

import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext()){
            long a=cin.nextInt();
            long sum=0;
            for(int i=1;i<=a;i++){
                sum+=i;
            }
            System.out.println(sum+"\n\n");
        }
    }
}

1002—A + B Problem II


讲真大数问题还是我Java方便

import java.math.BigInteger;
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext()){
            int n=cin.nextInt();
            for(int i=1;i<=n;i++){
                BigInteger a=cin.nextBigInteger();
                BigInteger b=cin.nextBigInteger();
                System.out.println("Case "+i+":");
                System.out.println(a+" + "+b+" = "+a.add(b));
                if(i!=n) System.out.println();
            }
        }
    }
}

1004—Let the Balloon Rise

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner cin=new Scanner    (System.in);
        while(cin.hasNext()){
            Map<String,Integer> map=new HashMap<String,Integer>();
            String key="";
            int n=cin.nextInt();
            if(n==0) break;
            for(int i=0;i<n;i++){
                key=cin.next();
                if(map.containsKey(key)==false){
                    map.put(key,new Integer(1));
                }else{
                    map.put(key, new Integer(map.get(key).intValue()+1));
                }
            }
            int max=0;
            String maxColor = null;
            for(String tmp:map.keySet()){
                if(map.get(tmp).intValue()>max){
                    max=map.get(tmp).intValue();
                    maxColor=tmp;
                }
            }
            System.out.println(maxColor);
        }
    }
}

1005—Number Sequence

import java.io.*;
import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int n,a,b;
        while(cin.hasNext()){
            a=cin.nextInt();
            b=cin.nextInt();
            n=cin.nextInt();
            if(n==0&&a==0&&b==0) break;
            System.out.println(f(a,b,n%49));
        }
    }
    public static int f(int a,int b,int n){
        if(n==1||n==2){
            return 1;
        }
        return (a*f(a,b,n-1)+b*f(a,b,n-2))%7;
    }
}

1008—Elevator

import java.io.*;
import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int n,a[];
        while(cin.hasNext()){
            n=cin.nextInt();
            if(n==0) break;
            a=new int[n];
            int begin=0,sum=0;
            for(int i=0;i<n;i++){
                a[i]=cin.nextInt();
                if(a[i]>begin){
                    for(int j=begin+1;j<=a[i];j++){
                        sum+=6;
                    }
                    sum+=5;
                    begin=a[i];
                }else if(a[i]==begin){
                    sum+=5;
                }else{
                    for(int j=begin-1;j>=a[i];j--){
                        sum+=4;
                    }
                    sum+=5;
                    begin=a[i];
                }
            }
            System.out.println(sum);
        }
    }
}

1012—u Calculate e

public class Main
{
    public static void main(String args[])
    {
        double e[]=new double[10];
        e[0]=1;
        e[1]=2;
        e[2]=2.5;
        System.out.println("n e");
        System.out.println("- -----------");
        System.out.println("0 1");
        System.out.println("1 2");
        System.out.println("2 2.5");
        for(int i=3;i<=9;i++){
            e[i]=(1.0/jc(i))+e[i-1];
            System.out.println(i+" "+String.format("%.9f", e[i]));
        }
    }
    public static int jc(int n){
        if(n==1){
            return 1;
        }
        if(n==2){
            return 2;
        }
        return jc(n-1)*n;
    }
}

1013—Digital Roots

import java.util.*;
public class Main{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        String n;
        while(cin.hasNext()){
            n=cin.next();
            if(n.length()==1&&n.charAt(0)=='0') break;
            int sum=0;
            for(int i=0;i<n.length();i++){
                sum+=n.charAt(i)-'0';
            }
            while(sum>=10){
                n=Integer.toString(sum);
                sum=0;
                for(int i=0;i<n.length();i++){
                    sum+=n.charAt(i)-'0';
                }
            }
            System.out.println(sum);
        }
    }
}

1032—The 3n + 1 problem


注意:这道题比较坑
1.前一个数不一定小于后一个数字
2.可以将两个数字互换,但是要注意输出的时候要保持输入的两个数字的顺序

import java.util.Scanner;

public class Main {
    public static int zc(int n){
        int s=1;
        while(n!=1){
            if(n%2==0){
                n/=2;
                s++;
            }else{
                n=n*3+1;
                s++;
            }
        }
        return s;
    }
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        while(cin.hasNext()){
            int a=cin.nextInt();
            int b=cin.nextInt();
            int maxZc=0,maxLength=0;
            int c=a,d=b;
            if(c>d) {int t=c;c=d;d=t;}
            for(int i=c;i<=d;i++){
                if(zc(i)>maxZc) {maxZc=zc(i);maxLength=i;}
            }
            System.out.println(a+" "+b+" "+maxZc);
        }
    }
}