7-8 整除光棍
2018-03-20 22:57
155 查看
这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1、11、111、1111等。传说任何一个光棍都能被一个不以5结尾的奇数整除。比如,111111就可以被13整除。 现在,你的程序要读入一个整数
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
int main()
{
int n;
cin>>n;
int flag=0;
int i;
int x=0;
for(i=1;; i++)
{
x=x*10+1;
if(x>=n)
{
flag=1;
cout<<x/n;
}
else if(flag)
{
cout<<0;
}
x=x%n;
if(x==0) break;
}
cout<<" "<<i<<endl;
// cout << "Hello world!" << endl;
return 0;
}
x,这个整数一定是奇数并且不以5结尾。然后,经过计算,输出两个数字:第一个数字
s,表示
x乘以
s是一个光棍,第二个数字
n是这个光棍的位数。这样的解当然不是唯一的,题目要求你输出最小的解。提示:一个显然的办法是逐渐增加光棍的位数,直到可以整除
x为止。但难点在于,
s可能是个非常大的数 —— 比如,程序输入31,那么就输出3584229390681和15,因为31乘以3584229390681的结果是111111111111111,一共15个1。
输入格式:
输入在一行中给出一个不以5结尾的正奇数x(<)。
输出格式:
在一行中输出相应的最小的s和
n,其间以1个空格分隔。
输入样例:
31
输出样例:
3584229390681 15思路:这就是一个模拟除法的过程,只不过后面的数是一次给出的,在比赛的时候没想到是模拟除法的过程,要把所学到的知识灵活应用。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
int main()
{
int n;
cin>>n;
int flag=0;
int i;
int x=0;
for(i=1;; i++)
{
x=x*10+1;
if(x>=n)
{
flag=1;
cout<<x/n;
}
else if(flag)
{
cout<<0;
}
x=x%n;
if(x==0) break;
}
cout<<" "<<i<<endl;
// cout << "Hello world!" << endl;
return 0;
}
相关文章推荐
- [patl1-046]整除光棍
- L1-046. 整除光棍
- L1-046. 整除光棍
- L1-046. 整除光棍
- L1-046. 整除光棍
- PAT-L1-046. 整除光棍
- L1-046. 整除光棍【java高精度】
- 7-8 整除光棍(20 分)(模拟除法)
- PAT L1——046. 整除光棍----模拟除法
- L1-046. 整除光棍(模拟竖式计算除法)
- 2017年团体程序设计天梯赛-总决赛 L1-6. 整除光棍
- PAT-L1-046. 整除光棍满分解答
- 7-5 整除光棍(20 分)
- 整除光棍
- L1-046. 整除光棍
- L1-046. 整除光棍
- 大数(乘、除、加)L1-046. 整除光棍-----Java
- 7-9 整除光棍(25 分)
- 整除光棍
- L1-046. 整除光棍