程序在有一些限制的情况下打印1到100个数字 [英] program to print 1 to 100 numbers with some constraints

查看:132
本文介绍了程序在有一些限制的情况下打印1到100个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家下午好.

问题:

程序需要打印1到100个数字.全为3的倍数的数字将需要打印为3.同样,全为5的倍数的数字将需要打印为5.都是3和5的倍数,则需要打印为35.


有人可以提供代码段吗?

在此先感谢

good afternoon to all..

Question:

program that need to prints 1 to 100 numbers.the number which are all multiples of 3 will need to print as 3.like wise the number which are all multiples of 5 will need to print as 5.As in the same way the numbers which are all multiples of both 3 and 5 will need to print as 35.


Can anybody provide the code snippet?

Thanks in advance

推荐答案

是.
但是我们不会.
我们不做您的作业:这是有原因的.在这里,您可以考虑自己被告知的内容,并尝试理解它.也可以在那里帮助您的导师识别您的弱点,并将更多的注意力放在补救措施上.

自己尝试,您可能会发现它并不像您想的那样困难!
Yes.
But we won''t.
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!


除了这个事实,这听起来像是入门级编程问题,它是高中数学问题还.您需要素数分解.如果您无法以某种方式解决此问题,请不要尝试成为一名程序员.这是一种递归和通用的方法: http://handcraftsman.wordpress.com/2010 /09/02/prime-factorization-in-csharp/ [ ^ ],这是另一个: http://www.blackwasp.co.uk/PrimeFactors.aspx [ ^ ].

一些数学基础知识: http://www.mathsisfun.com/prime-factorization.html [ ^ ],http://en.wikipedia.org/wiki/Prime_factor [ ^ ]
Besides the fact, that this sound like an entry level programming question, it is a high-school level mathematics question also. You need prime factorization. Don''t try to be a programmer if you can''t solve this somehow. Here is a recursive and generic approach: http://handcraftsman.wordpress.com/2010/09/02/prime-factorization-in-csharp/[^], and this is an other one: http://www.blackwasp.co.uk/PrimeFactors.aspx[^].

some mathematical basics: http://www.mathsisfun.com/prime-factorization.html[^], http://en.wikipedia.org/wiki/Prime_factor[^]


尝试一下

try this

class Program
    {
        public static void Main(string[] args)
        {
            for (int i = 1; i <= 100; i++)
            {
                int j=i;
                if ((i % 3 == 0) && (i % 5 != 0))
                    j = 3;
                else if ((i % 5 == 0) && (i % 3 != 0))
                    j = 5;
                else if ((i % 5 == 0) && (i % 3 == 0))
                    j = 35;

                Console.WriteLine(j);
            }
            Console.ReadLine();
        }
    }


这篇关于程序在有一些限制的情况下打印1到100个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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