WAP查找并打印给定数字中的第二大数字? (不使用数组,函数和仅使用一个循环)。 [英] WAP to find and print second largest digit in the given number? ( Without using arrays, functions and using only one loop).

查看:73
本文介绍了WAP查找并打印给定数字中的第二大数字? (不使用数组,函数和仅使用一个循环)。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(不使用数组,函数和仅使用一个循环)。



我尝试过的方法:



i编写了最大数字的代码,可以尝试第二大数字。不使用数组,函数。



  #include   <   stdio.h  >  
# include < stdlib.h >
#include < limits.h >
int main()
{
i nt n;
printf( 输入数字:);
scanf( %d,& n);
int min = INT_MIN,temp;
while (n!= 0
{
temp = n% 10 ;
if (temp> min)
min = temp;
n = n / 10;
}
printf( large,min);
return 0 ;
}

解决方案

我们不做你的HomeWork。

HomeWork未设置为测试你乞求别人做你的工作的技巧,它会让你思考并帮助你的老师检查你对你所学课程的理解,以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

所以,试一试,重读你的课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。



建议:拿一张纸并尝试手工完成,你的程序应该使用相同的程序。

为了判断一个数字是否是第二大数字,你需要同时跟踪最大数字。

在你的纸上,做2列为最大值和第2列,然后尝试查找数字即将发生的事情。



问题:当你有998时,答案是什么?

起初我认为对你的代码稍作修改就足够了。然而问题有其警告。

尝试

  #include   <   stdio.h  >  
int main()
{
int n,ld,sld;
printf( 输入正数:);
scanf( %d,& n);
if (n< = 0
{
printf ( no cheating,please\\\
); // 处理非正数作为练习
返回 - 1 ;
}

ld = - 1 ;
sld = - 1 ;

while (n)
{
int r = n% 10 ;
if (ld< r)
{
if ( sld< ld)
sld = ld ;;
ld = r;
}
其他 如果(ld> r&& sld< ; r)
{
sld = r;
}

n / = 10 ;
}

如果(ld!= - 1 )printf ( 最大数字是%d \ n,ld);
if (sld!= - 1 )printf( 第二大数字是%d \ n,sld);
return 0 ;
}


#include< stdio.h>

#include< conio.h>

void main()

{



int n,i = 0,r1 = 0,r,r2;

scanf(%d,& n);

while(n> 0)

{

r = n %10;

n = n / 10;

if(r> r1)

r2 = r1

r1 = r;

}

printf(%d,r2);

}


( without using arrays, functions and using only one loop).

What I have tried:

i have written the code for largest digit,can u try for 2nd largest digit.without using arrays,functions.

#include <stdio.h>
#include <stdlib.h>
#include<limits.h>
int main()
{
	int n;
	printf("Enter the number : ");
	scanf("%d",&n);
	int min=INT_MIN,temp;
	while(n!=0)
	{
		temp = n % 10;
		if(temp>min)
			min=temp;
		n = n/10;
	}
	printf(" large",min);
	return 0;
}

解决方案

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.
In order to tell if a digit is the second greatest, you need to track the greatest at the same time.
On you sheet of paper, do 2 columns for greatest and second, then try to find how things evolve as digits are coming.

Question: when you have 998, what is the answer ?


At first I thought a slight modification to your code would have been enough. However the problem had its caveats.
Try

#include <stdio.h>
int main()
{
  int n, ld, sld;
  printf("Enter a positive number number : ");
  scanf("%d",&n);
  if ( n <= 0 )
  {
    printf("no cheating, please\n"); // handling of non-positive numbers left as exercise
    return -1;
  }

  ld  = -1;
  sld = -1;

  while (n)
  {
    int r = n % 10;
    if ( ld < r )
    {
      if ( sld < ld)
        sld = ld;;
      ld = r;
    }
    else if ( ld > r && sld < r)
    {
      sld = r;
    }

    n /= 10;
  }

  if (ld != -1) printf("largest digit is %d\n", ld);
  if (sld != -1) printf("second largest digit is %d\n", sld);
  return 0;
}


#include<stdio.h>
#include<conio.h>
void main()
{

int n,i=0,r1=0,r,r2;
scanf("%d",&n);
while(n>0)
{
r=n%10;
n=n/10;
if(r>r1)
r2=r1
r1=r;
}
printf("%d",r2);
}


这篇关于WAP查找并打印给定数字中的第二大数字? (不使用数组,函数和仅使用一个循环)。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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