4 条题解

  • 1
    @ 2023-3-19 17:09:50
    # include <iostream>
    # include <algorithm>
    # include <string>
    
    using namespace std;
    
    int a[1005];
    int b[1005];
    int c[1005];
    
    // 123 + 456  = 579
    // 321 + 654 = 975
    // 5 + 7 
    string Add(string strA, string strB){
    	for(int i = 0; i < strA.length(); i ++)
    		a[i] = strA[strA.length() - 1 - i] - '0';
    	for(int i = 0; i < strB.length(); i ++)
    		b[i] = strB[strB.length() - 1 - i] - '0';
    
    	
    
    	
    	for(int i = 0; i < max(strA.length(), strB.length()); i ++){
    			c[i] += b[i] + a[i];  //求和
    			c[i + 1] += c[i] / 10;
    			c[i] = c[i] % 10;		 
    	}
    	
    	// 去前导0
    	int pos = 0; 
    	for(pos = max(strA.length(), strB.length()) + 1; pos > 0; pos --){
    		if(c[pos] != 0) break;
    	}
    	string ans = "";
    	for(int i = pos; i >=0; i --){
    		ans += char(c[i] + '0');
    	}
    	return ans;
    	
    }
    // A > B   1
    // A = B   0;
    // A < B   -1
    int  Compare(string strA, string strB){
    	
    	if(strA.length() < strB.length()) return -1;
    	if(strA.length() > strB.length()) return 1;
    	for(int i = 0; i < strA.length(); i ++){
    		if(strA[i] - '0' < strB[i] - '0' ) return -1;
    		if(strA[i] - '0' > strB[i] - '0' ) return 1;
    	}
    	return 0;
    }
    
    // a - b
    string Sub(string strA, string strB){
    	
    	bool flag = true;  // 正负号的问题 
    	if(Compare(strA, strB) == -1){
    		swap(strA, strB);
    		flag = false;
    	}
    	
    	// 字符到数字的问题 
    	for(int i = 0; i < strA.length(); i ++)
    		a[i] = strA[strA.length() - 1 - i] - '0';
    	for(int i = 0; i < strB.length(); i ++)
    		b[i] = strB[strB.length() - 1 - i] - '0';
    	
    	// 加减进位的问题 
    	for(int i = 0 ; i < max(strA.length(), strB.length()); i ++){
    		c[i] = a[i] - b[i];
    		if(c[i] < 0){   // 判断是否要借位 
    			c[i] += 10; // 借位后+10 
    			c[i + 1] -= 1; // 减 1 
    		}
    	}
        // 去前导0的问题 
    	int pos = 0; 
    	for(pos = max(strA.length(), strB.length()) + 1; pos > 0; pos --){
    		if(c[pos] != 0) break;
    	}
    	string ans = "";
    	for(int i = pos; i >=0; i --){
    		ans += char(c[i] + '0');
    	}
    	if(flag == false) return "-"+ans;
    	return ans;
    }
    string Multi(string strA, string strB){
    	// 字符到数字的问题 
    	for(int i = 0; i < strA.length(); i ++)
    		a[i] = strA[strA.length() - 1 - i] - '0';
    	for(int i = 0; i < strB.length(); i ++)
    		b[i] = strB[strB.length() - 1 - i] - '0';
    	// 乘法进位的问题
    	
    	for(int i = 0; i < strA.length(); i ++){
    		for(int j = 0; j < strB.length(); j ++){
    			c[i + j] += (a[i] * b[j]);
    			c[i + j + 1] += c[i + j] / 10;
    			c[i + j] %= 10;
    		}
    	}
    	// 去前导0
    	int pos = 0; 
    	for(pos = max(strA.length(), strB.length()) + 1; pos > 0; pos --){
    		if(c[pos] != 0) break;
    	}
    	string ans = "";
    	for(int i = pos; i >=0; i --){
    		ans += char(c[i] + '0');
    	}
    	return ans;	
    }
    // 高精度/低精度 
    string Div(string strA, long long B, long long& Mod){
    	// 字符到数字的问题 
    	for(int i = 0; i < strA.length(); i ++)
    		a[i] = strA[i] - '0';
    	for(int i = 0; i < strA.length(); i ++){
    		Mod = Mod * 10 + a[i];
    		c[i] = Mod / B;
    		Mod %= B;
    	} 
    		// 去前导0
    	int pos = 0; 
    	for(pos = 0; pos< strA.length(); pos ++){
    		if(c[pos] != 0) break;
    	}
    
    	string ans = "";
    	for(int i = pos; i < strA.length(); i ++){
    		ans += char(c[i] + '0');
    	}
    
    	return ans;	
    	
    }
    // 高精度/高精度 
    string Div2(string strA, long long B){
    
    	
    }
    
    
    int main() {
    	
    	string A = "15";
    	long long B = 4;
    	long long Mod = 0;
    	cout <<  Div(A,B, Mod) << endl;
    	cout << Mod << endl;
    
    
    	return 0;
    }
    
    

    信息

    ID
    1553
    时间
    1000ms
    内存
    256MiB
    难度
    6
    标签
    递交数
    146
    已通过
    41
    上传者