给定两个正整数int a,int b,同时给定一个int type代表运算的类型,1为求a * b,0为求a / b,-1为求a - b,但规定只能使用加号,请编写程序返回计算结果,并保证数据合法且结果一定在int范围内。
测试样例:
1,2,1
返回:2
public int calc(int a, int b, int type) { // write code here int res = 0; if(type == 1){ int tmp = 0; for(int i = 0; i < b; i++){ tmp += a; } res = tmp; }else if(type == 0 ){ int tmp = a; int count = 0; while(tmp > 0&& tmp >= b){ tmp = tmp + (~b) + 1; count++; } res = count; }else{ res = a + (~b) + 1; } return res; }
import java.util.*; public class AddSubstitution { public int calc(int a, int b, int type) { int r = 0; switch(type){ case 1: r = mult(a,b); break; case 0: r = div(a,b); break; case -1: r = sub(a,b); break; } return r; } //乘法 public int mult(int a,int b){ int r = 0; for(int i = 0;i<a;i++) r += b; return r; } //除法 public int div(int a,int b){ for(int i = 1;i<=a;i++) if(mult(b,i) <= a && mult(b,i+1) > a) return i; return 0; } //减法 public int sub(int a,int b){ if(a>=b) for(int i = 0;i<a;i++) if(i+b == a) return i; return mult(sub(b,a),-1); } }
//使用加法模拟乘法,除法转为乘法,再用加法实现,减法就先取反再加 class AddSubstitution { public: int calc(int a, int b, int type) { if(1 == type) return multiply(a,b); else if(0 == type) return divide(a,b); else return subtract(a,b); } int negate(int x){ int neg = 0; int d = x < 0 ? 1 : -1; while(x != 0){ neg += d; x += d; } return neg; } int multiply(int a, int b){ int res = 0; if((a < 0 && b < 0) || (a > 0 && b > 0)){ a = abs(a); b = abs(b); if(a < b){ int temp = a; a = b; b = temp; } for(int i = 0;i < b; i++){ res += a; } } else if(a == 0 || b == 0){ res = 0; } else{ a = abs(a); b = abs(b); res = negate(multiply(a,b)); } return res; } int divide(int a,int b){ if(b == 0) exit(1); int res = 0; if((a > 0 && b > 0) || (a < 0 && b < 0)){ a = abs(a); b = abs(b); int delt = b; while(b <= a){ b += delt; res++; } } else if(a < 0 || b < 0){ a = abs(a); b = abs(b); int delt = b; while(b <= a){ b += delt; res++; } res = negate(res); } return res; } int subtract(int a, int b){ return a + negate(b); } };
/*关键: 1 乘法比较简单,x*y等于将x连续累加y次,但要注意符号问题,若y为负数,最终结果需要取反 减法:x - y = x + (-y),最关键就是要对y取反。 取反的方法是: 如果y>0,那么令d=-1,sum=0, 然后只要y!=0,就领sum += d,然后领 y += d,牛逼,制造 与y不同符号的最基本的数,通过使y变为0,使得另一个数变成-y 同理:y<=0 ,d=1,处理过程同上 除法:涉及到最牛逼的部分: 设a = x/y,那么求a 等同于 x=y*a,即求得这样一个数乘以y使其满足x, 如果使用a从最小的数不断变大,来做,耗时 转换思路 x = a*y,直接累加y如果变为x,则累加的次数等于a 易错:a可能不为整数,则在累加中加上判定条件如果: 累加a次y <= x < 累加(a+1)次的y,则a为所求 */ class AddSubstitution { public: int invertNumber(int a) //整数取反 { int d = a > 0 ? -1 : 1;//牛逼,构造与待取反整数相反符号的数,通过累加1或-1,构造取反的整数 int invertNum = 0; while( 0 != a) { invertNum += d; a += d; } return invertNum; } int minus(int a, int b) //减法 { b = invertNumber(b); return (a + b); } int multiply(int a, int b) //乘法 { //需要将较大的数作为a,使b变为较小数,作为累加次数,使得累加次数减少 if(a < b) { int temp = a; a = b; b = temp; } //累加 abs(b)次 int sum = 0; for(int i = 0 ; i < abs(b) ; i++ ) { sum += a; } //易错,如果b是负数,需要对结果取反 if(b < 0) { sum = invertNumber(sum); } return sum; } int divide(int a, int b) //除法 { //先确定a和b是否符号相同,符号不同,记录符号为-1,同时令a和b都为绝对值 int result = multiply(a , b); int symbol = result > 0 ? 1 : (-1); int tempA = abs(a); int tempB = abs(b); int count = 0; while( tempB <= tempA ) { tempB += abs(b); count++; } //易错,符号不同,需要取反 if(symbol == -1) { count = invertNumber(count); } return count; } int calc(int a, int b, int type) { // write code here int res; if(type==-1){ res = minus(a , b) } else if(type == 1){ res = multiply(a , b); } else{ res = divide(a , b); } return res; } };
class AddSubstitution { public: int calc(int a, int b, int type) { // write code here if(type==1){ int t = a; for(int i=1;i<b;i++)a += t; } if(type==0){ int t = 0; int temp = b; for(;;t++){ if(a<b)break; b += temp; } a = t; } if(type==-1){ if(a>=b){ for(int i=0;;i++){ if(b+i==a){ a = i; break; } } }else { for(int i=0;;i--){ if(b+i==a){ a = i; break; } } } } return a; } };
import java.util.*; public class AddSubstitution { public int calc(int a, int b, int type) { int result = 0; switch (type) { case 1: for (int i = 0; i < b; i++) result += a; break; case -1: result = a + ~b + 1; break; case 0: while (a >= b) { result++; a = a + ~b + 1; } break; } return result; } }
# -*- coding:utf-8 -*- class AddSubstitution: def calc(self, a, b, t): if t == -1: return self.minus(a, b) elif t == 1: return self.multiply(a, b) else: return self.divide(a, b) def minus(self, a, b): return self.negate(b) + a def multiply(self, a, b): result = sum([a for i in range(0, abs(b))]) if b < 0: result = self.negate(result) return result def divide(self, a, b): # a = xb 同乘法类似 absa = abs(a) absb = abs(b) x = 0 pro = 0 while pro + absb <= absa: pro += absb x += 1 if (a > 0 and b >0) or (a < 0 and b < 0): return x else: return self.negate(x) def negate(self, x): neg = 0 d = 1 if x < 0 else -1 while x != 0 : neg += d x += d return neg
public int calc(int a, int b, int type) { if (type == 1) { return mul(a, b); } else if (type == -1) { return min(a, b); } else return div(a, b); } int div(int a, int b) { boolean flag = (a<0&&b<0)||(a>0&&b>0); if (a < 0) { a = neg(a); } if (b < 0) { b = neg(b); } int i = 0, x = b; for (; x <= a; i++) { x+=b; } return flag?i:neg(i); } int min(int a, int b) { return a + neg(b); } int neg(int x) { int i = x < 0 ? 1 : -1; int k = 0; for (; x != 0; k += i, x += i) ; return k; } int mul(int a, int b) { boolean flag = (a<0&&b<0)||(a>0&&b>0); if (a < 0) { a = neg(a); } if (b < 0) { b = neg(b); } if (a < b) { int k = mul(b,a); return flag?k:neg(k); } int x = 0; for (int i = 0; i < b; i++) { x += a; } return flag?x:neg(x); }
public int calc(int a, int b, int type) {
int result = 0; // 乘 if ( type==1 ) { for ( int i=0; i<b; i++ ){ result += a; } } // 除 else if ( type==0 ) { int i = 0; while ( !(i*b<=a && (i+1)*b>a) ) { i++; } result = i; } // 减 else { result = a + (-b); } return result; }
import java.util.*; /* 思路:乘法就是a+b个a,除法就是n个b相加之后恰好大于a时n的值,减法就是循环找到一个值使其与b相加等于a 但是减法这里用到了-号,可是我觉得用补码的方式也不符合,因此随便用了 */ public class AddSubstitution { public int calc(int a, int b, int type) { if(type ==1){//乘法 int product=0; while(b>0){ product+=a; b--; } return product; } if(type ==0){//除法 int sum =0; int n=0; while(sum<=a){ sum+=b; n++; } return n-1; } if(type ==-1){//减法 int count=0; boolean flag=true; if(a<b){ int temp=a; a=b; b=temp; flag =false; } while(count+b<a){ count++; } if(!flag)return -count; return count; } return 0; } } 运行时间:83ms 占用内存:8904k
class AddSubstitution { public: int calc(int a, int b, int type) { // write code here int res = 0; int bb = b; if(type == 1){ while(b){ res += a; b--; } }else if(type == 0){ if(a < b){ res = 0; }else{ while(a > b){ b += bb; res++; } } }else{ b = ~b + 1; res = a + b; } return res; } };
class AddSubstitution { public: int calc(int a, int b, int type) { // write code here int result = 0; switch(type) { case 1: result = multiply(a, b); break; case 0: result = divide(a, b); break; case -1: result = minus(a, b); break; default: result = a + b; break; } return result; } int invertNumber(int a) { int d = a > 0 ? -1 : 1;//牛逼,构造与待取反整数相反符号的数,通过累加1或-1,构造取反的整数 int invertNum = 0; while( 0 != a) { invertNum += d; a += d; } return invertNum; } int minus(int a, int b) { b = invertNumber(b); return (a + b); } int multiply(int a, int b) { //需要将较大的数作为a,使b变为较小数,作为累加次数,使得累加次数减少 if(a < b) { int temp = a; a = b; b = temp; } //累加 abs(b)次 int sum = 0; for(int i = 0 ; i < abs(b) ; i++ ) { sum += a; } //易错,如果b是负数,需要对结果取反 if(b < 0) { sum = invertNumber(sum); } return sum; } int divide(int a, int b) { //先确定a和b是否符号相同,符号不同,记录符号为-1,同时令a和b都为绝对值 int result = multiply(a , b); int symbol = result > 0 ? 1 : (-1); int tempA = abs(a); int tempB = abs(b); int count = 0; while( tempB <= tempA ) { tempB += abs(b); count++; } //易错,符号不同,需要取反 if(symbol == -1) { count = invertNumber(count); } return count; } };
class AddSubstitution { public: int calc(int a, int b, int type) { if(type == 1){ int ans = 0; for(int i = 0; i < b; i++) ans += a; return ans; } if(type == 0){ int count = 0, ans = 0; if(a == b) return 1; else if(a > b){ while(ans < a){ ans += b; count++; } return count - 1; } else return 0; } if(type == -1){ if(a == b) return 0; else if(a > b){ for(int i = 1; ; i++) if(b + i == a) return i; } else{ for(int i = 1; ; i++) if(a + i == b) return -i; } } } };
public class AddSubstitution { public int calc(int a, int b, int type) { // write code here if(type==0){ return divid(a,b); }else if(type==1){ return mul(a,b); }else{ return minus(a,b); } } private int mul(int a,int b){ int res=0; while(b>0){ if((b&1)==1){ res+=a; } b>>=1; a+=a; } return res; } private int minus(int a,int b){ return a+(b^0xffffffff)+1; } private int divid(int a,int b){ int res=-1; while(a>=0){ a=minus(a,b); res++; } return res; } }
/* 测试用例: 减法:将b取反相加 大减小 小减大 正减负可能会溢出,给个警告 2147483646 - (-2) 乘法(b个a相加): b为负数,0 除法: 先确定结果符号,然后全转为正数,判定a>b?ans+符号:over 0 -2 -5 2 5 5 -2 */ int calc(int a, int b, int type) { // write code here if(type==-1){ //-b 是将b按位取反再加1 //可能溢出 if(b<0){ b = ~b+1; if(a>0 and INT_MAX-a<b) cout<<"warning! this op result will be overflowed"<<endl; } else b = ~b+1; return a+b; } if(type==1){ //b个a相加,b为负时将a,b同时取反 if(b<0){ a=~a+1; b=~b+1; } int ans = 0; for(;b>0;b--){ ans+=a; if(ans>0 and INT_MAX-ans<a) cout<<"warning! this op result will be overflowed"<<endl; if(ans<0 and INT_MIN-a<ans) cout<<"warning! this op result will be overflowed"<<endl; } return ans; } if(type==0){ if(b==0) { cout<<"divided zero error"<<endl; return -1; } // -5 2 int token = -1;//结果的符号 if(a<0 and b<0){ a=~a+1; b=~b+1; } if(a>0 and b>0) token = 1; else{ if(a<0) a=~a+1; if(b<0) b=~b+1; } int ans = 0; //a>b就减去b while(a>b){ ans+=token; a = calc(a,b,-1); } return ans; } }
//这题目有什么意义? class AddSubstitution { public: int calc(int a, int b, int type) { int ans; if(type==1) { ans=max(a,b); for(int i=1;i<min(a,b);++i) ans+=max(a,b); return a==0||b==0?0:ans; } if(type==0) { ans=0; int sum=0; while(sum<a) { sum+=b; ++ans; } return a>b?ans-1:0; } if(type==-1) { ans=0; if(a>b) while(ans+b<a) ++ans; else while(ans+a<b) ++ans; return a>b?ans:-ans; } return -1; } };