• 主题

    Calculate a + b

  • 杭电OJ-1000

    • Input

      Each line will contain two integers A and B. Process to end of file.

    • Output

      For each case, output A + B in one line.

    • Mine

      #include <stdio.h>
      
      int main()
      {
      int a,b;
      while(~scanf("%d %d",&a,&b)) //多次输入a和b。
      {
      printf("%d\n",a+b);
      }
      } /***
      while(~scanf("%d %d",&a,&b)) 多次输入a和b。
      这句话中的“~”符号可以理解为“重复”,代码含义是反复执行scanf(“%d %d”,&a,&b) 语句,直到语句接收不到有效结果。换一种说法就是while语句会在括号中的判断为真的情况执行语句,那么对于scanf函数而言,判断为真也就是接收到了有效数据。而~符号代表无限重复,直到scanf语句不能取到有效的值为止(while的括号中判断为假),循环跳出。
      ***/
    • Review

      题目: 接收两个整数并返回两个数的和。

      需要注意的是题目中说明了每行两个数据,但并没有说明多少行。

      换一种常用说法叫:“多组数据”,是常见的要求。但没有C语言算法书会写明接收多组数据的方式。

  • 杭电OJ-1001

    • Description

      calculate SUM(n) = 1 + 2 + 3 + ... + n.

    • Input

      The input will consist of a series of integers n, one integer per line.

    • Outpu

      For each case, output SUM(n) in one line, followed by a blank line. You may assume the result will be in the range of 32-bit signed integer.

    • Mine

      #include<stdio.h>
      
      int main()
      {
      int x,n,sum;
      scanf("%d",&x);
      scanf("%d",&n);
      for(x=1;x<=n;x++)
      {
      sum=0;
      sum=sum+x;
      }
      printf("1\n%d",sum); }
    • Writeup

      #include <stdio.h>
      int main()
      {
      int a;
      int sum=0;
      while((scanf("%d",&a))!=EOF){
      for(int i=0;i<=a;i++)
      sum = sum+i;
      printf("%d\n\n",sum);
      sum = 0;
      }
      return 0;
      }
    • Review

      • 我的问题在于没有考虑连续读取的可能性,好像对输入输出有误解T-T
      • 有一个疑问:输入输出需要和sample一样的格式吗?
      • 本身是一个前N项和的累加问题,如果用公式法也是no accept,原因是S=(1+n)*n/2中乘法容易造成溢出,而循环累加的好处在于溢出的可能比较小。
  • 杭电OJ-1002

    • Description

      Given two integers A and B, your job is to calculate the Sum of A + B.

    • Input

      The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.

    • Output

      For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.

    • 手写代码@V1-20210805

      #include<stdio.h>
      #include<string.h>
      #define max 1005 int main()
      {
      int a[max],b[max],c[max];
      while(~scanf("%s1 %s2",s1,s2))
      {
      int i,j,k;
      i=0;j=0;
      k=strlen(s1)>strlen(s2)?strlen(s1):strlen(s2) for(i;i<=k;i++) //k有无定义的必要?
      {
      a[]=s1; //好像不太合适?字符数组能直接赋值给数组吗?查一下书
      b[]=s2; //似乎需要循环读入? c[i]=a[i]+c[i];
      if(c[i]>=10)
      {
      c[i]=c[i]%10;
      c[i+1]++; //s2的长度是必要的吗?
      }
      }
      for(j=0;j<=k;j++)
      {
      printf("%d",a[j]); //哪里有点奇怪
      }
      } }
    • v1现在存在的问题

      • 读入的顺序和相加的顺序不太对?要处理一下?[n-i]好像可行?
      • 输出的时候应该是逆序?
      • 需要加一个读入的限制条件:读取正整数?
      • 题目要求的输入输出 用一个循环?
    • 手写代码@V2-20210806

      #include<stdio.h>
      #include<string.h>
      #define max 1010 int main()
      {
      int T,u,n,i,j;
      char s1[max],s2[max],c[max];
      scanf("%d",&T);
      while(T>=1 && T<=20)
      {
      for(u=0;u<=T;u++)
      {
      scanf("%s %s",s1,s2);
      } n =strlen(s1)>strlen(s2)?strlen(s1):strlen(s2); for(i=2 ; i<=n ; i++)
      {
      c[n-i]=s1[n-i]+s2[n-i];
      if(c[i]>=10)
      {
      c[n-i]=c[n-i]%10;
      c[n-i-1]++; //没有考虑不是同位数的情况
      }
      } for(j=0;j<=n;j++)
      {
      printf("case %d:\n",u);
      printf("%s + %s = %d\n",s1,s2,c[j]);
      } } }
    • writeup-0806

      #include<stdio.h>
      #include<string.h>
      #define max 1000+10 /**
      * 1. define the variable
      **/ int a[max],b[max];
      char str1[max],str2[max]; int main(){ int m; //test number T?
      int k=1;
      scanf("%d",&m); // read T? /**
      *2.read number and make sure input.
      this part is to read the big number and covert to array
      **/ while(m--){ //this circle ie funny! it's better than mine which use more variable u. scanf("%s %s",str1,str2); //read big number
      memset(a,0,sizeof(a));
      memset(b,0,sizeof(b)); //memset() 函数可以说是初始化内存的“万能函数”,通常为新申请的内存进行初始化工作。 int i,j;
      for(i=0,j=strlen(str1)-1;i<strlen(str1);i++){ //it's similar to my code ,i use the i=1 to replace len
      a[j--]=str1[i]-'0'; //string1 covert to array1?
      } for(i=0,j=strlen(str2)-1;i<strlen(str2);i++){ //two strlen conditions,less code ,nice~
      b[j--]=str2[i]-'0'; //string2 to array2? why not combine with above 'for cicle' together ?
      }
      /**
      * 3. add two big number
      **/
      for(i=0;i<max;i++){
      a[i]+=b[i];
      if(a[i]>=10){
      a[i]-=10; //mine : a[i]=a[i]%10
      a[i+1]+=1; //similar~
      }
      }
      /**
      * 4.output
      *
      **/
      printf("Case %d:\n",k++); // k has been defined and value=1?
      printf("%s + %s = ",str1,str2);
      for(i=max-1;(i>=0)&&(a[i]==0);i--); //reverse output?(i>=0)&&(a[i]==0) what's mean? only deal the 10?
      if(i>=0){
      for(;i>=0;i--){
      printf("%d",a[i]);
      }
      }
      else printf("0");
      if(m!=0) printf("\n\n");
      else printf("\n"); //not clear..
      }
      return 0;
      }
    • Review

      • 大数加法问题一般考虑数组进行存储,然后按位相加满十进一。
      • 再看Writeup时发现大家再提java,import java.util.Scanner,以及大数需要import java.math.BigInteger,且BigInterger相加不是"a+b",而是"a.add(b)",就可以很好的解决大数问题。
    import java.util.Scanner;
    import java.math.BigInteger;
    public class Main{
    public static void main(String args[]){
    BigInteger a,b;
    int T;
    int n=1;
    Scanner in = new Scanner(System.in);
    T=in.nextInt();
    while(T>0){
    a=in.nextBigInteger();
    b=in.nextBigInteger();
    System.out.println("Case "+n+":");
    System.out.println(a+" + "+b+" = "+a.add(b));
    if(T!=1) System.out.println();
    T--;
    n++;
    }
    }
    }
  • 杭电OJ-1089

    • Input

      The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

    • Output

      For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

    • **Mine **- accepted

      #include<stdio.h>
      
      int main()
      { int a,b;
      while(~scanf("%d %d",&a,&b))
      {
      printf("%d\n",a+b); }
      return 0; }
  • 杭电OJ-1090

    • Input

      Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.

    • Output

      For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

    • Mine

      #include<stdio.h>
      
      int i,a,b,c;
      int main()
      {
      while(~scanf("%d",&i))
      {
      for(c=1;c<=i;c++)
      {
      scanf("%d %d",&a,&b);
      printf("%d\n",a+b);
      }
      }
      }
  • 杭电OJ-1091

    • Input

      Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.

    • Output

      For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

    • Mine

      #include<stdio.h>
      
      int a,b;
      int main()
      {
      while(~scanf("%d %d",&a,&b))
      {
      if(a==0 && b==0)
      {return 0;}
      else
      {
      printf("%d\n",a+b);
      }
      }
      }
  • 杭电OJ-1092

    • Input

      Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.

    • Output

      For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

    • Mine

      #include<stdio.h>   //始终是wrong...
      
      int main()
      {
      int a,n,sum;
      while(scanf("%d",&n)!=EOF && n!=0)
      {
      while(n--)
      {
      scanf("%d",&a);
      sum=0;
      sum+=a;
      }
      printf("%d\n",sum);
      }
      return 0;
      }
    • Writeup

      #include<stdio.h>
      int main()
      {
      int a[10000];
      int n, i, s;
      while (scanf("%d", &n) && n)
      //输入正确scanf返回1,n!=0继续输入
      {
      for (i = s = 0; i < n; i++)
      scanf("%d", &a[i]);
      for (i = 0; i < n; i++)
      {
      s = s + a[i];
      }
      printf("%d\n", s);
      }
      return 0;
      }
    • Review

      没有考虑到大数的可能,存在溢出问题,用数组存放更为合理

  • 杭电OJ-1093

    • Input

      Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.

    • Output

      For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

    • Mine

      #include<stdio.h>   //wrong answer...
      
      int main()
      {
      int i,j,k,s;
      int a[1000];
      while(~scanf("%d",&i))
      {
      for(i;i>=1;i--)
      {
      while(scanf("%d",&j)!=EOF && j)
      {
      for(j;j>=0;j--)
      {
      scanf("%d", &a[i]);
      }
      for (k= 0; k< j; k++)
      {
      s += a[i];
      }
      printf("%d\n", s);
      }
      }
      }
      }
    • writeup

      #include<stdio.h>
      int main()
      {
      int n,i,m,sum; //n是行数,m是加数个数
      scanf("%d",&n);
      while(n--) //简洁!
      {
      sum=0;
      scanf("%d",&m);
      while(m--) //两个-- 简洁欸!
      {
      scanf("%d",&i);
      sum=sum+i;
      }
      printf("%d\n",sum);
      }
      return 0;
      }
    • Review

      所以上一道题不是大数的原因?? (((φ(◎ロ◎;)φ)))

  • 杭电OJ-1094

    • Input

      Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.

    • Output

      For each test case you should output the sum of N integers in one line, and with one line of output for each line in input.

    • Mine

      #include<stdio.h>
      
      int main()
      {
      int n,m,sum;
      while(~scanf("%d",&n))
      {
      sum = 0;
      while(n--)
      {
      scanf("%d",&m);
      sum += m;
      }
      printf("%d\n",sum);
      }
      }
  • 杭电OJ-1095

    • Input

      The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

    • Output

      For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.

    • Mine

      #include<stdio.h>
      int main()
      {
      int a,b;
      while(~scanf("%d %d",&a,&b))
      {
      printf("%d\n",a+b);
      printf("\n");
      }
      return 0;
      }
  • 杭电OJ-1096

    • Input

      Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.

    • Output

      For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.

    • Mine

      #include<stdio.h>
      int main()
      {
      int n,m,i;
      int sum;
      while(~scanf("%d\n",&n)) //行数
      {
      while(n--)
      {
      scanf("%d",&m); //加数个数
      sum = 0;
      while(m--)
      {
      scanf("%d",&i);
      sum += i;
      }
      printf("%d\n",sum);
      if(m!=0)
      printf("\n",sum);
      }
      }
      }

杭电OJ 输入输出练习汇总的更多相关文章

  1. C#利用POST实现杭电oj的AC自动机器人,AC率高达50%~~

    暑假集训虽然很快乐,偶尔也会比较枯燥,,这个时候就需要自娱自乐... 然后看hdu的排行榜发现,除了一些是虚拟测评机的账号以外,有几个都是AC自动机器人 然后发现有一位作者是用网页填表然后按钮模拟,, ...

  2. 杭电oj 2095 & 异或^符号在C/C++中的使用

    异或^符号,在平时的学习时可能遇到的不多,不过有时使用得当可以发挥意想不到的结果. 值得注意的是,异或运算是建立在二进制基础上的,所有运算过程都是按位异或(即相同为0,不同为1,也称模二加),得到最终 ...

  3. 用python爬取杭电oj的数据

    暑假集训主要是在杭电oj上面刷题,白天与算法作斗争,晚上望干点自己喜欢的事情! 首先,确定要爬取哪些数据: 如上图所示,题目ID,名称,accepted,submissions,都很有用. 查看源代码 ...

  4. 杭电oj 4004---The Frog Games java解法

    import java.util.Arrays; import java.util.Scanner; //杭电oj 4004 //解题思路:利用二分法查找,即先选取跳跃距离的区间,从最大到最小, // ...

  5. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  6. 杭电oj————2057(java)

    question:A+ B again 思路:额,没啥思路/捂脸,用java的long包里的方法,很简单,只是有几次WA,有几点要注意一下 注意:如果数字有加号要删除掉,这里用到了正则表达式“\\+” ...

  7. 爬取杭电oj所有题目

    杭电oj并没有反爬 所以直接爬就好了 直接贴源码(参数可改,循环次数可改,存储路径可改) import requests from bs4 import BeautifulSoup import ti ...

  8. 『ACM C++』HDU杭电OJ | 1418 - 抱歉 (拓扑学:多面体欧拉定理引申)

    呕,大一下学期的第一周结束啦,一周过的挺快也挺多出乎意料的事情的~ 随之而来各种各样的任务也来了,嘛毕竟是大学嘛,有点上进心的人多多少少都会接到不少任务的,忙也正常啦~端正心态 开心面对就好啦~ 今天 ...

  9. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

随机推荐

  1. 玩转html2canvas以及常见问题解决

    前端小伙伴经常会遇到页面截图或者把网页中指定的区域(某个大div)的内容转换成png的图片.这个时候常常会用到html2canvas库来实现,js真的很强大. 我最近也遇到了一个需求,需要把输入的文本 ...

  2. 看完互联网大佬的「LeetCode 刷题手册」, 手撕了 400 道 Leetcode 算法题

    大家好,我是 程序员小熊 ,来自 大厂 的程序猿.相信绝大部分程序猿都有一个进大厂的梦想,但相较于以前,目前大厂的面试,只要是研发相关岗位,算法题基本少不了,所以现在很多人都会去刷 Leetcode ...

  3. 流程自动化RPA,Power Automate Desktop系列 - 创建WPF程序安装包及升级包

    一.背景 之前写过的几个WPF小工具,每次发布都需要给它打安装包和升级包,涉及到一些系列繁琐的手工操作,有了Power Automate Desktop,于是便寻思着能不能做成一个自动化的流来使用. ...

  4. JS replace 替换全部数据

    (1)使用具有全局标志g的正则表达式 var str = "dogdogdog"; var str2 = str.replace(/dog/g,"cat");/ ...

  5. Jquery Validate动态添加和删除校验规则

    以下面文本框为例, <input type="text"name="username" id="username"/> 我们要让 ...

  6. Swoole实现毫秒级定时任务

    项目开发中,如果有定时任务的业务要求,我们会使用linux的crontab来解决,但是它的最小粒度是分钟级别,如果要求粒度是秒级别的,甚至毫秒级别的,crontab就无法满足,值得庆幸的是swoole ...

  7. 简单学习java内存马

    看了雷石的内存马深入浅出,就心血来潮看了看,由于本人java贼菜就不介绍原理了,本文有关知识都贴链接吧 前置知识 本次主要看的是tomcat的内存马,所以前置知识有下列 1.tomcat结构,tomc ...

  8. Python报错“UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)”的解决办法

    最近在用Python处理中文字符串时,报出了如下错误: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ...

  9. python使用笔记16--操作redis

    操作redis应先引入第三方模块 执行以下命令 pip install redis 1.redis常用方法 1 import redis 2 #decode_responses=True将bytes转 ...

  10. 使用Nginx将请求转发至Google Analytics实现后端数据统计

    前言 Google Analytics 加载缓慢是本博客在国内访问缓慢的原因之一.虽然通过使用大公司的 ga.js 的 CDN ,可以很大程度上加快加载 ga.js 文件的速度( ga.js 的更新频 ...