浙大机试2013:A+B and C (64bit) (20)
来源:互联网 发布:js捕获事件target 编辑:程序博客网 时间:2024/05/29 17:26
http://www.patest.cn/contests/pat-a-practise/1065
Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C.
Input Specification:
The first line of the input gives the positive number of test cases, T (<=10). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.
Output Specification:
For each test case, output in one line "Case #X: true" if A+B>C, or "Case #X: false" otherwise, where X is the case number (starting from 1).
Sample Input:31 2 32 3 49223372036854775807 -9223372036854775808 0Sample Output:
Case #1: falseCase #2: trueCase #3: false
费了老半天的劲写了一堆渣代码出来,然后一看别人的解法,顿时就蒙了。为什么这么简单!!!!
虽然代码写得烂,贴出来留作纪念吧。自我安慰一下吧,我的算法可以处理任意位数的数据,只需要把数组大小改一下就可以了。
#include <stdio.h>#include <string.h>#define max(a,b) ((a)>(b)?(a):(b))char A[63];char B[63];char C[63];int nA[15];int nB[15];int nC[15];int nsum[15];int len[3];void init(){int i;for(i = 0;i < 15;i++)nA[i] = nB[i] = nC[i] = 0;}int trans(char A[],int n[]){int l = strlen(A);int i = l,j,tmp_i;int sum = 0,cnt = 0;int end = 0;if(A[0] == '-') end = 1;tmp_i = i;i = i - 5;while(i >= end){for(j = 0;j < 5;j++){sum = sum * 10 + A[i+j] + (0 - '0');}tmp_i = i;i = i - 5;n[cnt++] = sum;sum = 0;}if(i < end){i = end;for(j = 0;i + j < tmp_i;j++)sum = sum * 10 + A[i+j] + (0 - '0');n[cnt++] = sum;}return cnt;}int main(){int T,i,j,k;int case_n;scanf("%d",&T);for(i = 1;i <= T;i++){init();scanf("%s %s %s",A,B,C);len[0] = trans(A,nA);len[1] = trans(B,nB);len[2] = trans(C,nC);if(A[0] != '-' && B[0] != '-') case_n = 0;else if(A[0] != '-' && B[0] == '-') case_n = 1;else if(A[0] == '-' && B[0] != '-') case_n = 2;else if(A[0] == '-' && B[0] == '-') case_n = 3;j = max(len[0],len[1]);k = 0;switch(case_n){case 0:while(k < j){nsum[k] = nA[k] + nB[k];k++;}break;case 1:while(k < j){nsum[k] = nA[k] - nB[k];k++;}break;case 2:while(k < j){nsum[k] = nB[k] - nA[k];k++;}break;case 3:while(k < j){nsum[k] = -(nA[k] + nB[k]);k++;}break;}for(k = 0;k < j;k++)nA[k] = 0;if(C[0] == '-'){j = max(j,len[2]);k = 0;while(k < j){nA[k] = nsum[k] + nC[k];k++;}}else{j = max(j,len[2]);k = 0;while(k < j){nA[k] = nsum[k] - nC[k];k++;}}for(k = 0;k < j - 1;k++){while(nA[k] < 0){nA[k+1]--;nA[k] = nA[k] + 100000;}while(nA[k] >= 100000){nA[k+1]++;nA[k] = nA[k] - 100000;}}k = j - 1;while(k >= 0){if(nA[k] == 0)k--;else break;}if( k >= 0 && nA[k] > 0)printf("Case #%d: true\n",i);elseprintf("Case #%d: false\n",i);}return 0;}
0 0
- 浙大机试2013:A+B and C (64bit) (20)
- 浙大PAT 1065. A+B and C (64bit) (20)
- 浙大PAT 1065 A+B and C (64bit) (20)
- A+B and C (64bit) (20)
- PAT A 1065. A+B and C (64bit) (20)
- PAT-A 1065. A+B and C (64bit) (20)
- PAT-A-1065. A+B and C (64bit) (20)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065.A+B and C (64bit)
- pat1065 A+B and C (64bit)
- PAT A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065 A+B and C(64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- 1065. A+B and C (64bit)
- ActiveMQ安装
- 那年,一步一步学linux c ---sigprocmask 阻塞进程---sem_wait/sem_post
- AEAI DP开发平台v3.3.0_20150228发版说明
- Selenium WebDriver之JavaScript
- android上的i-jetty (1)环境搭建
- 浙大机试2013:A+B and C (64bit) (20)
- Bitmap基本概念及在Android4.4系统上使用BitmapFactory的注意事项
- Android Studio系列教程三--快捷键
- 01-复杂度1. 最大子列和问题(20)
- 增加ubuntu分区的容量
- 【ShaderToy】开篇
- 条形码应用之一--------函数库的简介
- 月薪3000与月薪30000的文案区别
- Android之ArrayAdapter用法总结