题目第二句和第三句说的不是很清楚啊,像这样的题目先手动算一下,找规律再coding就很简单了。(按我的理解是第二个星期的星期六攒100元:(10*6+20)/2 + 10*6)
解决方案 »
- 一个SSH框架的有趣的问题,大家都进来看看
- 抽象类和接口的特征能否具体讲解?
- 可自动恢复的JMS消息收发类,完美处理异常
- 学java一定要学oracle吗?
- 急急急!100分,十万火急呀,cvs服务器配置
- tomcat5.5 ssi 中文乱码
- action中有一个String类型的test属性,我想在前台用JS alert出来,怎么写?
- 关于Struts标签的困惑
- 有研究ofbiz的朋友吗?大家留下个联系方式!!
- java客户端调用vb写的webservice失败?为什么?急!!!
- 大神如何用JSP在留言表里面上传图片,并且显示出来
- No Session found for current thread
double money=0;
int day=0;
do{
for(int i=0;i<6;i++){
//周一到周六每天存10元钱
money+=10;
day++;
}
//周日
money=(money+20)/2;
day++;
}while(money<100)
int day = 0; //总天数
int flag = 0; //星期几 while(Zong0HQ < 100) {
if(Zong0HQ + 10 < 100) {
if(flag == 6 ) {
Zong0HQ = (Zong0HQ + 10) / 2;
flag = 0;
} else {
Zong0HQ += 10;
flag++;
}
} else {
Zong0HQ += 10;
flag++;
}
System.out.println(day + ": " + Zong0HQ);
if(Zong0HQ < 100) day++;
}结果
0: 10
1: 20
2: 30
3: 40
4: 50
5: 60
6: 35
7: 45
8: 55
9: 65
10: 75
11: 85
12: 95
13: 105第14天即第二个星期天不花钱之前就能存够100
int day = 0; //总天数
int flag = 0; //星期几 while(Zong0HQ < 100) {
if(flag == 6 ) {
Zong0HQ = (Zong0HQ + 10) / 2;
flag = 0;
} else {
Zong0HQ += 10;
flag++;
}
System.out.println(day + ": " + Zong0HQ);
if(Zong0HQ < 100) day++;
}0: 10
1: 20
2: 30
3: 40
4: 50
5: 60
6: 35
7: 45
8: 55
9: 65
10: 75
11: 85
12: 95
13: 52
14: 62
15: 72
16: 82
17: 92
18: 102如果要算花完钱之后存够100的话
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head> <body>
<script type="text/javascript">
<!--
var s = 0;
for(var i = 1;;i++){
if(i%7 != 0){
s = s + 10;
}else{
s = (s + 20)/2;
}
if(s >= 100){
document.writeln("i=",i," s = ",s);
break;
}
}
//-->
</script>
</body>
</html>结果为:13天
public static void main(String[] args) {
System.out.println(countDay(20, 100));
}
public static int countDay(int getMoney,int saveMoney){
int sum=0;
int day=0;
for (int i = 1;; i++) {
day++;
if (i % 7 != 0) {
sum+=getMoney>>1;
}
else{
sum>>=1;
}
if(sum>=100){
break;
}
}
return day;
}
getmoney是给的零花钱,savemoney是要存的钱
int i = 1;
while (true) {
System.out.print("number of days :"+i);
if(i==7) {
money = money /2;
System.out.println(" ### the money :"+money);
i = 1;
continue;
}
money += 20/2;
if(money>=100){
System.out.println(" OK,money great than or equal to 100. number of days :"+i+" the money :"+money);
break;
}
System.out.println(" the money :"+money);
i++;
}
number of days :1 the money :10
number of days :2 the money :20
number of days :3 the money :30
number of days :4 the money :40
number of days :5 the money :50
number of days :6 the money :60
number of days :7 ### the money :30
number of days :1 the money :40
number of days :2 the money :50
number of days :3 the money :60
number of days :4 the money :70
number of days :5 the money :80
number of days :6 the money :90
number of days :7 ### the money :45
number of days :1 the money :55
number of days :2 the money :65
number of days :3 the money :75
number of days :4 the money :85
number of days :5 the money :95
number of days :6 OK,money great than or equal to 100. number of days :6 the money :105
int i = 1;
while (true) {
System.out.print("number of days :"+i);
//拿到钱,存一半
money += 20/2;
if(i==7) {
//周日,花掉一半
money = money /2;
System.out.println(" ### the money :"+money);
i = 1;
continue;
}
if(money>=100){
//检查是否达到100
System.out.println(" OK,money great than or equal to 100. number of days :"+i+" the money :"+money);
break;
}
System.out.println(" the money :"+money);
i++;
}
number of days :1 the money :10
number of days :2 the money :20
number of days :3 the money :30
number of days :4 the money :40
number of days :5 the money :50
number of days :6 the money :60
number of days :7 ### the money :35
number of days :1 the money :45
number of days :2 the money :55
number of days :3 the money :65
number of days :4 the money :75
number of days :5 the money :85
number of days :6 the money :95
number of days :7 ### the money :52
number of days :1 the money :62
number of days :2 the money :72
number of days :3 the money :82
number of days :4 the money :92
number of days :5 OK,money great than or equal to 100. number of days :5 the money :102这个应该没问题了吧
int days = 0;
do {
days++;
int money = 20; //每天拿到的零花钱
if(days % 7 == 0){//周日拿到“后”把“所有”零花钱花一半,ps:拿到后把所有的花一半
sum = (sum + money) * 0.5;
}else{
sum += money * 0.5; // “平日里”存零花钱的一半
}
System.out.println(days + ":" + sum);
}while (sum < 100);
System.out.println("需要"+days+"天");
1:10.0
2:20.0
3:30.0
4:40.0
5:50.0
6:60.0
7:40.0
8:50.0
9:60.0
10:70.0
11:80.0
12:90.0
13:100.0
需要13天
double n = 0;
int days = 1;
for (int i = 1; n < count; i++, days++) {
i = i % 7;
if (i != 0)
n += 10;
else
n = (n + 20) / 2;
System.out.println(days + "\t" + i + "\t" + n);
}
return days - 1;
}
public static void main(String[] args) {
System.out.println(com(100));
}
1 1 10.0
2 2 20.0
3 3 30.0
4 4 40.0
5 5 50.0
6 6 60.0
7 0 40.0
8 1 50.0
9 2 60.0
10 3 70.0
11 4 80.0
12 5 90.0
13 6 100.0
13
/**
* 小明的妈妈每天会给他20元零花钱。
* 平日里,小明先花掉一半,再把一半存起来,
* 每到周日,小明拿到钱后会把所有零花钱花掉一半。
* 请编程计算,从周一开始,小明需要多少天才能存够100元?
*
* @author zyc
*/
public class Test021 {
public static void main(String[] args) {
System.out.println(save());
} static int save() {
int day = 0,money = 0;
while(money < 100) {
money = (++day % 7 == 0) ? (money + 20) / 2 : money + 10;
}
return day;
}
}
public static void main(String[] args) {
System.out.println(save());
}
static int save() {
int day = 1,money = 20;
while(money < 100) {
money = (day%7==0)?(money/2):(money-10);
money +=20;
day++;
}
return day;
}
}12
// TODO Auto-generated method stub
final int getMoney = 20;
double TotalMoney = 0;
int days = 0;
while(TotalMoney<100){
days++;
TotalMoney+=getMoney/2;
if(days%7==0){
TotalMoney/=2;
}
System.out.println("第"+days+"天有"+TotalMoney+"元钱");
}
System.out.println(days);
}}
最后输出的结果:
第1天有10.0元钱
第2天有20.0元钱
第3天有30.0元钱
第4天有40.0元钱
第5天有50.0元钱
第6天有60.0元钱
第7天有35.0元钱
第8天有45.0元钱
第9天有55.0元钱
第10天有65.0元钱
第11天有75.0元钱
第12天有85.0元钱
第13天有95.0元钱
第14天有52.5元钱
第15天有62.5元钱
第16天有72.5元钱
第17天有82.5元钱
第18天有92.5元钱
第19天有102.5元钱
19
人家说的是: 每到周日,小明拿到钱后会把所有零花钱花掉一半。
拿到钱之后,就是要加上20元再除以2;
public class Test {
public static void main(String[] args) {
double countMoney = 0;
final double money = 20;
int result = 0;
for(int i=1;countMoney<=100;i++) {
if(countMoney>=100) {
break;
}
result++;
if(i % 7 == 0) {
countMoney += money;
countMoney /= 2;
System.out.println("第" + result + "天,小明手上的钱余额为: " + countMoney);
continue;
}
countMoney += (money / 2);
System.out.println("第" + result + "天,小明手上的钱余额为: " + countMoney);
}
System.out.println("小明攒够100元,总共花了" + result + "天");
}
}
输出结果:
第1天,小明手上的钱余额为: 10.0
第2天,小明手上的钱余额为: 20.0
第3天,小明手上的钱余额为: 30.0
第4天,小明手上的钱余额为: 40.0
第5天,小明手上的钱余额为: 50.0
第6天,小明手上的钱余额为: 60.0
第7天,小明手上的钱余额为: 40.0
第8天,小明手上的钱余额为: 50.0
第9天,小明手上的钱余额为: 60.0
第10天,小明手上的钱余额为: 70.0
第11天,小明手上的钱余额为: 80.0
第12天,小明手上的钱余额为: 90.0
第13天,小明手上的钱余额为: 100.0
小明攒够100元,总共花了13天
using namespace std;int computeDay(){
int day = 0;
int money = 0;
while(money < 100){
day++;
if (day % 7 == 0){
money = money + 20;
money = money/2;
}else{
money = money + 10;
}
}
return day;
}int main(){
cout<<computeDay()<<endl;
}
public class TTT {
public static void main(String[] args) {
double count=0;
for(int i=1;;i++){
count+=10;
if(count>=100){
System.out.println(i-1);
break;
}
if(i%7==0){
count=count-count/2;
}
}
}
}
*
* @author admin
* @version $Id: Test.java, v 0.1 Feb 8, 2014 3:55:57 PM admin Exp $
*/
public class Test { public int getDayForSaveMoney(){
int day = 0;
int money = 0;
while(money<100){
day++;//天数加1
if(day%7>0){
money += 20/2;//周一到周六花一半存一半
}else{
money = (money+20)/2; //周日所有零花钱花掉一半。 之前的零花钱加上周日20元零花钱
}
System.out.println(day+" 天存 "+money+"(元)");
}
System.out.println("一共需要 "+day+" 天才能存够100元");
return day;
}
/*
小明的妈妈每天会给他20元零花钱。 平日里,小明先花掉一半,再把一半存起来。 每到周日,小明拿到钱后会把所有零花钱花掉一半。 请编程计算,从周一开始,小明需要多少天才能存够100元?*/
public static void main(String[] args) {
Test test = new Test();
test.getDayForSaveMoney();
}
}
//m钱,x天数
int m=0,x=0;
while(m<100){
x++;
if(x%7==0){
m=(m+20)/2;
}else{
m=m+20/2;
}
System.out.println("第"+x+"天,"+m+"元钱");
}
System.out.println(x+"天,"+m+"元钱");
打印结果:第1天,10元钱
第2天,20元钱
第3天,30元钱
第4天,40元钱
第5天,50元钱
第6天,60元钱
第7天,40元钱
第8天,50元钱
第9天,60元钱
第10天,70元钱
第11天,80元钱
第12天,90元钱
第13天,100元钱
13天,100元钱
说下我的思路吧!楼上很多代码!先声明变量当前天数CurrentDays零花钱PocketMoney
首先读到“每到周日,小明拿到钱后会把所有零花钱花掉一半。”
说明书一个循环的问题可以考虑用取余的方法进行,也就是当前是第几天的天数CurrentDays%7==0的时候
PocketMoney=PocketMoney/2.0连起来就是
for(int CurrentDays=0;;CurrentDays++){
if(CurrentDays%7==0){
PocketMoney=PocketMoney/2.0 }else{
PocketMoney+=20.0/2
}
if(PocketMoney>=100){
break;
}
}
// TODO Auto-generated method stub
float balance = 0;// 小明的余额
int day = 0;
while (balance < 100) {
day++;
if (day % 7 == 0) {
balance += 20;
balance = balance / 2;
} else {
balance += 10;
}
System.out.println("day:" + day + "balance:" + balance);
}
System.out.println("一共需要:" + day + "天");
}