zoukankan      html  css  js  c++  java
  • A+B

    题目截图:

    思路:

      32 位系统下 int 的范围为:-2^31 ~ 2^31 - 1,因此只需将字符串转化为相应的整数然后进行加法运算即可。

    代码如下:

     1 /*
     2     A+B
     3 */
     4 
     5 #include <stdio.h>
     6 #include <string.h>
     7 #include <math.h>
     8 #include <stdlib.h>
     9 #include <time.h>
    10 #include <stdbool.h>
    11 
    12 char A[15], B[15];                    // 存储两个输入的字符串 
    13 
    14 // 将字符串转化为整数 
    15 int chartoint(char str[]) {
    16     int len = strlen(str);            // 字符长度 
    17     int flag, i=0, ans=0;            // flag用来表示正负,ans用来存储正整数 
    18     if(str[0] == '-') {                // 若为负 
    19         flag = -1;
    20         i++;                        // 从第二个字符开始处理 
    21     } else {
    22         flag = 1;
    23     }
    24     for(; i<len; ++i) {                // 提取数字 
    25         if(str[i]>='0' && str[i]<='9') {
    26             ans = ans*10 + (str[i]-'0');
    27         }
    28     }
    29     return flag*ans;                // 加上正负即是所求的整数 
    30 }
    31 
    32 int main() {
    33     while(scanf("%s %s", A, B) != EOF) {
    34         int ta = chartoint(A);        // 字符串转换成整数 
    35         int tb = chartoint(B);
    36         printf("%d
    ", ta+tb);        // 输出 A+B 
    37     }
    38 
    39     return 0;
    40 }
  • 相关阅读:
    postgres导入和导出
    postgres日常操作
    NumPy Ndarray 对象
    NumPy 简介及安装
    Python两个内置函数locals 和globals
    python之multiprocessing多进程
    postgres外部表
    css中文本超出部分省略号代替
    js中的作用域链
    css中clip:rect矩形剪裁功能
  • 原文地址:https://www.cnblogs.com/coderJiebao/p/HustTest18.html
Copyright © 2011-2022 走看看