输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。
对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从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
#include <stdio.h> #include <stdlib.h> #include <string.h> struct group { /*将字符串转换为整型*/ int a; int b; int c; /*用于存放输入数字的字符串*/ char astr[12]; char bstr[12]; char cstr[12]; }; void judgeIfFlow(char *inputStr, char *outputStr, int *pIfFlow, int i) //根据输入字符串和输出字符串的比较,判断数据是否溢出函数 { int j; if(strlen(inputStr) == strlen(outputStr)) //若输入字符串与输出字符串位数相同,则从高位向低位逐个字符比较 { for(j = 0;j < strlen(outputStr); j++) { if(inputStr[j] == outputStr[j]) continue; else { printf("Case #%d: false\n", i+1); //存在不同的字符,即输入的数字字符与输出的数字字符不同,则发生溢出 *pIfFlow = 1; break; } } } else //位数不同,则一定发生溢出 { if(strcmp(inputStr,"2147483648") != 0) //除去了等于2147483648的情况,因为实际上它会发生溢出,但此值又属于取值范围内 { printf("Case #%d: false\n", i+1); *pIfFlow = 1; } } } int main() { struct group g[10] = {0}; int n,i; int ifFlow = 0; /*用于存放输出数字的字符串*/ char astrjud[12]; char bstrjud[12]; char cstrjud[12]; /*设定测试组的个数*/ scanf("%d", &n); getchar(); for(i = 0; i < n; i++) { scanf("%s %s %s", g[i].astr, g[i].bstr, g[i].cstr); getchar(); /*由于在VS2008下的aiot函数会将溢出的整数字符串转换为不发生溢出的最大值,即将atoi("2147483648") = 2147483647,故转换为数字后要+1 而有的编译器会atoi("2147483648") = 2147483648 = -2147483648,比如本题的OJ,因此67-75行要根据编译器的实际行为更改,即把+1去掉*/ if(strcmp(g[i].astr,"2147483648") == 0) g[i].a = atoi(g[i].astr) + 1; else g[i].a = atoi(g[i].astr); if(strcmp(g[i].bstr,"2147483648") == 0) g[i].b = atoi(g[i].bstr) + 1; else g[i].b = atoi(g[i].bstr); g[i].c = atoi(g[i].cstr); } for(i = 0; i < n; i++) { sprintf(astrjud,"%d",g[i].a); sprintf(bstrjud,"%d",g[i].b); sprintf(cstrjud,"%d",g[i].c); judgeIfFlow(g[i].astr, astrjud, &ifFlow, i); judgeIfFlow(g[i].bstr, bstrjud, &ifFlow, i); judgeIfFlow(g[i].cstr, cstrjud, &ifFlow, i); if(ifFlow) //进行溢出判断后,若溢出标志位为1,则发生溢出,终止本次循环,执行下一循环 continue; /*若a和b同时输入大于等于0*/ if((g[i].astr[0] != '-') && (g[i].bstr[0] != '-')) { if(strcmp(g[i].cstr,"2147483648") == 0) //若c为2147483648 { if((g[i].a + g[i].b > -2147483648) && (g[i].a + g[i].b < 0)) //a+b发生溢出且大于-2147483648,即-2147483648 <a+b <0,则大于c printf("Case #%d: true\n", i+1); else if(g[i].a + g[i].b == 0) //a+b=0,有两种情况:a=b=0 或 a=b=2147483648 { if(g[i].astr[0] == '0' && g[i].bstr[0] == '0') //a=b=0,则小于c printf("Case #%d: false\n", i+1); else //a=b=2147483648,则大于c printf("Case #%d: true\n", i+1); } else //a+b<2147483648,即小于c printf("Case #%d: false\n", i+1); } else if(g[i].c >= 0) //c大于等于0且不等于2147483648的情况 { if(g[i].a + g[i].b < 0) //a+b发生溢出,则大于c printf("Case #%d: true\n", i+1); else if(g[i].a + g[i].b == 0) //同上 { if(g[i].astr[0] == '0' && g[i].bstr[0] == '0') printf("Case #%d: false\n", i+1); else printf("Case #%d: true\n", i+1); } else if(g[i].a + g[i].b > g[i].c) //除去溢出情况外的比较 printf("Case #%d: true\n", i+1); else printf("Case #%d: false\n", i+1); } else //c小于0,则a+b>c printf("Case #%d: true\n", i+1); } /*若a和b同时输入小于0*/ else if((g[i].astr[0] == '-') && (g[i].bstr[0] == '-')) { if(strcmp(g[i].cstr,"-2147483648") == 0) //若c为-2147483648 { if((g[i].a + g[i].b > -2147483648) && (g[i].a + g[i].b < 0)) //a+b没有发生溢出,则大于c printf("Case #%d: true\n", i+1); else //否则发生溢出,则小于c printf("Case #%d: false\n", i+1); } else if(g[i].c <= 0) //c小于等于0且不等于-2147483648的情况 { if(g[i].a + g[i].b >= 0) //a+b发生溢出,则小于c printf("Case #%d: false\n", i+1); else if(g[i].a + g[i].b < g[i].c) //除去溢出情况外的比较 printf("Case #%d: false\n", i+1); else printf("Case #%d: true\n", i+1); } else //c大于0,则a+b<c printf("Case #%d: false\n", i+1); } /*若a和b异号,或一个为0一个为负*/ else { /*若c为-2147483648,则只有当a、b一个为0且另一个为-2147483648时,才不满足a+b>c,其他情况一定满足a+b>c*/ if(strcmp(g[i].cstr,"-2147483648") == 0) { if((strcmp(g[i].astr,"-2147483648") == 0 && strcmp(g[i].bstr,"0")) || (strcmp(g[i].bstr,"-2147483648") == 0 && strcmp(g[i].astr,"0"))) printf("Case #%d: false\n", i+1); else printf("Case #%d: true\n", i+1); } /*若a、b一个为0且另一个为2147483648,则只有当c为2147483648时,才不满足a+b>c,其他情况一定满足a+b>c*/ else if((strcmp(g[i].astr,"2147483648") == 0 && strcmp(g[i].bstr,"0")) || (strcmp(g[i].bstr,"2147483648") == 0 && strcmp(g[i].astr,"0"))) { if(strcmp(g[i].cstr,"2147483648") == 0) printf("Case #%d: false\n", i+1); else printf("Case #%d: true\n", i+1); } /*除去两种极端情况下的比较*/ else if(g[i].a + g[i].b > g[i].c) printf("Case #%d: true\n", i+1); else printf("Case #%d: false\n", i+1); } } return 0; }
/**
* 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");
}
}
}
}
就跟我说 1 2147483648 2147483648 2147483648 过不去 然而我这能过去啊... 什么鬼
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)<b) result[i] = true; else result[i] = false; } for(int i = 0; i < n; i++) System.out.println("Case #" + (i+1) + ": "+result[i]); } }
import java.util.ArrayList; import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<String> list = new ArrayList<String>(); 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。
#include<iostream>
using namespace std;
int main(void)
{
int T;
cin >> T;
long int **pt = new long int*[T];
for (int i = 0; i<T; 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;
}
#include <stdio.h> #include <stdlib.h> 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<num;i++){ scanf("%ld%ld%ld",&a,&b,&c); result[i] = comp(a,b,c); } for(i=0;i<num;i++){ if(result[i]==1) printf("Case #%d: true\n",i+1); else printf("Case #%d: false\n",i+1); } return 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)); } } }
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"); } } } }
int
范围是溢出的,说明加数,和均会溢出。所以不能使用int
类型存储输入数据,需要用long long
类型来保证计算不会溢出。 /* * app=PAT-Basic lang=c++ * https://pintia.cn/problem-sets/994805260223102976/problems/994805312417021952 */ #include <cstdio> 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; }
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())); } }
#include <bits/stdc++.h> #define il inline int #define re register int //看! #define int long long int n; int A,B,C; signed main(){ scanf("%lld",&n); for(int i=0;i<n;i++){ scanf("%lld %lld %lld",&A,&B,&C); printf("Case #%d: %s\n",i+1,(A+B)>C?"true":"false"); } return 0; }
import java.util.Scanner;