首页 > 试题广场 >

A+B和C (15)

[编程题]A+B和C (15)
  • 热度指数:159987 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。

输入描述:
输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。


输出描述:
对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)。
示例1

输入

4<br/>1 2 3<br/>2 3 4<br/>2147483647 0 2147483646<br/>0 -2147483648 -2147483647

输出

Case #1: false<br/>Case #2: true<br/>Case #3: true<br/>Case #4: false
推荐
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
        int i=0;
        int num=0;
        long a,b,c;
        a=0;b=0;c=0;
      
            num=sc.nextInt();
            while(i++<num){
                a=sc.nextLong();
                b=sc.nextLong();
                c=sc.nextLong();
                if((a+b)>c){
                    System.out.println("Case #"+i+":"+" true");
                }else{
                    System.out.println("Case #"+i+":"+" false");
                }
            }    
    }

}
一把辛酸泪T_T,没做过OJ的记得java类名为Main,此题大家记得测试的数据定义为long类型,case首字母大写,要与题干要求一致,无力吐槽。。。。
编辑于 2015-06-19 17:40:10 回复(44)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/*Case #1: false
        Case #2: true
        Case #3: true
        Case #4: false*/
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        int i = 1;
        while (n--> 0) {
            String[] s = reader.readLine().split(" ");
             boolean flag = Long.parseLong(s[0]) + Long.parseLong(s[1]) > Long.parseLong(s[2]);
            System.out.println("Case #" + i++ + ": " + flag);
        }
    }
}

发表于 2021-01-26 11:45:30 回复(0)
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc =new Scanner(System.in);
		while (sc.hasNext()){
			List<Boolean> list =new ArrayList<>();
			Scanner sc2 =new Scanner(System.in);
			int i = sc.nextInt();
			int count = i;
			while (i>=1&&sc2.hasNext()){
				int A = sc2.nextInt();
				int B = sc2.nextInt();
				int C = sc2.nextInt();
				boolean b =A+B>C;
				list.add(b);
				i--;
				if(i == 0){
					for (int j=count;j>=1;j--){
						System.out.println("Case #"+(count-j+1)+": "+list.get(count-j));
					}
				}
			}

		}
	}
}
为什么总说我的输出为空?我在idea上输出没问题啊。

发表于 2020-10-23 11:34:21 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
         Scanner input=new Scanner(System.in);
        int T=input.nextInt();
        long[]A=new long[T];
        long[]B=new long[T];
        long[]C=new long[T];
        for(int i=0;i<T;i++){
             A[i]=input.nextLong();
             B[i]=input.nextLong();
             C[i]=input.nextLong();
        }
        for(int j=0;j<T;j++){
            if(A[j]+B[j]>C[j]){
                System.out.println("Case #"+(j+1)+": true");
            }else{
                System.out.println("Case #"+(j+1)+": false");
            }
        }
    }
}
发表于 2020-10-17 18:56:02 回复(1)
import java.util.*;
import java.io.*;
public class Main{
void  BB(int i,long A,long B,long C)
{
    
    if(A+B>C){
        System.out.println("Case #"+i+":ture");
    }
    else{
        System.out.println("Case #"+i+":false");
    }
    
}
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int k=in.nextInt();
        for(int i=0;i<k;i++){
        long a=in.nextLong();
        long b=in.nextLong();
        long c=in.nextLong();
        Main bb=new Main();
        bb.BB(i+1,a,b,c);
        }
    }
}
这是哪里错了啊
发表于 2020-10-10 17:37:30 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc=new Scanner (System.in);
        long T=sc.nextLong();
        for(int i=0;i<T;i++) {
            long A=sc.nextLong();
            long B=sc.nextLong();
            long C=sc.nextLong();
            add(A, B, C, i+1);
        }
         
    }
    public static void add(long A,long B ,long C ,int i) {
        if(A+B>C) {
            System.out.println("Case #"+i+": true");
        }else {
            System.out.println("Case #"+i+": false");
        }
    }
}

思路:

这里主要有一个问题是范围问题,如果用int型,那么会超出界限,所以我用的是long型
在每次输入一组数据后,都调用一下判断的方法add()
add(long A,long B ,long C ,int i)方法
定义4个形参A,B,C和序列i,在每次输入后,将其传入,并且比较是否正确


发表于 2020-01-12 20:07:28 回复(0)
import java.util.Scanner;
public class Main {
public static void main(String[] args){
		Scanner scan =new Scanner(System.in);
		byte count=scan.nextByte();
		StringBuffer sb = new StringBuffer();
		for(byte i=1;i<=count;i++) {
			long A=scan.nextLong();
			long B=scan.nextLong();
			long C=scan.nextLong();
			sb.append("Case #").append(i).append(": ");
			if(A+B>C) {
				sb.append(true);
			}else {
				sb.append(false);
			}
			sb.append("\r");
		}
		System.out.println(sb);
	}}

发表于 2019-11-11 15:01:39 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        byte T = in.nextByte();
        for (int i = 1; i <= T; i++)
            System.out.println("Case #" + i + ": " + (in.nextLong() + in.nextLong() > in.nextLong()));
    }
}

编辑于 2019-08-31 22:06:24 回复(0)
import   java.util.Scanner;
public class   Main{
public  static void main(String[]  args){
Scanner  sc =new  Scanner(System.in);
int  i = sc.nextInt();
boolean[] arr =new  boolean[i];
for(intn =0;n <i;n++){
long a  = sc.nextLong();
long b  = sc.nextLong();
long c  = sc.nextLong();
if((a+b)<=c){
arr[n] =false;
}
else
arr[n] =true;
}
for(intnum=0;num<arr.length;num++){
System.out.println("Case #"+(num+1)+": "+arr[num]);
}
}
编辑于 2019-08-02 14:03:43 回复(0)
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestSumAAndB {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int num = 0;
        List<String> list = new ArrayList<String>();
        if(scan.hasNextInt()) {
            num = scan.nextInt();
        }
        for(int i = 0;i < num+1;i++ ) {
            if(scan.hasNextLine()) {
                list.add(scan.nextLine()); // 这一行第一次会默认输入空字符串,为啥
            }
        }
        for(int i = 1;i < num+1;i++) {
            String[] strs = list.get(i).split(" ");
            long a = Long.parseLong(strs[0]);
            long b = Long.parseLong(strs[1]);
            long c = Long.parseLong(strs[2]);
            if(a + b > c) {
                System.out.println("Case #"+ i +": true");
            } else {
                System.out.println("Case #"+ i +": false");
            }
            
        }
        
        scan.close();
        
    }

}
有谁知道为什么list.add(scan.nextLine()) 的地方,会默认输入个空的字符串上去?
发表于 2019-06-06 14:59:23 回复(0)
import java.io.*;
import java.util.*;
public class Main {     public static void main(String[]args)throws Exception {         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));         int number=Integer.parseInt(br.readLine());         long A=0,B=0,C=0;         List list=new ArrayList();                  for(int i=0;i<number;i++) {             String[] strArray=br.readLine().trim().split(" ");             A=Long.parseLong(strArray[0]);             B=Long.parseLong(strArray[1]);             C=Long.parseLong(strArray[2]);             long []array=new long[3];             array[0]=A;array[1]=B;array[2]=C;             list.add(array);             }         for(int i=0;i<list.size();i++) {             long []a=(long[])list.get(i);             A=a[0];B=a[1];C=a[2];             if(A>=0) {                 if(B+A>C)System.out.println("Case #"+(i+1)+": true");                 else System.out.println("Case #"+(i+1)+": false");             }else {                 if(B>=0) {                     if(A+B>C)System.out.println("Case #"+(i+1)+": true");                     else System.out.println("Case #"+(i+1)+": false");                 }else {                     if(Math.abs(A+B)>Math.abs(C))System.out.println("Case #"+(i+1)+": false");                     else System.out.println("Case #"+(i+1)+": true");                 }             }         }     }
}

发表于 2019-04-21 15:12:04 回复(0)
import java.util.Scanner;
public class Main {
 public static void main(String[] args) {
  Scanner in=new Scanner(System.in);
  while(in.hasNext()) {
  int n=in.nextInt();
   long [][]num=new long[n][3];
   for(int i=0;i<n;i++) {
    for(int j=0;j<3;j++) {
     num[i][j]=in.nextLong();
    }
   }
   for(int i=0;i<n;i++) {
    if(num[i][0]+num[i][1]>num[i][2]) {
     System.out.println("Case #"+(i+1)+": true");
    }
    else {
     System.out.println("Case #"+(i+1)+": false");
    }
  }
 }
}
}

发表于 2019-03-06 10:38:24 回复(0)
long a=0,b=0,c=0;         System.out.println("请输入用例个数(小于10)");         Scanner scanner=new Scanner(System.in);         int num=scanner.nextInt();         long[][] arr=new long[num][3];         for(int i=0;i<num;i++){             for(int j=0;j<3;j++){                 arr[i][j]=scanner.nextLong();             }         }         for(int i=0;i<num;i++){             System.out.println("Case #"+(i+1)+":"+((arr[i][0]+arr[i][1])>arr[i][2]));                      }
好坑啊 第一次OJ 两个空格没打 来回输了好几次 吐血
发表于 2019-01-24 18:27:17 回复(0)
import java.util.Scanner;
public class Main {
 public static void main(String[] args) {
  Scanner input=new Scanner(System.in);
  int n=input.nextInt(); 
      
         
  for(int i=1;i<=n;i++) {
   long a=input.nextInt();
   long b=input.nextInt();
   long c=input.nextInt();
   if((a+b)>c)
     System.out.println("Case # "+i+" : true");
   else
     System.out.println("Case # "+i+" : false");
  }
 }
}
在自己的电脑上运行是没问题的,为什么在网站上就都不通过惹
发表于 2018-11-12 12:01:50 回复(0)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();
        for(intX=1;X<=T;X++)
         {
            long A=sc.nextLong();
            long B=sc.nextLong();
            long C=sc.nextLong();
            if((A+B)>C)
                System.out.println("Case #"+X+": true");
            else
                System.out.println("Case #"+X+": false");
          }
    }
  
}
发表于 2018-07-29 23:46:33 回复(0)
import java.util.*;
//简单题
public class Main {
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        int i =0;
        while(t-->0){
            i++;
            long a = in.nextLong();
            long b = in.nextLong();
            long c = in.nextLong();
            System.out.println("Case #"+i+": "+(a+b>c));
        }
    }
} 

发表于 2018-05-04 08:41:32 回复(0)
    提交代码出现异常:
Exception in thread "main" java.util.InputMismatchException: For input string: "2147483648"
    好吧,我以为测试用例只有上面的示例,Java中用上面的示例测试本地通过,提交出现异常。
    我本来是通过同号相减、异号相加避免溢出,如果输入数据没有溢出,计算过程中是不会溢出的。现在输入的数据就已经超出int表示范围了,用long是没问题,不过这样没有达到考察要求。
    之后我尝试自己实现使用字符串表示的非负整数的加法,减法也可以实现,不过感觉在这题上有点大材小用了,就此作罢。
    还有一种思路是:对字符串进行9位一截断,
Integer.MAX_VALUE=2147483647
是10位,每9位表示一个整数,这也是可行的,不再赘述。
编辑于 2018-03-09 19:50:03 回复(0)
请教下各位,我这为什么是错的,一直说答案错误? import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
   
public class Main {
   
    public static void main(String[] args) {
   
        Scanner scanner = new Scanner(System.in);
   
        //测试用例个数
        int num = 0;
        num = scanner.nextInt();
        
        int i = 0;
        long a,b,c;
        List<String> list = new ArrayList<>();
        while (i < num) {
            a = scanner.nextLong();
            b = scanner.nextLong();
            c = scanner.nextLong();
            if ((a+b) > c) {
                list.add(i,"true");
            } else {
                list.add(i,"false");
            }
            i++;
        }
   
        for (int j=0;j<num;j++) {
            System.out.println("Case #"+ (j+1) +":"+list.get(j));
            System.out.println();
        }
    }
}

编辑于 2017-12-15 12:09:58 回复(0)

问题信息

难度:
32条回答 91283浏览

热门推荐

通过挑战的用户

A+B和C (15)