Hirdetés
- AMD Navi Radeon™ RX 9xxx sorozat
- Azonnali fotós kérdések órája
- Vezeték nélküli fülhallgatók
- Bemutatta saját SSD-jét a Raspberry Pi
- Milyen HASZNÁLT notebookot vegyek?
- Intel Core i5 / i7 / i9 "Alder Lake-Raptor Lake/Refresh" (LGA1700)
- Vezetékes FEJhallgatók
- Steam Deck
- Apple MacBook
- AMD Ryzen 9 / 7 / 5 9***(X) "Zen 5" (AM5)
Új hozzászólás Aktív témák
-
blaces
tag
Hello!
Ismét egy feladathoz kéne segítség.
A feladat két hosszú egész szám összeszorzása (olyannyira hosszú, hogy a long double-kbe se ment bele, ezért stringeztem) 3 napig tartott az összehegesztése...Feladat: Írj programot, amely két hosszú egész számot szoroz össze! Csak pozitív számok fordulnak elő a bemeneten, viszont ezek tetszőlegesen sok 0-val kezdődhetnek. Egy szám legfeljebb 100 számjegyből áll. A két szám két egymást követő sorban helyezkedik el. A program kimenetén nem lehetnek vezető nullák!
Bemenet: Kimenet:
2. eset:
0000789675 19375465800
24536
3. eset:
0000000000000000000000000000000000000000000000000000 0.
35363567#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUMBER 200
int toInt(char ch) {
return (ch - '0');
}
char toChar(int num) {
return (num + '0');
}
void eraseNewLine(char* str) {
size_t len;
len = strlen(str);
if (len > 0 && str[len-1] == '\n') {
str[len-1] = '\0';
}
}
void eraseNulls(char* str) {
unsigned int count = 0;
size_t i = 0;
size_t len;
char c;
len = strlen(str);
c = str[0];
while (c != '\0') {
c = str[i];
i++;
if (c == '0') {
count++;
} else {
break;
}
}
if (count == len) {
str[0] = '0';
str[1] = '\0';
} else if (count > 0) {
memcpy(str, str+count, (len-count+1));
}
}
void addNulls(int num, char* solution) {
size_t len;
unsigned int i;
len = strlen(solution);
if (num > 0) {
for (i = 0; i < num; i++) {
solution[len+i] = '0';
}
solution[i+len] = '\0';
}
}
void changeStr(char* s1, char* s2) {
char temp[2*MAX_NUMBER];
size_t l1;
size_t l2;
l1 = strlen(s1);
l2 = strlen(s2);
memcpy(temp, s1, l1+1);
memcpy(s1, s2, l2+1);
memcpy(s2, temp, l1+1);
}
void multiplyWithOneChar(char x, char* y, char* solution) {
unsigned int xv = toInt(x);
unsigned int yi;
unsigned int tempi;
unsigned int carry = 0;
char temp[2*MAX_NUMBER];
unsigned int i;
size_t len;
len = strlen(y);
for (i = 0; i < len; i++) {
yi = toInt(y[len-i-1]);
tempi = yi * xv + carry;
if (tempi > 9) {
carry = tempi / 10;
yi = tempi % 10;
} else {
carry = 0;
yi = tempi;
}
temp[i] = toChar(yi);
}
if (carry > 0) {
temp[i] = toChar(carry);
temp[i+1] = '\0';
} else {
temp[i] = '\0';
}
len = strlen(temp);
for (i = 0; i < len; i++) {
solution[i] = temp[len-i-1];
}
solution[i] = '\0';
}
void add(char* y, char* solution) { // solution = solution + y;
unsigned int yi;
unsigned int si;
unsigned int tempi;
unsigned int carry = 0;
char temp[2*MAX_NUMBER];
unsigned int i;
size_t len;
size_t lens;
len = strlen(y);
lens = strlen(solution);
if (lens > len) {
changeStr(y, solution);
len = strlen(y);
lens = strlen(solution);
}
for (i = 0; i < len; i++) {
yi = toInt(y[len-i-1]);
if (i < lens) {
si = toInt(solution[lens-i-1]);
} else {
si = 0;
}
tempi = yi + si + carry;
if (tempi > 9) {
carry = tempi / 10;
yi = tempi % 10;
} else {
carry = 0;
yi = tempi;
}
temp[i] = toChar(yi);
}
if (carry > 0) {
temp[i] = toChar(carry);
temp[i+1] = '\0';
} else {
temp[i] = '\0';
}
len = strlen(temp);
for (i = 0; i < len; i++) {
solution[i] = temp[len-i-1];
}
solution[i] = '\0';
}
void multiply(char* x, char* y, char* solution) {
size_t len;
unsigned int i;
char temp[2*MAX_NUMBER];
eraseNewLine(x);
eraseNewLine(y);
eraseNulls(x);
eraseNulls(y);
solution[0] = '0';
solution[1] = '\0';
len = strlen(y);
for (i = 0; i < len; i++) {
multiplyWithOneChar(y[len-i-1], x, temp);
addNulls(i, temp);
add(temp, solution);
}
}
int main() {
char first[MAX_NUMBER];
char second[MAX_NUMBER];
char solution[2*MAX_NUMBER];
fgets(first, MAX_NUMBER, stdin);
fgets(second, MAX_NUMBER, stdin);
multiply(first, second, solution);
printf("%s\n", solution);
return 0;
}és a második esett az ahol, a második számjegyig végig adja ki a nullákat, az elsőre(ezt nem írtam fentebb) és másodikra jó!
Új hozzászólás Aktív témák
● olvasd el a téma összefoglalót!
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- BESZÁMÍTÁS! ASUS PRIME Z790M i9 14900K 32GB DDR5 1TB SSD RTX 3080 TI 12GB Zalman Z1 Plus EVGA 850W
- LG 32GS95UE - 32" OLED / UHD 4K / 240Hz - 480Hz & 0.03ms / 1300 Nits / NVIDIA G-Sync / AMD FreeSync
- ÁRGARANCIA!Épített KomPhone i5 13400F 16/32/64GB RAM RTX 3060 12GB GAMER PC termékbeszámítással
- LG 27UN83A-W - IPS LED - 3840x2160 4K - 60Hz 5ms - USB Type-C - HDR 400 - AMD FreeSync - Hangszórók
- 8 GB-os GeForce RTX 2060 SUPER (OEM HP) - garanciával
Állásajánlatok
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest
Cég: CAMERA-PRO Hungary Kft.
Város: Budapest