输入一个n,输出结果1 2 3---经典算法
来源:互联网 发布:矩阵与线阵音响 编辑:程序博客网 时间:2024/05/16 00:37
转载至:http://blog.chinaunix.net/u/33061/showart_266703.html
输入一个n,输出结果分别用1、2、3输出,即输出n长度1 2 3的所有组合。
如n=1,输出1 2 3 ,n=2,输出 11 12 13 21 22 23 31 32 33
如n=1,输出1 2 3 ,n=2,输出 11 12 13 21 22 23 31 32 33
#include <iostream>
#include <stack>
#include <vector>
#include <tchar.h>
#include<math.h>
#include <stack>
#include <vector>
#include <tchar.h>
#include<math.h>
using std::vector;
using std::stack;
using std::cout;
using std::endl;
using std::cin;
using std::stack;
using std::cout;
using std::endl;
using std::cin;
void printThree(int n,int len);
//输入n,打印出1---n的三进制,三进制各位分别加1输出
//len是输出的三进制的长度
//输入n,打印出1---n的三进制,三进制各位分别加1输出
//len是输出的三进制的长度
void printAll(int n);
//输入一个n,输出结果
//分别用1、2、3输出,输入一个n,输出n长度1 2 3的所有组合。
//如n=1,输出1 2 3 ,n=2,输出 11 12 13 21 22 23 31 32 33
int _tmain(int argc, _TCHAR* argv[])
{
int n;
cout<<"input a integer"<<endl;
cin>>n;
printAll(n);//output the result;
cout<<endl;
return 0;
}
//输入一个n,输出结果
//分别用1、2、3输出,输入一个n,输出n长度1 2 3的所有组合。
//如n=1,输出1 2 3 ,n=2,输出 11 12 13 21 22 23 31 32 33
int _tmain(int argc, _TCHAR* argv[])
{
int n;
cout<<"input a integer"<<endl;
cin>>n;
printAll(n);//output the result;
cout<<endl;
return 0;
}
void printThree(int n,int len)
{
//输入n,打印出n的三进制,三进制各位分别加1输出
//len是输出的三进制的长度
int count=0;//记忆三进制的年度(?)
stack<int> intstack;//define a stack
{
//输入n,打印出n的三进制,三进制各位分别加1输出
//len是输出的三进制的长度
int count=0;//记忆三进制的年度(?)
stack<int> intstack;//define a stack
while(n)
{//打出三进制 各位加1
intstack.push(n%3+1);
n/=3;
count++;
}
while(count<len)
{//输出len 位
intstack.push(1);
count++;
}
while(!intstack.empty())
{
cout<<intstack.top();
intstack.pop();
}
cout<<" ";
}
{//打出三进制 各位加1
intstack.push(n%3+1);
n/=3;
count++;
}
while(count<len)
{//输出len 位
intstack.push(1);
count++;
}
while(!intstack.empty())
{
cout<<intstack.top();
intstack.pop();
}
cout<<" ";
}
void printAll(int n)
{
//分别用1、2、3输出,即输出n长度1 2 3的所有组合。
//如n=1,输出1 2 3 ,n=2,输出 11 12 1321 22 23 31 32 33
for(vector<int>::size_type i=0;i<pow(3,n);i++)
printThree(i,n);
}
{
//分别用1、2、3输出,即输出n长度1 2 3的所有组合。
//如n=1,输出1 2 3 ,n=2,输出 11 12 1321 22 23 31 32 33
for(vector<int>::size_type i=0;i<pow(3,n);i++)
printThree(i,n);
}
- 输入一个n,输出结果1 2 3---经典算法
- 输出n! n为用户输入的一个正整数 n! = n*(n-1)*(n-2)*...*1 5! = 5*4*3*2*1 = 120
- 递归的使用:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n。将输出结果保留到至少8个小数点。
- 输入一个数n,输出1~n的全排列
- 输入一个数n,输出1,2,3,...,9999(n个9)(利用大数求解)
- 需找符合条件的整数(输入整数N,输出一个最小自然数M,使N*M结果只包含1跟0)
- 需找符合条件的整数(输入整数N,输出一个最小自然数M,使N*M结果只包含1跟0)
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 算法习题51:输入一个正数n,输出所有和为n连续正数序列
- 【算法学习-01】 输入一个正数 n,输出所有和为 n 连续正数序列。
- 输入一个字符串表达式,输出计算结果
- 2-1.输入n,输出1,2,3,。。。N的值(n >= 1);
- 1.输入n,输出1,2,3,。。。N的值(n >= 1)
- 经典算法: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数
- 2-3.输入正整数n <= 20,输出一个n层的正三角形
- 输入一个n,输出2到n的具体素数值
- (转)C#使用SendMessage传递字符串
- Inno Setup使用教程大全
- Grep的用法
- 7-7
- 韩国自主研发16年操作系统Tmax?
- 输入一个n,输出结果1 2 3---经典算法
- 1EB
- jspsmartupload组件问题
- CBitmap和HBITMAP的区别及相互转换方法
- QT/X11版本 在linux下的安装
- 常用的JS的函数
- ubuntu9.04下kvm虚拟机的安装与桥接网络配置(总结)
- 运用C#创建一个.Net下的Web Service
- 新会员