10035 - Primary Arithmetic
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10035 - Primary Arithmetic
Try input 1 0
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 5
- Joined: Thu Oct 16, 2014 8:15 am
Re: 10035
why WA?
Code: Select all
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
long long int a,b;
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
while(scanf("%lld %lld\n",&a,&b))
{
if(a==0 && b==0) return 0;
int m,n;
int count=0;
int p=0;
while(a||b)
{
m=a%10;
n=b%10;
a=a/10;
b=b/10;
if(m+n+p>9)
{
count++;
p=(m+n+p)/10;
}
}
if(count==0) printf("No carry operation.\n");
if(count==1) printf("%d carry operation.\n",count);
if(count>1) printf("%d carry operations.\n",count);
}
return 0;
}
Last edited by brianfry713 on Fri Oct 17, 2014 10:16 pm, edited 1 time in total.
Reason: Added code blocks
Reason: Added code blocks
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 10035 - Primary Arithmetic
Don't read and write to files.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 5
- Joined: Thu Oct 16, 2014 8:15 am
Re: 10035 - Primary Arithmetic
why wa?checked a lot.someone help plz....
Code: Select all
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
long long int a,b;
while(scanf("%lld %lld\n",&a,&b))
{
if(a==0 && b==0) return 0;
int m,n;
int count=0;
int p=0;
while(a||b)
{
m=a%10;
n=b%10;
a=a/10;
b=b/10;
if(m+n+p>9)
{
count++;
p=(m+n+p)/10;
}
}
if(count==0) printf("No carry operation.\n");
if(count==1) printf("%d carry operation.\n",count);
if(count>1) printf("%d carry operations.\n",count);
}
return 0;
}
Last edited by brianfry713 on Tue Oct 21, 2014 7:36 pm, edited 1 time in total.
Reason: Added code blocks
Reason: Added code blocks
Re: 10035 - Primary Arithmetic
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
-
- New poster
- Posts: 15
- Joined: Tue Oct 21, 2014 4:08 pm
- Location: Bangladesh
- Contact:
Re: 10035 - Primary Arithmetic
removed after ac
-
- New poster
- Posts: 6
- Joined: Thu Dec 04, 2014 11:40 pm
Re: 10035 - Primary Arithmetic
Why wrong answer..?
Code: Select all
#include<stdio.h>
int main()
{
unsigned int a,b;
for(;;)
{
scanf("%u %u",&a,&b);
if(a==0&&b==0) return 0;
if(a<b)
{
int temp=a;
a=b;
b=temp;
}
int i,j,k,x[10],y[10],p;
for(i=0; a>0; i++)
{
p=a/10;
x[i]=a%10;
a=p;
}
for(j=0; b>0; j++)
{
p=b/10;
y[j]=b%10;
b=p;
}
i--;
j--;
if(j!=i)
{
int s;
for(s=i; s>j; s--)
{
y[s]=0;
}
}
int count=0;
for(k=0; k<=i; k++)
{
int sum=x[k]+y[k];
if(sum>=10)
{
count++;
x[k+1] =x[k+1]+1;
}
}
if(count==0) printf("No carry operation.\n");
else printf("%d carry operations.\n",count);
}
return 0;
}
Re: 10035 - Primary Arithmetic
Try to copy/paste output format from problem description or sample.
Your OutputSample Output
Your Output
Code: Select all
1 carry operations.
Code: Select all
1 carry operation.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Re: 10035 - Primary Arithmetic
¿Can someone help me? I checked everything and I don't find a mistake. I have to say that I am learning C++.
Thanks.
Thanks.
Code: Select all
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
ifstream in ("input");
ofstream out ("output");
string dato1, dato2;
int long1, long2, carry, index1, index2, aux1, aux2;
bool flag;
cin >> dato1 >> dato2;
if (dato1== "0" && dato2 == "0") return 0;
while (dato1 != "0" || dato2 != "0") {
carry = 0;
flag = false;
long1 = dato1.length() - 1;
long2 = dato2.length() - 1;
while (long1 > -1 && long2 > -1) {
aux1 = dato1.at(long1) - 48;
aux2 = dato2.at(long2) - 48;
if (flag) aux1++;
if (aux1 + aux2 >= 10) {
carry++;
flag = true;
} else flag = false;
long1--;
long2--;
}
while (long1 > -1 && flag) {
if (dato1.at(long1) - 47 == 10) carry++;
long1--;
}
while (long2 > -1) {
if (dato2.at(long2) - 47 == 10) carry++;
long2--;
}
if (carry == 0) cout << "No carry operation.";
else if (carry == 1) cout << "1 carry operation.";
else cout << carry << " carry operations.";
cout << "\n";
cin >> dato1 >> dato2;
}
return 0;
}
Re: 10035 - Primary Arithmetic
Why WA ????
Code: Select all
#include <stdio.h>
int main()
{
long long int a, b;
int flag=0;
while(scanf("%lld %lld", &a, &b) && a>0 || b>0)
{
if(a < b)
{
int swap = a;
a = b;
b = swap;
}
for(int i=1;; i++)
{
if(a==0 && b==0)
break;
int rem1, rem2;
rem1 = a%10;
rem2 = b%10;
a = a/10;
b = b/10;
if(rem1+rem2 >9)
{
flag++;
b++;
int rem3 = b%10;
if(rem3==0)
b--;
}
}
if(flag == 0)
printf("No carry operation.\n");
else if(flag==1)
printf("1 carry operation.\n");
else
printf("%d carry operations.\n",flag);
flag = 0;
}
}
Re: 10035 - Primary Arithmetic
HELP!!why WA?I have already tried many cases and the answer is correct.
Code: Select all
#include <stdio.h>
int main(void)
{
unsigned int a, b;
int carry, c, mod1, mod2;
while(1)
{
scanf("%u %u",&a, &b);
carry = 0;
if(a == 0 && b == 0)
{
break;
}
if(b > a)
{
a = a^b;
b = a^b;
a = a^b;
}
while(a > 0)
{
mod1 = a%10;
mod2 = b%10;
a /= 10;
b /= 10;
c = mod1 + mod2;
if(c > 9)
{
carry++;
a += (c/10);
}
}
if(!carry)
printf("No carry operation.\n");
else if(carry == 1)
printf("%d carry operation.\n", carry);
else
printf("%d carry operations.\n", carry);
}
return 0;
}
Re: 10035 - Primary Arithmetic
Try this one "9998877 9998"joy398 wrote:HELP!!why WA?I have already tried many cases and the answer is correct.Code: Select all
#include <stdio.h> int main(void) { unsigned int a, b; int carry, c, mod1, mod2; while(1) { scanf("%u %u",&a, &b); carry = 0; if(a == 0 && b == 0) { break; } if(b > a) { a = a^b; b = a^b; a = a^b; } while(a > 0) { mod1 = a%10; mod2 = b%10; a /= 10; b /= 10; c = mod1 + mod2; if(c > 9) { carry++; a += (c/10); } } if(!carry) printf("No carry operation.\n"); else if(carry == 1) printf("%d carry operation.\n", carry); else printf("%d carry operations.\n", carry); } return 0; }
should return 7 carry operations.
Re: 10035 - Primary Arithmetic
Why WA I'm very confused
please help me
please help me
Code: Select all
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<utility>
#include<vector>
//#include<fstream>
using namespace std;
int xi[10];
int yi[10];
vector<char>xc;
vector<char>yc;
int main(){
//ofstream o;
//o.open("tst1.txt");
long long a=1, b=1, c,f,g,z=10,p,spl,trigss=3;
bool gee = false;
char m[1], n[1];
string d, e;
cin >> a;
cin >> b;
while (a != 0 || b != 0) {
z = 10;trigss = 3;
gee = false;
c = 0;p = 0;c = 0;
d = to_string(a);
e = to_string(b);
f = d.length();
g = e.length();
if (z > f) {
z = f;
trigss = 0;
}
if (z > g) {
z = g;
trigss = 1;
}
for (int i = 0; i < f; i++) {
xc.push_back(d.at(i));
}
for (int i = 0; i < g; i++) {
yc.push_back(e.at(i));
}
reverse(xc.begin(), xc.end());
reverse(yc.begin(), yc.end());
for (int i = 0; i < z; i++)
{
m[0] = xc[i];
n[0] = yc[i];
if (gee) {
spl = atol(m) + atol(n) + 1;
gee = false;
}
else {
spl = atol(m) + atol(n);
}
if (spl >= 10) {
gee = true;
c++;
}
}
if (gee) {
if (trigss == 0) {
while (z < g) {
n[0] = yc[z];
if (atol(n) + 1 >= 10) {
c++;
}
else {
break;
}
z++;
}
}
else
{
while (z < f) {
m[0] = xc[z];
if (atol(m) + 1 >= 10) {
c++;
}
else
{
break;
}
z++;
}
}
gee = false;
}
if (c == 0) {
cout << "No carry operation." << endl;
//o << "No carry operation." << endl;
}
else if (c == 1) {
cout << c << " carry operation." << endl;
//o << c << " carry operation." << endl;
}
else
{
cout << c << " carry operations." << endl;
//o << c << " carry operations." << endl;
}
xc.clear();
yc.clear();
cin >> a >> b;
}
return 0;
}
Re: 10035 - Primary Arithmetic
Input
Correct output
Try to check input in this thread and on uDebug before posting. https://www.udebug.com/UVa/10035
Code: Select all
1 1
0 0
Code: Select all
No carry operation.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman