Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Naloga2 {
- public static void main(String[] args) {
- String a = "123";
- String b = "456";
- // os(a, b);
- System.out.println(dv((a), (b)));
- // System.out.println(stOut("0"));
- }
- static void os(String a, String b) {
- String result = "";
- for (int i = 0; i < b.length(); i++) {
- String s = "";
- int c = 0;
- int v1 = b.charAt(i) - '0';
- for (int j = a.length() - 1; j >= 0; j--) {
- int v2 = a.charAt(j) - '0';
- int product = v1 * v2 + c;
- s = (j > 0) ? (product) % 10 + s : product + s;
- c = product / 10;
- }
- System.out.println((s.charAt(0) == '0') ? 0 : s);
- if (i == 0) {
- result = s;
- } else {
- result += 0;
- while (s.length() != result.length()) {
- s = 0 + s;
- }
- result = sumStr(result, s);
- }
- }
- for (int i = 0; i < result.length(); i++) {
- System.out.print("-");
- }
- System.out.println();
- System.out.println(result);
- }
- static String sumStr(String a, String b) {
- String result = "";
- int c = 0;
- if (a.length() > b.length()) {
- while (a.length() != b.length()) {
- b = 0 + b;
- }
- } else if (b.length() > a.length()) {
- while (a.length() != b.length()) {
- a = 0 + a;
- }
- }
- for (int i = a.length() - 1; i >= 0; i--) {
- int v1 = b.charAt(i) - '0';
- int v2 = a.charAt(i) - '0';
- int sum = v1 + v2 + c;
- int x = (i > 0) ? sum % 10 : sum;
- result = x + result;
- c = sum / 10;
- }
- return result;
- }
- static String [] sodaDolzinaEnaka(String a, String b) {
- while(a.length()%2!=0){
- a=0+a;
- }
- while(b.length()%2!=0){
- b=0+b;
- }
- if (a.length() > b.length()) {
- while (a.length() != b.length()) {
- b = 0 + b;
- }
- } else if (b.length() > a.length()) {
- while (a.length() != b.length()) {
- a = 0 + a;
- }
- }
- return new String[]{a,b};
- }
- static String stOut(String s) {
- int i = 0;
- if (isZero(s)) {
- return "0";
- }
- while (i < s.length() && s.charAt(i) == '0') {
- i++;
- }
- return s.substring(i, s.length());
- }
- static boolean isZero(String str) {
- for (int i = 0; i < str.length(); i++) {
- if (str.charAt(i) != '0')
- return false;
- }
- return true;
- }
- static int numZer(String s) {
- int c = 0;
- int i = 0;
- while(i<s.length() && s.charAt(i)=='0') {
- c++;
- i++;
- }
- return c;
- }
- static String dv(String a, String b) {
- System.out.println(a + " " + b);
- if (isZero(a) || isZero(b)) {
- return "0";
- }
- if (stOut(a).length() == 1 || stOut(b).length() == 1) {
- return linMnoz(a, b);
- }
- String arr[] = sodaDolzinaEnaka(a, b);
- String tmpa = arr[0];
- String tmpb = arr[1];
- String a0 = stOut(tmpa.substring(tmpa.length() / 2, tmpa.length()));
- String b0 = stOut(tmpb.substring(tmpb.length() / 2, tmpb.length()));
- String a1 = stOut(tmpa.substring(0, tmpa.length() / 2));
- String b1 = stOut(tmpb.substring(0, tmpb.length() / 2));
- String a0b0 = dv(a0, b0);
- System.out.println(a0b0);
- String a0b1 = dv(a0, b1);
- System.out.println(stOut(a0b1));
- String a1b0 = dv(a1, b0);
- System.out.println(stOut(a1b0));
- String a1b1 = dv(a1, b1);
- System.out.println(stOut(a1b1));
- String str = tmpa + tmpb;
- int n = str.length() / 2;
- for (int i = 0; i < n; i++) {
- a1b1 += 0;
- }
- String sum = sumStr(a0b1, a1b0);
- for (int i = 0; i < n / 2; i++) {
- sum += 0;
- }
- String r1 = sumStr(a1b1, sum);
- String r2 = sumStr(r1, a0b0);
- return stOut(r2);
- }
- static String linMnoz(String a, String b) {
- String result = "";
- for (int i = 0; i < b.length(); i++) {
- String s = "";
- int c = 0;
- int v1 = b.charAt(i) - '0';
- for (int j = a.length() - 1; j >= 0; j--) {
- int v2 = a.charAt(j) - '0';
- int product = v1 * v2 + c;
- s = (j > 0) ? (product) % 10 + s : product + s;
- c = product / 10;
- }
- if (i == 0) {
- result = s;
- } else {
- result += 0;
- while (s.length() != result.length()) {
- s = 0 + s;
- }
- result = sumStr(result, s);
- }
- }
- return result;
- }
- static String ka(String a, String b) {
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement