+ 1
[Solved] Balconies C++
Hello, can you help me please with the Balconies problem? I got stuck :/ cases 3,4,5 fail. This is my code: Thanks! #include <iostream> using namespace std; int main() { int h,w,A; int h1,w1,B; cin>>h>>w; cin>>h1>>w1; A=h*w; B=h1*w1; if (A>B) { cout<<"Apartment A"; } else { cout<<"Apartment B"; } return 0; }
10 ответов
+ 1
Try changing int into long long. There may be a case where the multiplication of 2 integers causes overflow.
edit: or double
+ 9
Try this
cin»h; getchar();
cin»w;
do the same for the other one
+ 8
It will be better if you will send question link we will check it . your code have no errors but without link we cannot test your code
+ 1
I don’t know about the question but there are some ways your code can go wrong:
1. What happens if A = B? you don’t have that condition.
2. What if (h) and (w) are not integers?
+ 1
[Solved] Thanks for help!
0
I also tried the case where both apartments have the same (h*w), but still don't work
0
You are trying to determine which of two apartments has a larger balcony. Both balconies are rectangles, and you have the length and width, but you need the area.
Task
Evaluate the area of two different balconies and determine which one is bigger.
Input Format
Your inputs are two strings where the measurements for height and width are separated by a comma. The first one represents apartment A, the second represents apartment B.
Output Format:
A string that says whether apartment A or apartment B has a larger balcony.
Sample Input
'5,5'
'2,10'
Sample Output
Apartment A
0
[Solved] Balconies JAVA, using some Regex : enjoy
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String txt1 = scan.nextLine();
String txt2 = scan.nextLine();
int nbre1 = 1;
int nbre2 = 1;
for (String i: txt1.split(",")){
String t = i.replaceAll("[^0-9]","");
int apt1 = Integer.parseInt(t);
nbre1 = nbre1*apt1;
}
for (String j: txt2.split(",")){
String t = j.replaceAll("[^0-9]","");
int apt2 = Integer.parseInt(t);
nbre2 = nbre2*apt2;
}
if(nbre1>nbre2){
System.out.println("Apartment A");
}else{
System.out.println("Apartment B");
}
}
}
0
#include <iostream>
using namespace std;
int main() {
string A,a1,a2;
cin>>A;
int t,e;
t=A.find(",");
for(int i=0;i<t;i++){
a1+=A[i] ;
}
for(int i=t;i<size(A);i++){
a2+=A[i+1];
}
int h=stoi(a1);
int w=stoi(a2);
int A2=h*w;
string B,b1,b2;
cin>>B;
int t1,e1;
t1=B.find(",");
for(int i=0;i<t1;i++){
b1+=B[i] ;
}
for(int i=t1;i<size(B);i++){
b2+=B[i+1];
}
int h1=stoi(b1);
int w1=stoi(b2);
int A3=h1*w1;
if(A2>A3){
cout<<"Apartment A";
}
else{
cout<<"Apartment B";
}
return 0;
}
- 1
if you could post the question, that would be great