Mantissa of integral Division
Problem Description
An integer that knows only the first few bits and does not know the last two bits is divided by another integer. What is the last two bits of the number?
Input
There are several groups of input data, each group of data contains two integers a, b (0 < a < 10000, 10 < b < 100). If 0 is encountered, the processing is finished.
Output
Corresponding to each group of data, output all mantissa that meet the conditions in one line. See sample output for format. For the output of the same group of data, there is a space between each mantissa and no space at the end of the line.
Sample Input
200 40
1992 95
0 0
Sample Output
00 40 80
15
The problem is logically flat, which is the most difficult problem for beginners like me:
- How to realize the space between numbers
- How to output 0 as 00
At first, my method was so stupid that I even thought of converting it to string forced output 00. Later, I found out that c + + has
cout.width(2);//Output bit is 2 cout.fill('0');//If it's not enough, fill it with 0 (note that it must be '0', with a pair of quotation marks; if it's not OK with light 0, it will still output 2 bits, but the filled bits are empty)
The first one is that there are many ingenious methods. My own methods are very stupid. When I look online, I suddenly realize that I can explain them in detail in the code
Method 1:
//There is a space between the output numbers and there is no space at the end for(int i=0;i<num;i++){ cout<<count[i]; if(i!=num-1) cout<<' '; } cout<<endl;
Method two:
///or the second method of spaces between numbers cout<<count[0]; for(int i=1;i<num;i++){ cout<<" "<<count[i]; } cout<<endl;
Code above:
#include<iostream> #include<cstring> using namespace std; int main(){ int a,b; while((cin>>a>>b)&&a>0){ int num=0; int count[105]; for(int i=0;i<=9;i++){ for(int j=0;j<=9;j++){ if((100*a+10*i+j)%b==0){ count[num]=10*i+j; num++; } } } //How to output two digits cout.width(2); cout.fill('0'); //There is a space between the output numbers and there is no space at the end for(int i=0;i<num;i++){ cout<<count[i]; if(i!=num-1) cout<<' '; } cout<<endl; // //or the second method of spaces between numbers // cout<<count[0]; // for(int i=1;i<num;i++){ // cout<<" "<<count[i]; // } // cout<<endl; //Stupid method black history, but also hard to empty the array, hard not to please // if(num==1){ // cout<<count[0]<<endl; // }else{ // cout<<count[0]<<"hh"; // for(int i=1;i<=num;i++){ // cout<<count[i]<<" "; // } // //cout<<"jj"<<count[num]<<endl; // memset(count,0,num*sizeof(int)); // } } }