输入一个n,输出结果1 2 3---经典算法
2009-07-07 14:29
309 查看
转载至: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
#include <iostream>
#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;
void printThree(int n,int 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;
}
void printThree(int n,int len)
{
//输入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<<" ";
}
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);
}
输入一个n,输出结果分别用1、2、3输出,即输出n长度1 2 3的所有组合。
如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>
using std::vector;
using std::stack;
using std::cout;
using std::endl;
using std::cin;
void printThree(int n,int 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;
}
void printThree(int n,int len)
{
//输入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<<" ";
}
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);
}
相关文章推荐
- 设计一个程序,从键盘上输入若干字符串,利用算法库中的查找函数对给定的字符串进行查找,将查找后的结果输出
- 开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
- 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果输出。 即输出P%1000000007
- 编写一函数,由实参数来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述的结果
- 例9.3 有3个候选人,每个选民只能投票选一人,要求编一个统计选票的程序,先后输入被选人的名字,最后输出各人得票结果。
- 给定一个正整数,编写程序计算有多少对质数的和等于输入的这个正整数,并输出结果。
- 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形。 例如: 输入:3+8×2/9-2 输出:2
- 输入一个整数,判断它的奇偶性后输出结果。
- 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序
- 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序
- 求实数的绝对值。输入数据有多组,每组占一行,每行包含一个实数。对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 有三个候选人,每个选民只能投选一人,要求编一个统计选票的程序,先后输入被选人的名字,最后输出各人得票结果
- Java 输入一个数放到变量n中,如果这个数大于0.计算1+2+3+。。。+(n-1)+n的结果, //否则输出 输入的撒数据有误
- 华为机试——通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
- [算法]将一个正整数拆分成若干个正整数的和,输出所有的结果不重复
- (c++)编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
- 算法例题-求输入一个整数,输出对应二进制中1的个数。
- 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。