这几天写的几个小程序

来源:互联网 发布:老电视能看网络电视吗 编辑:程序博客网 时间:2024/04/30 01:12

在网上找了几个小练习题做了做,回顾回顾基础

 

/**
 小明在一张纸上写了一个四位数3_45(其中_代表一个看不清的数字)。已知这个四位数被3除后值为1115,编程求出_
*/
class ti1
{
 public static void main(String[] args)
 {
  int a = 1115*3;
  int _ = a%1000/100;
  System.out.println(_);
 }
}

/**
学校买来100张电影票,花了580元,票价有5元、6元。编写程序算出两种电影票各有多少张?
*/
class ti3
{
 public static void main(String[] args)
 {
  int piaoa=0;
  int piaob=100;
  while(true)
  {
   if(piaoa*5+piaob*6 == 580)
   {
    System.out.println("最终答案是:piaoa="+piaoa+";"+"piaob="+piaob);
    break;
   }
   else
   {
    piaoa++;
    piaob=100-piaoa;
   }
   System.out.println("piaoa="+piaoa+";"+"piaob="+piaob);
  }
 }
}

/**
小林对小明说:“只要把你的日子乘以12,再把出生的月份乘以31,
然后把他们加起来,将总数告诉我。我能马上告诉你的生日是几月几日。
”小明随口说出302,小林在计算机上编了一个简单的程序,马上就算出小明的生日是2月20日。
小林的程序是怎样编的?
*/
class  ti4
{
 public static void main(String[] args)
 {
  int month;
  int day;
  //String totalCountString = args[0];
  //int totalCount=Integer.valueOf(totalCount);
  int totalCount=302;
  for(month=1;month<=12;month++)
  {
   for(day=1;day<=31;day++)
   {
    if(totalCount == day*12+month*31)
    {
     System.out.println("最终答案:month="+month+"day="+day);
     break; //怎样同时跳出两层循环
    }
    System.out.println("month="+month+"day="+day);
   }
   if(totalCount == day*12+month*31)
    {
     System.out.println("最终答案:month="+month+"day="+day);
     break;
    }
  }
 }
}

/**
求s=a+aa+aaa+aaaa+aaaaa的值,其中a是一个数字
*/
import java.lang.Math.*;
import java.util.*;
class ti5
{
 public static void main(String[] args)
 {
  Scanner can=new Scanner(System.in);
  int totalcount=can.nextInt(); //参加计算的数个数
  int a=1;
  int s=0;
  for(int count=0;count<totalcount;count++)
  {
   int tempcount=count;
   while(tempcount>=0)
   {
    s+=a*Math.pow(10,tempcount);
    tempcount--;
   }
  }
  System.out.println(s);
 }
}

 

原创粉丝点击