• 数字递归输出


    数字递归输出

    一个朋友遇到一个不是很熟悉的问题,对于新手或许有些帮助,没有其他检验操作,只是递归.
    

    详情

    题目1要求将一个正整数按序输出,要去使用递归.
    eg.input 12345
       output 1-2-3-4-5
    
    #include <stdio.h>  
    #include <stdlib.h>  
    #include <math.h>  
    
    int fn(int n)  
    {  
        
    	int s=0;
    	int y=0;
    	s=n/10;
    	y=n%10;
    	if(s!=0)
    	{
    		fn(s);
    		printf("-%d",y);
    	}
    	else
    	{
    		printf("%d",y);
    	}
    }
    
    
    int main()  
    {  
        int m=1234;  
    	fn(m);
        return 0;  
    }  
    
    题目2要求根据输入的数据,直到?截至,然后通过递归倒序输出.
    eg. input 1234567?
        output 7654321
    
    #include"iostream"
    using namespace std;
    
    void fn(char* s){
    	char *p=s+1;
    	if(*s!=''){
    		fn(p);
    		cout<<*s;
    	}
    	
    }
    
    int main(){
    	char a[100];
    	int i=0;
    	cin>>a[i];
    	char t;
    	while(a[i]!='?')
    	{
    		cin>>a[++i];
    	}
    	a[i]='';
    	char *p=a;
    	fn(p);	
    	return 0;
    }
    
  • 相关阅读:
    线程私有关键字配合static使用
    对定义局部变量位置的思考
    获取文件的md5值
    JNI接口的整理
    数兔子问题
    使用XML布局文件和java代码混合控制UI界面
    应用程序类型
    android组成
    封装
    接口
  • 原文地址:https://www.cnblogs.com/wangkun1993/p/8287279.html
Copyright © 2020-2023  润新知