计算a+b的和
每行包含两个整数a和b
对于每行输入对应输出一行a和b的和
输入
1 5
输出
6
#include<iostream> using namespace std; int main(){ int a,b; while(cin>>a>>b) cout<<a+b<<endl; }
int getLength(int n){ int i; for(i=0;n!=0;i++){ n=n/8; } return i; } int main(){ int num; int end; while(scanf("%d",&num)!=EOF){ end=0; int length=getLength(num); for(int i=0;i<length;i++){ end=end+(int)pow(10,i)*(num%8); num=num/8; } printf("%d",end); } }自我感觉写的挺好,和上边的大佬差太多…
//不用BigInteger,自己写 public class Main{ public static void main(String[] args) { Scanner input = new Scanner(System.in); while(input.hasNext()){ String str1 = input.next(); String str2 = input.next(); int flag = 0; if(str1.contains("-") && !str2.contains("-")){ str1 = new StringBuffer(str1.substring(1)).reverse().toString(); flag = 1; }else if(!str1.contains("-") && str2.contains("-")){ str2 = new StringBuffer(str2.substring(1)).reverse().toString(); flag = 2; }else if(str1.contains("-") && str2.contains("-")){ str1 = new StringBuffer(str1.substring(1)).reverse().toString(); str2 = new StringBuffer(str2.substring(1)).reverse().toString(); flag = 3; } String s=""; int len1 = str1.length(); int len2 = str2.length(); int len = Math.min(len1, len2); int num1,num2,a=-1,t=0; for(int i=0;i<len;i++){ num1 = str1.charAt(i)-48; num2 = str2.charAt(i)-48; if(num1+num2+t>=10){ a = num1+num2+t-10; s += a; if(a>=0){ t = 1; }else{ t = 0; } }else{ int v = num1+num2+t; s += v; t = 0; a = -1; } } if(len1>len2){ if(a>=0){ int c = str1.charAt(len2)-48+1; s += c; if(len1-len2>=2){ s += str1.substring(len2+1); } }else{ s += str1.substring(len2); } }else if(len1 == len2){ if(a>=0){ s += 1; } }else{ if(a>=0){ int c = str2.charAt(len1)-48+1; s += c; if(len2-len1>=2){ s += str2.substring(len1+1); } }else{ s += str2.substring(len1); } } if(flag == 0){ System.out.println(new StringBuffer(s).reverse()); }else if(flag == 1){ //str1有负号,str2没有 System.out.println(Long.parseLong(new StringBuffer(s).reverse().toString())-2*Long.parseLong(str1)); }else if(flag == 2){ System.out.println(Long.parseLong(new StringBuffer(s).reverse().toString())-2*Long.parseLong(str2)); }else{ System.out.println("-"+new StringBuffer(s).reverse()); } } } }
while True: try: data = raw_input().split() a = int(data[0]) b = int(data[1]) print a+b except: break