我需要在c - 递增和递减数组中执行一个程序 [英] i need to do a program in c - increment and decrement array

查看:75
本文介绍了我需要在c - 递增和递减数组中执行一个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

如果数组输入:1 0 0 0

递增数组应为:1 0 0 1

递减数组:0 9 9 9



i使用标签和循环来完成这项任务。但在背诵它时似乎很复杂。所以我需要知道是否有任何简单的方法来解决这个程序。如果有的话帮助我提高我的技能。

这是我的代码。

example:
if array input: 1 0 0 0
incremented array should be:1 0 0 1
decremented array: 0 9 9 9

i have used labels and loop to complete this task. but it seems complex in reciting it. so i need to know is there any simple method to solve this program. if so help me in improving my c skill.
here is my code.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],b[20],c[20],n,i,j;
clrscr();
printf("\n Enter the no of elemnts");
scanf("%d",&n);
printf("\n enter the array\t");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
c[i]=a[i];
j=n;
check:
if((a[j-1]+1)<=9)
a[j-1]++;
else
{
a[j-1]=0;
j--;
goto check;
}
if(a[0]==0)
{
for(i=1,j=0;i<=n;i++,j++)
b[i]=a[j];
b[0]=1;
printf("result of incremnted array\t");
for(i=0;i<=n;i++)
printf("%d\t",b[i]);
}
else
{
printf("result of incremnted array\t");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
j=n;
while(j>=0)
{
if(c[j-1]==0)
 {
if(c[j-2]==0)
j=j-1;
else
{
c[j-2]--;
c[j-1]=c[j-1]+10;
if(j!=n)
j=j+1;
}
 }
else
{
c[j-1]--;
j=-1;
}
}
printf("\n decremented array");
for(i=0;i<n;i++)
printf("%d\t",c[i]);
getch();
}

推荐答案

尝试将您的数组转换为 long 值进行计算并转换回数组。
Try converting your array into a long value doing your computations and converting back int an array.


首先,您可以在没有(更优雅) for 循环的情况下对其进行编码。就像使用铅笔和纸一样。
At first, you may code it without the (more elegant) for loop. Just do it as you would do with pencil and paper.


这篇关于我需要在c - 递增和递减数组中执行一个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆