About some exercises positive integer

1. Enter a positive integer n, the number of bits output.

Sample input: 123
Output Sample: 3

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
 int n,count=0;
 cin>>n;
 while(n!=0)
 {
  n=n/10;
  count++;
 }
 cout<<count<<endl;
 } 

2. Enter a positive integer n, the digits from the low to the high output n.

Sample input: 123
Output Sample: 321

#include<iostream>
#include<cmath>
using namespace std;
int main()
 {
  int n=0;
  cin>>n;
  while(n!=0)
  {
   cout<<n%10<<" ";
   n=n/10;
  }
 }

3. Enter a positive integer n, the digits from the high to the low output n.

Sample input: 123
Output Sample: 1 2 3

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
 int n,count,i=0;
 int a[10];
 cin>>n;
 while(n!=0)
 {
  a[i++]=n%10;
  n=n/10;
  count++;
 }
 for(int m=count;m>0;m--)
 {
  cout<<a[m-1]<<" ";
 }
}

4. Enter a positive integer n, 2 times the integer outputting reverse.

Sample input: 125
Output Sample: 1042 Description: After 125 times is the reverse of 1042 521,2

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
  int n,count,i,sum,res=0;
  int a[10];
 cin>>n;
 while(n!=0)
 {
  a[i++]=n%10;
  n=n/10;
  count++;
 }
 for(int m=count;m>0;m--)
 {
  sum=sum+a[m-1]*pow(10,count--);
 }
 sum=sum/10;
 res=2*sum;
 cout<<res<<endl;
} 

5. Enter a number, and outputs the factorization expression. It requires the expression of each factor from small to large.

Sample input:
60
sample output:

2*2*3*5
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
 int n,m=0;
 int a[20];
 cin>>n;
 int i=2;
 while(n>=0)
 {
  
  if(n%i==0)
  {
   a[m++]=i;
   n=n/i;
  }
  else
  {
   i++;
  }
 }
 for(int k=0;k<m-2;k++)
 {
  cout<<a[k]<<"*";
 }
 cout<<a[m-2];
}
Published 102 original articles · won praise 93 · views 4968

Guess you like

Origin blog.csdn.net/huangziguang/article/details/104722146