62
推荐
import java.uti
查看全部
编辑于 2015-06-19 17:40:10
回复(44)
34
关键是处理溢出问题,溢出情况有:
a和b都是正数,a+b >= 2^32;
a和b都是负数,a+b <= -2^32;
因此溢出只出现在a b 同号,a b异号的话直接按 (a+b) > c 来算就可以。
对于a b同号,也有两种处理方法:
1. b c 同号, 将 (a+b) > c 转换成 a > (c - b), 此时 (c - b)必定不会出现溢出;
1. b c 异号, 由于式子左边同号,相加后符号不变,式子右边跟左边异号,因此不需要计算即可知道大小关系。
发表于 2017-03-20 13:55:25
回复(10)
30
这题就是考一个int整数的表示范围问题吧。有符号整数大概到20亿 超过了就要用long long 来接收输入
#include
int main(){
int n;
long long A,B,C;
scanf("%d",&n);
for(int i=0;i
scanf("%lld %lld %lld",&A,&B,&C);
printf("Case #%d: %s\n",i+1,(A+B)>C?"true":"false");
}
return 0;
}
编辑于 2017-01-09 17:15:27
回复(6)
17
python 4行解法: a=int(input())
for i in range(a):
b,c,d=map(int,input().split())
print("Case #"+str(i+1)+": true" if b+c>d else "Case #"+str(i+1)+": false")
发表于 2017-10-09 11:17:42
回复(8)
5
/**
* A+B和C (15)
* 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
* 题目描述
* 给定区间[-2的31次方, 2的31次方]内的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开始)。
* 输入例子:
* 4
* 1 2 3
* 2 3 4
* 2147483647 0 2147483646
* 0 -2147483648 -2147483647
* 输出例子:
* Case #1: false
* Case #2: true
* Case #3: true
* Case #4: false
*
* @author shijiacheng
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int i = 0; i < T; i++) {
BigInteger A = sc.nextBigInteger();
BigInteger B = sc.nextBigInteger();
BigInteger C = sc.nextBigInteger();
BigInteger sum = A.add(B);
if (sum.compareTo(C) > 0) {
System.out.println("Case #" + (i + 1) + ": true");
} else {
System.out.println("Case #" + (i + 1) + ": false");
}
}
}
}
发表于 2018-01-23 22:36:25
回复(2)
8
#include
#include
int cmp(long int a,long int b,long int c)
{
int r =((a+b)>c?(1):(0));
return r;
}
int main()
{
int num = 0,i=0;
long int a,b,c;
scanf("%d",&num);
int k[num];
for(i=0;i
scanf("%ld%ld%ld",&a,&b,&c);
k[i] = cmp(a,b,c);
}
for(i=0;i
if(k[i]==1)
printf("Case #%d: true\n",i+1);
else
printf("Case #%d: false\n",i+1);
}
return 0;
}
发表于 2015-08-26 10:24:07
回复(2)
3
import java.io.*;
public class PAT1001 {
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
long[] as = new long[n], bs = new long[n], cs = new long[n];
for(int i=0;i
String[] line = in.readLine().split(" ");
as[i] = Long.parseLong(line[0]);
bs[i] = Long.parseLong(line[1]);
cs[i] = Long.parseLong(line[2]);
}
for(int i=0;i
System.out.println("Case #" + (i+1) + ": " + //isGreaterThan(as[i], bs[i], cs[i]));
(as[i]+bs[i]>cs[i]));
}
}
public static boolean isGreaterThan(int a, int b, int c){
if(a>0&&b>0&&a+b<0) return true;
if(a<0&&b<0&&a+b>=0) return false;
return a+b>c;
}
} 就跟我说
1
2147483648 2147483648 2147483648
过不去 然而我这能过去啊... 什么鬼
发表于 2017-08-16 13:52:53
回复(2)
3
n = int(raw_input())
ls = []
while n > 0:
temp = raw_input().split()
ls.append(temp)
n -= 1
i = 1
for temp in ls:
if float(temp[0])+float(temp[1])>float(temp[2]):
print "Case #%d: true" % i
else:
print "Case #%d: false" % i
i += 1
用python好像很简单啊
发表于 2015-10-21 16:51:30
回复(0)
5
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean[] result = new boolean[n];
for(int i = 0; i < n; i++)
{
long a = sc.nextLong();
long b = sc.nextLong();
long c = sc.nextLong();
if((c-a)
else result[i] = false;
}
for(int i = 0; i < n; i++)
System.out.println("Case #" + (i+1) + ": "+result[i]);
}
}
发表于 2017-07-28 13:02:02
回复(1)
2
编辑于 2017-04-24 13:40:11
回复(1)
2
import java.util.ArrayList;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList list = new ArrayList();
long a,b,c;
int i=1;
int count = scan.nextInt();
while(count > 0){
a = scan.nextLong();
b = scan.nextLong();
c = scan.nextLong();
if(a+b>c)list.add("true");
else list.add("false");
count --;
}
/*
* case #1: false
*/
for(String str:list){
System.out.println("Case #"+i+": "+str);
i++;
}
}
}
提交了多次,错误的原因竟然是因为加了package。
发表于 2015-08-13 21:41:49
回复(3)
2
#include
#include
#include
#include
using namespace std;
int main(){
int i;
long a,b,c;
scanf("%d",&i);
if (i>10 ){
printf("请重新输入循环次数,小于等于10\n");
}else{
for(int j=1; j<=i; ++j)
{
scanf("%ld %ld
%ld",&a,&b,&c);
if((a+b)>c){
printf("Case #%d: true\n",j);
}else{
printf("Case #%d: false\n",j);
}
}
}
return 0;
}
没看清题目,打印的每行需要换行,巨坑。。。
发表于 2015-08-13 00:42:16
回复(0)
3
#include
using namespace std;
int main(void)
{
int T;
cin >> T;
long int **pt = new long int*[T];
for (int i = 0; i
{
pt[i] = new long int[3];
cin >> pt[i][0] >> pt[i][1] >> pt[i][2];
}
for (int i = 0; i < T; i++)
if (pt[i][0] + pt[i][1] > pt[i][2])
cout << "Case #" << i+1 << ": true" << endl;
else
cout << "Case #" << i+1 << ": false" << endl;
return 0;
}
编辑于 2018-06-16 19:24:39
回复(0)
4
#include
#include
int comp(long int a,long int b,long int c)
{
int r =(a+b)>c?(1):(0);
return r;
}
int main()
{
int num = 0,i=0;
long int a,b,c;
scanf("%d",&num);
int result[num];
for(i=0;i
scanf("%ld%ld%ld",&a,&b,&c);
result[i] = comp(a,b,c);
}
for(i=0;i
if(result[i]==1)
printf("Case #%d: true\n",i+1);
else
printf("Case #%d: false\n",i+1);
}
return 0;
}
按照题目要求,先接收输入的测试组数,再输入每一组数据时,进行调用比较函数判断并将结果存于一个暂时的数组中,做好对应关系。等全部输入完后,再将原先存好的数组数据判断,输出结果,如此,并可实现题目的一串输入,一串结果输出。
发表于 2015-08-18 16:03:37
回复(3)
2
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)
2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int p = 0;
//System.out.println("Type T that 0 < T <= 10 :");
Scanner _scanner = new Scanner(System.in) ;
int T = _scanner.nextInt() ;
while (T <= 0 || T > 10) {
System.out.println("retype T that 0 < T <= 10 :");
Scanner _scanner1 = new Scanner(System.in) ;
T = _scanner1.nextInt() ;
}
//System.out.println("输入" + T + "组测试用例,每组A B C三个数,并用空格隔开:");
long[][] _2darray = new long[T][3] ;
for (int i = 0; i < T; i++) {
for (int j = 0; j < 3; j++) {
_2darray[i][j] = _scanner.nextLong() ;
}
}
for (int i = 0; i < T; i++) {
if (_2darray[i][0] + _2darray[i][1] > _2darray[i][2]) {
System.out.println("Case #" + ++p + ": " + "true");
} else {
System.out.println("Case #" + ++p + ": " + "false");
}
}
}
}
发表于 2017-08-02 00:15:22
回复(2)
2
s = input()
for i in range(1,s+1):
num = raw_input().split(' ')
if int(num[0])+int(num[1])>int(num[2]):
print 'Case #'+str(i)+': true'
else:
print 'Case #'+str(i)+': false' python代码系列 注意python(2.7)获取的只能是单独的一行int或者一行字符串
发表于 2017-07-29 10:48:13
回复(0)
1
#include
#include
#include
#include
using namespace std;
int T;
long long A,B,C;
bool flag=false;
int i=1;
int main(){
scanf("%d",&T);
//while(T--){
//cin>>A>>B>>C;
//if(A+B>C)
//flag=true;
//else
//flag=false;
//printf("Case #%d: ",i++);
//cout<
//}
while(T--){
cin>>A>>B>>C;
printf("Case #%d: %s\n",i++,(A+B)>C?"true":"false");
}
return 0;
}
发表于 2020-02-06 16:36:48
回复(0)
1
用int类型就行
#include
using namespace std;
int main()
{
int i,j,n,a,b,c;
cin>>n;
for(i=1;i<=n;i++){
cin>>a>>b>>c;
if((a*0.5+b*0.5)>(c*0.5))
cout<
else
cout<
}
return 0;
}
发表于 2020-01-15 11:22:13
回复(0)
1
分析 因为在给定区间
判断,该区间右端点在int范围已经溢出,且
也在int范围是溢出的,说明加数,和均会溢出。所以不能使用int类型存储输入数据,需要用long long类型来保证计算不会溢出。 /*
* app=PAT-Basic lang=c++
* https://pintia.cn/problem-sets/994805260223102976/problems/994805312417021952
*/
#include
int main()
{
int T;
long long A, B, C,sum;
scanf("%d",&T);
for (int i = 0; i < T;i++){
scanf("%lld%lld%lld",&A,&B,&C);
sum = A + B;
if (sum > C){
printf("Case #%d: true\n",i + 1);
}
else{
printf("Case #%d: false\n", i + 1);
}
}
return 0;
}
编辑于 2019-12-02 13:07:39
回复(0)
1
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)