This is the easiest problem in the set. This problem can be solved various methods. For instance, if A - B mod 10 = 9, print A - B - 1, otherwise print A - B + 1. Be careful not to print 0 if A - B = 1. Your answer must be a positive integer.
Why this code does not work??
class CielAB
{
public static void main(String args[])
{
Scanner inp=new Scanner(System.in);
String a=inp.nextLine();
String b[]=a.split(" ");
int a1=new Integer(b[0]);
int b1=new Integer(b[1]);
int c1=a1-b1;
if(c1%10==0)
c1=c1+1;
else
c1=c1-1;
System.out.println(c1);
}
}
Following is my code in C#, but it says that answer is Wrong. I don’t understand why. Please Help.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeChef_CEILAB
{
class Program
{
static void Main(string[] args)
{
string s = Console.ReadLine();
string[] s1 = s.Split(' ');
int A = int.Parse(s1[0]);
int B = int.Parse(s1[1]);
//Console.WriteLine("Enter First Number");
//int A = int.Parse(Console.ReadLine());
//Console.WriteLine("Enter Second Number");
//int B = int.Parse(Console.ReadLine());
var result = A - B;
// First of all get the number of digits in result
var ResCnt = digitCounter(result);
var answer = result + 1;
var AnsCnt = digitCounter(answer);
if (ResCnt == AnsCnt)
Console.WriteLine(answer);
else
Console.WriteLine(answer - 1);
}
static int digitCounter(int result)
{
var temp = result;
int count = 1;
while ((temp / 10) > 0)
{
count++;
temp = temp / 10;
}
return count;
}
}
}
Please let me know why the above code is wrong.
Thanks in advance!!
import java.io.*;
import java.util.Scanner;
import java.lang.Math;
class Ceil
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
int difference = a-b;
int temp = difference;
int randomnumber = (difference%10);
while(randomnumber==(difference%10))
randomnumber = (int)((Math.random()*10));
int wrongnumber = 0;
//Changing only the last digit
wrongnumber = (((a-b)/10)*10)+randomnumber;
System.out.println(wrongnumber);
}
}
PS: Why is it not working?
/** Class for buffered reading int and double values */
class Reader
{
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input)
{
reader = new BufferedReader(new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() )
{
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
class CIELAB
{
public static void main ( String[] args ) throws IOException
{
Reader.init(System.in);
int a = Reader.nextInt();
int b = Reader.nextInt();
int res = a - b;
int bb = res;
boolean flag = false;
while ( true )
{
int rem = bb%10;
bb = bb/10;
if ( bb >= 0 && bb <= 9 )
{
if ( rem == 9 )
{
flag = true;
}
break;
}
}
if ( flag == true )
{
System.out.println(res-1);
}
else
{
System.out.println(res+1);
}
}
Weird problem. I got an AC after some WA, I changed the approach but I have no idea what was wrong with the first approach. Maybe something’s wrong with how it tests solutions.
What is wrong here???
import java.util.;
public class Main
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int a,b,x,d;
a=sc.nextInt();
b=sc.nextInt();
x=a-b;
d=x%10;
x=x/10;
d=(d+1)%10;
x=x10+d;
System.out.println(x);
}
}
#include<stdio.h>
int main()
{
int a,b,ans,result;
scanf("%d",&a);
scanf("%d",&b);
result=a-b;
if(result%10==0)
ans=result+1;
else
ans=result-1;
printf("%d",ans);
}
why this is wrong?