10494 - If We Were a Child Again

All about problems in Volume 104. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Laevatein
New poster
Posts: 2
Joined: Wed Feb 13, 2013 3:40 am

Re: 10494 - If We Were a Child Again

Post by Laevatein » Wed Feb 13, 2013 3:43 am

can anyone help me? i don't have any idea why my code is wrong

Code: Select all

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

string bagi(string a, long long b)
{
	long long nilai = 0;
	string  hasil;
	bool belum = true;
	for (long i = 0; i < a.length(); i++)
	{
		nilai *= 10;
		nilai += a[i]-'0';
		if (belum)
		{
			if (nilai/b)
			{
				belum = false;
				hasil.push_back(('0' + nilai/b));	
			}
		}	
		else
			hasil.push_back(('0' + nilai/b));
		nilai %= b;	
	}
	if (belum) return "0";
	return hasil;
}

long long mod(string a, long long b)
{
	long long nilai = 0;
	bool belum = true;
	for (long i = 0; i < a.length(); i++)
	{
		nilai *= 10;
		nilai += a[i]-'0';
		nilai %= b;
	}
	return nilai;
}

int main()
{
	long long b;
	string a;
	char c;
	bool sudah = false;
	while (cin >> a)
	{
		if(sudah) printf("\n");
		else sudah = true;
		scanf(" %c %lld",&c,&b);
		if (c == '/')
			cout << bagi(a,b);
		else
			cout << mod(a,b);
		
	}
	return 0;	
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10494 - If We Were a Child Again

Post by brianfry713 » Thu Feb 14, 2013 12:22 am

Print a newline at the end of the last line.
Check input and AC output for thousands of problems on uDebug!

hello
New poster
Posts: 25
Joined: Sun Mar 10, 2013 7:29 pm

Re: 10494 - If We Were a Child Again

Post by hello » Thu Apr 04, 2013 10:02 am

Code: Select all

#include<stdio.h>
#include <iostream>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<vector>
#include<string.h>


using namespace std;

int main()
{

   char ch;
   unsigned long int rem;
   char str1[1000];
   unsigned long int num;
   while(scanf("%s %c %lu",str1,&ch,&num)==3){
    if(ch=='%'){
             rem=0;
            for(int i=0;str1[i];i++){
                rem=rem*10+(str1[i]-'0');
                     rem=rem%num;
                }
                printf("%d\n",rem);
            }
    if(ch=='/'){
            rem=0;
            int flag=0;
    int dev;
        for(int i=0;str1[i];i++)
                  {
                    rem=rem*10+(str1[i]-48);
                    dev=rem/num;
                    if(flag==0 && dev>0)flag=1;
                    if(flag==1)printf("%d",dev);
                    rem=rem%num;
                  }
                  printf("\n");


    }

    }
return 0;


   }


got a WA why.......?

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10494 - If We Were a Child Again

Post by brianfry713 » Thu Apr 04, 2013 10:58 pm

Input:

Code: Select all

99999999999999999999999999999999999999999999999999999999999999999999999999999 % 2147483647
99999999999999999999999999999999999999999999999999999999999999999999999999999 / 2147483647
Correct output:

Code: Select all

450864903
46566128752457969241057508271679984532147638747537340385623900399368
Check input and AC output for thousands of problems on uDebug!

User avatar
uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 10494 - If We Were a Child Again

Post by uDebug » Mon Feb 24, 2014 2:48 pm

brianfry713 wrote:Input: Correct output:
Thanks for sharing this test case.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

Shahidul.CSE
Experienced poster
Posts: 148
Joined: Sun Jul 13, 2014 4:32 am
Location: Rangpur, Bangladesh

Re: 10494 - If We Were a Child Again

Post by Shahidul.CSE » Fri Aug 29, 2014 8:05 pm

Code: Select all

Removed after getting AC
Last edited by Shahidul.CSE on Mon Sep 01, 2014 5:56 am, edited 2 times in total.
Md. Shahidul Islam
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10494 - If We Were a Child Again

Post by lighted » Sun Aug 31, 2014 5:53 pm

Value of r is overflowing long type.

Code: Select all

 r = r * 10 + n[i] - '0';
 res[i] = r / d + '0';
 r = r % d;
When d = 2147483647. After r = r % d opeation r can get value between 214748364..2147483646 and in the next iteration of for loop r = r * 10 + n - '0' value of r will be greater than 2147483647.
So use long long for r.
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

Shahidul.CSE
Experienced poster
Posts: 148
Joined: Sun Jul 13, 2014 4:32 am
Location: Rangpur, Bangladesh

Re: 10494 - If We Were a Child Again

Post by Shahidul.CSE » Sun Aug 31, 2014 6:28 pm

Removed after getting AC :D :D
Last edited by Shahidul.CSE on Mon Sep 01, 2014 5:53 am, edited 2 times in total.
Md. Shahidul Islam
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10494 - If We Were a Child Again

Post by lighted » Sun Aug 31, 2014 7:43 pm

I don't know what the problem is but changing printing to puts gives acc. :-? :)

Code: Select all

if(sign=='/')
{
    puts(result);           
}
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

ehsanulbigboss
New poster
Posts: 32
Joined: Tue Jul 22, 2014 1:17 am

10494 - If We Were a Child Again

Post by ehsanulbigboss » Fri Oct 03, 2014 10:57 pm

GOT AC!
Last edited by ehsanulbigboss on Sat Oct 04, 2014 8:56 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10494 - If We Were a Child Again

Post by brianfry713 » Sat Oct 04, 2014 12:52 am

Next time post in the existing thread.
Don't make a function return a pointer to a local variable. In g++ that gives me a warning. Either print ans in your division function or make ans global.
Check input and AC output for thousands of problems on uDebug!

Post Reply

Return to “Volume 104 (10400-10499)”