新手求教简单编程题

huifeidemayi945 2011-03-07 07:45:03
固定值P1=100. 变量P2初始值为10. P2=P2+t. t的值在程序启动前可输入. 进行多次循环,循环次数是要求的值. 每次循环后P2都变化.-----P2=P2+t. 判断条件是P1-P2<5 .退出程序
例子:
t=2 P2=10+2=12 P1-P2=100-12=88 >=5 程序继续
t=2 P2=12+2=14 P1-P2=100-14=86 >=5 程序继续
..............................................
t=2 P2=94+2=96 P1-P2=100-96=4 <5 程序退出.
求一共进行多少次循环,每次循环结束后显示P2 P1-P2 循环次数

...全文
143 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
huifeidemayi945 2011-03-08
  • 打赏
  • 举报
回复
刚申请的号,没多少分,只能给1楼2楼各10分了,ccforevercc不好意思啦 囧囧囧囧囧囧
huifeidemayi945 2011-03-08
  • 打赏
  • 举报
回复
非常感谢LBJMD、hnuqinhuan、ccforevercc,我是新手,刚开始学习c++,非常感谢各位帮忙解答。不是课后作业。我正在自学中......
yileisen 2011-03-07
  • 打赏
  • 举报
回复
看热闹
CiCi 2011-03-07
  • 打赏
  • 举报
回复

//////////////////////////////////////////////////////


#include <iostream.h>

void main()
{
int P1=100;
int P2=10;
int t;
cout<<"请输入t的值:"<<endl;
cin>>t;
int i=0;
while (P1-P2>=5)
{
P2+=t;
cout<<"t="<<t<<" "<<"P2="<<P2<<"+"<<t<<"="<<P2<<" "<<"P1-P2="<<P1<<"-"<<P2<<"="<<P1-P2<<" "<<">=5"<<"循环第"<<i+1<<"次"<<" 程序继续"<<endl;
++i;
}
cout<<"t="<<t<<" "<<"P2="<<P2<<"+"<<t<<"="<<P2<<" "<<"P1-P2="<<P1<<"-"<<P2<<"="<<P1-P2<<" "<<"<5"<<"循环第"<<i+1<<"次"<<" 程序退出"<<endl;
cout<<"共循环了"<<i+1<<"次"<<endl;
}

//////////////////////////////////////////////////////////////

#3楼写的判断条件写错了··· 从给你写下···
無_1024 2011-03-07
  • 打赏
  • 举报
回复

#include <iostream>
#include <cstdlib>

using namespace std;

const int p1 = 100;

int main()
{
int count = 0;
int p2 = 10,t;
cout << "input t: ";
cin >> t;
while(1)
{
cout << "t = " << t << " p2 = " << p2 << " + " << t << " = " << p2 + t;
p2 = p2 + t;
cout << " p1 - p2 = " << p1 << " - " << p2 << " = " << p1 - p2 ;
count ++;
if( p1 - p2 >= 5 )
{
cout << ">=5 程序继续" << endl;
}
else
{
cout << "<5 程序退出" << endl;
break;
}
}
cout << "the total time is " << count << endl;
return 0;
}
CiCi 2011-03-07
  • 打赏
  • 举报
回复

////////////////////////////////////////////
#include <iostream.h>

void main()
{
int P1=100;
int P2=10;
int t;
cout<<"请输入t的值:"<<endl;
cin>>t;
for(int i=0;P1-P2>5;++i)
{
P2+=t;
cout<<"P2="<<P2<<" "<<"P1-P2="<<P1-P2<<" "<<"循环第"<<i+1<<"次"<<endl;
}

}

/////////////////////////////////////////////


.... 是在帮你做作业么···
無_1024 2011-03-07
  • 打赏
  • 举报
回复

#include <iostream>
#include <cstdlib>

using namespace std;

const int p1 = 100;

int main()
{
int p2 = 10,t;
cout << "input t: ";
cin >> t;
while(1)
{
cout << "t = " << t << " p2 = " << p2 << " + " << t << " = " << p2 + t;
p2 = p2 + t;
cout << " p1 - p2 = " << p1 << " - " << p2 << " = " << p1 - p2 ;
if( p1 - p2 >= 5 )
{
cout << ">=5 程序继续" << endl;
}
else
{
cout << "<5 程序退出" << endl;
break;
}
}
return 0;
}
LBJMD 2011-03-07
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
int main()
{
int p1=100,p2=10;
int t;
int count=0;
cout<<"Input number t:"<<endl;
cin>>t;

while(p1-p2>=5)
{
p2+=t;
count++;
}

cout<<count<<endl;
return 0;
}

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧