简体   繁体   中英

program to add one to each digit of a number

As new to competitive programming, I was solving this practice question. The goal is to write a program to display numbers whose digits are 1 greater than the corresponding digits of the entered number. So if the number input is 12345 then the output number should be 23456. I have figured out how to separate each number and add them, but I was unable able to take a number of test cases in the following program.

The question is as follows

Input

First line of input will contain a number N = number of test cases. Next N lines will contain number n as test case where 1<=n<=99999.

Output

For each input case, add one to each digit of n, and print the new number.

As a beginner in competitive programming would be helpful if you give some tips to optimize the code.

here is the code that I have written.

#include<stdio.h>
void main()
{
    int n, t, sum = 0;
    scanf("%d", &t);
    int a[t];
    for (int j = 0; j < t; j++)
    {
        for (int i = 0; i < t; i++)
        {
            scanf("%d", &n);
            a[i] = n;

            if (t == 1) {
                if (i == 0) {
                    a[i] = (a[i] + 1) * 1;
                }
            }
            else if (t == 2) {
                if (i == 0) {
                    a[i] = (a[i] + 1) * 10;
                }
                else if (i == 1) {
                    a[i] = (a[i] + 1) * 1;
                }
            }
            else if (t == 3) {
                if (i == 0) {
                    a[i] = (a[i] + 1) * 100;
                }
                else if (i == 1) {
                    a[i] = (a[i] + 1) * 10;
                }
                else if (i == 2) {
                    a[i] = (a[i] + 1) * 1;
                }
            }
            else if (t == 4) {
                if (i == 0) {
                    a[i] = (a[i] + 1) * 1000;
                }
                else if (i == 1) {
                    a[i] = (a[i] + 1) * 100;
                }
                else if (i == 2) {
                    a[i] = (a[i] + 1) * 10;
                }
                else if (i == 3) {
                    a[i] = (a[i] + 1) * 1;
                }
            }
            else if (t == 5) {
                if (i == 0) {
                    a[i] = (a[i] + 1) * 10000;
                }
                else if (i == 1) {
                    a[i] = (a[i] + 1) * 1000;
                }
                else if (i == 2) {
                    a[i] = (a[i] + 1) * 100;
                }
                else if (i == 3) {
                    a[i] = (a[i] + 1) * 10;
                }
                else if (i == 4) {
                    a[i] = (a[i] + 1) * 1;
                }
            }
            else if (t == 6) {
                if (i == 0) {
                    a[i] = (a[i] + 1) * 100000;
                }
                else if (i == 1) {
                    a[i] = (a[i] + 1) * 10000;
                }
                else if (i == 2) {
                    a[i] = (a[i] + 1) * 1000;
                }
                else if (i == 3) {
                    a[i] = (a[i] + 1) * 100;
                }
                else if (i == 4) {
                    a[i] = (a[i] + 1) * 10;
                }
                else if (i == 4) {
                    a[i] = (a[i] + 1) * 1;
                }
            }
        }
    }
    for (int i = 0; i < t; i++)
    {
        sum = sum + a[i];
    }
    printf("%d\n", sum);
}

I've reworked on the code from beginning and I've made a solution for you:

#include <stdio.h>

int main(void)
{
    int num, sum, remainder, check; // check used as a boolean expression
    sum = check = 0;

    printf("Enter the sequence: ");
    scanf("%d", &num);

    while (num > 0)
    {
        remainder = num % 10; // each time num is reduced

        if (remainder != 9)
        {
            if (check == 0)
                sum = (10 * sum) + (remainder + 1);
            else
            {
                sum = (10 * sum) + (remainder + 2);
                check = 0;
            }
        }
        else
        {
            sum = (10 * sum) + 0;
            check = 1;
        }
        num /= 10; // will divide and execute in each iteration until it's true
    }

    num = sum; // final number will be equal to the sum
    sum = 0;

    // Summing up the results
    while (num > 0)
    {
        remainder = num % 10;
        sum = (10 * sum) + remainder;
        num /= 10;
    }

    printf("Result: %d\n", sum);

    return 0;
}

Test Output

Enter the sequence: 23456
Result: 34567

It's just all about the sum & remainder. Hope it helps you understand better.

import java.util.Scanner;

class Main
{
  public static void main(String[] args)
  {
    int num,i=1,j;
    Scanner scan=new Scanner(System.in);
    int numo=scan.nextInt();num=numo;
    for(;numo>0;numo=numo/10,i=i*10)
    {
      num=num+i;
      if(numo%10==9)  
      num=num-i*10;  
    } 
  System.out.println(num);
  }
}

Hope you find the below solution helpful. it's done using basic remainder and reverse approach:-

int addOne(int n)
{
    int rem, ans=0, p=1 ;
    while(n>0)
    {
        rem = n%10;
        (rem == 9)?rem = 0:rem+=1;
        ans+=p*rem;
        p*=10;n/=10;
    }
    return ans;
}

int main() {
   
   int n;
   cin>>n;
   cout<<addOne(n);
   return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM