第1个回答 2011-12-19
/******************************************************************************************************
用c++ 判断一个整数是否全由奇数组成 并按顺序输出组成的奇数 不可重复
*//****************************************************************************************************/
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int cifang(int x,int y)
{
int sum=1;
if(y==0)
sum=1;
else
{
for(int i=1;i<=y;++i)
{
sum*=x;
}
}
return sum;
}
int main()
{
int num;
cout<<"please input your number:";
cin>>num;
if(!cin)
{cout<<"input error !"<<endl;}
char sn[20];
itoa(num,sn,10);;
int weishu=strlen(sn);
vector<int> vec;
for(int i=0;i<weishu;++i)
{
int a;
a=num/cifang(10,weishu-i-1);
num=num-a*cifang(10,weishu-i-1);
vec.push_back(a);
}
bool flag=1;
for(vector<int>::iterator it=vec.begin();it!=vec.end();++it)
{
if(*it%2==0)
{flag=0;}
}
if(flag)
{
cout<<"Yes-";
for(vector<int>::iterator is=vec.begin();is!=vec.end();++is)
{
cout<<*is<<" ";
}
}
else
{
cout<<"No-";
for(vector<int>::iterator ir=vec.begin();ir!=vec.end();++ir)
{
cout<<*ir<<" ";
}
}
}
通过把整数转换为字符串 确定整数的位数,然后判断是否全为奇数组成 ;最后输出;vc6.0编译通过本回答被提问者采纳
第2个回答 2011-12-18
用两个函数解决,第一个函数判断是否符合要求,第二个输出结果
#include<iostream>
using namespace std;
bool fun(int n){
int arr[10] = {0};
while(n != 0){
if(n%10%2 == 0 || arr[n%10] == 1)
return false;
else{
arr[n%10] = 1;
n /= 10;
}
}
return true;
}
void print(int n){
cout<<"yes-";
while(n>=10){
cout<<n%10<<",";
n /= 10;
}
cout<<n<<endl;
}
int main(){
int n;
cin>>n;
if(fun(n))
print(n);
return 0;
}
没写否定的情况
第3个回答 2011-12-18
用两个函数解决,第一个函数判断是否符合要求,第二个输出结果
#include<iostream>
using namespace std;
bool fun(int n){
int arr[10] = {0};
while(n != 0){
if(n%10%2 == 0 || arr[n%10] == 1)
return false;
else{
arr[n%10] = 1;
n /= 10;
}
}
return true;
}
void print(int n){
cout<<"yes-";
while(n>=10){
cout<<n%10<<",";
n /= 10;
}
cout<<n<<endl;
}
int main(){
int n;
cin>>n;
if(fun(n))
print(n);
return 0;
第4个回答 2011-12-18
#include<iostream>
using namespace std;
bool fun(int n){
int arr[10] = {0};
while(n != 0){
if(n%10%2 == 0 || arr[n%10] == 1)
return false;
else{
arr[n%10] = 1;
n /= 10;
}
}
return true;
}
void print(int n){
cout<<"yes-";
while(n>=10){
cout<<n%10<<",";
n /= 10;
}
cout<<n<<endl;
}
int main(){
int n;
cin>>n;
if(fun(n))
print(n);
return 0;