文号
t21468
阅读
7546
回复
7
收藏
5
无聊程序---数字冰雹
2009/12/20
IP:江苏
我刚学c,编了这个来练练手,呵呵,很简单的[s:258]
#include "stdafx.h"
#include<iostream>
using namespace std;
int a;
int b;
int n;
int _tmain(int argc, _TCHAR* argv[])
{
    n=1;
    cout<<"请输入检测数字:";
    cin>>a;
    cout<<"\n";
    while(a!=1){
     if(a%2==0)
        a=a*0.5;
     else
        a=(a*3+1)/2;
     cout<<"第"<<n<<"个数:"<<a<<"\t";
     n++;
    };
    cin>>b;
    return 0;
}

不过,谁能告诉我怎么让它执行完后返回重新执行吗???
还有,"cin>>b;"这条是多余的,但不加的话计算完毕就关掉了,看不到结果.
attachment icon 数字冰雹.rar 10.05KB RAR 19次下载
17年1个月前IP:未同步
172113
1楼
#include "stdafx.h"
#include<iostream>
using namespace std;
int a;
int b;
int n;
int _tmain(int argc, _TCHAR* argv[])
{
loop:
    n=1;
    cout<<"请输入检测数字:";
    cin>>a;
    cout<<"\\n";
    while(a!=1){
     if(a%2==0)
        a=a*0.5;
    else
        a=(a*3+1)/2;
     cout<<"第"<<n<<"个数:"<<a<<"\\t";
     n++;
    };
    cin>>b;
goto loop;
    return 0;
}
引用
评论
17年1个月前IP:未同步
172117
2楼
谭叔叔说要少用goto,除非迫不得已。。。。会打乱结构的

#include "stdafx.h"
#include<iostream>
using namespace std;
int a;
int b;
int n;
int _tmain(int argc, _TCHAR* argv[])
{
while(1) {
    n=1;
    cout<<"请输入检测数字:";
    cin>>a;
    cout<<"\\n";
    while(a!=1){
    if(a%2==0)
        a=a*0.5;
    else
        a=(a*3+1)/2;
    cout<<"第"<<n<<"个数:"<<a<<"\\t";
    n++;
    };
    cin>>b;
}
    return 0;
}
引用
评论
17年1个月前IP:未同步
172119
3楼
日飞日飞。。。这么点东西goto就goto嘛。。。。

输入0退出

#include "stdafx.h"
#include<iostream>
using namespace std;
int a;
int b;
int n;
int _tmain(int argc, _TCHAR* argv[])
{
while(1) {
    n=1;
    cout<<"请输入检测数字:";
    cin>>a;
    cout<<"\\n";
    while(a!=1){
    if(a%2==0)
        a=a*0.5;
    else
        a=(a*3+1)/2;
    cout<<"第"<<n<<"个数:"<<a<<"\\t";
    n++;
    };
    cin>>b;
if(b==0){return 0;}
}
    return 0;
}
引用
评论
17年1个月前IP:未同步
172162
4楼
日啊!我输到C-Free里,结果第一行就有错误,说是stdafx.h:没有这个文件或目录..... [s:94]
引用
评论
17年1个月前IP:未同步
172164
5楼
dev-c++里这么写的。。。

using namespace std;
int a;
int b;
int n;
int main(int argc, char* argv[])
{
while(1) {
    n=1;
    cout<<"请输入检测数字:";
    cin>>a;
    cout<<"\\n";
    while(a!=1){
    if(a%2==0)
        a=a*0.5;
    else
        a=(a*3+1)/2;
    cout<<"第"<<n<<"个数:"<<a<<"\\t";
    n++;
    };
    
}
    return 0;
}
引用
评论
17年1个月前IP:未同步
172166
6楼
话说stdafx不是在MFC里面才用的么?  在这里是做什么的?
引用
评论
17年1个月前IP:未同步
173068
7楼
M$习惯用stdafx.h
其实就是include的集合
我写单片机程序的,用HAL_modules.h代替。。。哈哈哈
引用
评论
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的