有连续n(n<=9)个个位数字,问能组成多少个互不相同且无重复的三位数?都是多少?(OJ系统上提交)
input:
有多组数据,每组数据由两个自然数组成。
第1个是数据的个数,第2个数是起始数字。如4 1,表示从1开始的4个数字,即1 2 3 4,再如3 7表示从7开始的3个数字,即7 8 9。
output:
每个3位数占4位宽度,每行输出10个数,按数据由小到大的顺序输出。并在最后显示出总的个数。
Sample Input
4 1
5 5
2 3
Sample Output
123 124 132 134 142 143 213 214 231 234
241 243 312 314 321 324 341 342 412 413
421 423 431 432
Total of 24 numbers.
567 568 569 576 578 579 586 587 589 596
597 598 657 658 659 675 678 679 685 687
689 695 697 698 756 758 759 765 768 769
785 786 789 795 796 798 856 857 859 865
867 869 875 876 879 895 896 897 956 957
958 965 967 968 975 976 978 985 986 987
Total of 60 numbers.
Total of 0 numbers.
C语言。
回答如下:C++编写(DevC++即可运行,和C语言没啥区别,除了输入输出的问题,由于比较不熟悉格式化输入输出,用C++比较习惯),代码如下:
#include <iostream>
using namespace std;
int main() //意外情况,当有5个数,起始数为7,会出现溢出,所以需要判断
{
int m,n; //input
int a,b,c; //output,不一定每次都是三位数,所以可能不够用,不过对结果不影响
int temp=0;
int total=0;
cout<<"请输入两个数(数据个数,起始数):";
cin>>m>>n;
if(m+n>10){
cout<<"情况不符合,溢出了,两个数的和不能超过9,已退出"<<endl;
return 0;
}
//当数字超过三个或者不足三个时,需要特殊处理
if(m<3){
cout<<"数字个数小于3,Total of 0 numbers"<<endl;
}
else{
for(int i=n;i<m+n;i++){
for(int j=n;j<m+n;j++){
for(int k=n;k<m+n;k++){
if(i!=j &&i!=k && j!=k){
cout<<i<<j<<k<<" ";
temp++;
if(temp%10==0){
cout<<endl;
}
total++;
}
}
}
}
}
cout<<endl;
cout<<"Total of "<<total<<" numbers"<<endl;
}
这是排列组合问题,应该就是987