A - 商
编一个程序求A/B的值 ,要求精确到小数点后N位(N<=80的自然数,并且A<B,A和B是整型 数范围),不足N位的用“0”补齐。例如:精确到小数点后9位:6/7=0.857142857。输入A、B、N, 求A/B。
Input
输入文件: 只有一行,就是A,B,N,
Output
输出文件: 只有一行,就是A/B的结果。
Sample Input
6 7 9
Sample Output
0.857142857
简单题不断递归,算出指定的位数即可,注意,不用四舍五入
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
int arr[85];
int x,n;
void dfs(int depth,int num)
{
if(depth==n)
{
return;
}
arr[depth]=num*10/x;
dfs(depth+1,num*10%x);
}
int main()
{
int y;
while(cin>>y>>x>>n)
{
dfs(0,y);
printf("0.");
for(int i=0;i<n;i++)
{
printf("%d",arr[i]);
}
cout<<endl;
}
return 0;
}
B - 平衡等式
写一个程序要求当输入在整数范围内的一个整数R后, 计算机便会检查,在下式□处能否填上“+”、“-”或“×”号凑成相应等式。如能凑成,则印出所有这些等式的个数。注意,考虑符号的优先级。 1□2□3□4□5□6□7□8□9=R
Input
只有一行,就是一个整数R。
Output
只有一行,就是使等式成立的个数。
Sample Input
20
Sample Output
30
dfs穷举即可
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
const double eps=1e-6;
long long cnt=0,R;
int arr[9]={1,2,3,4,5,6,7,8,9};
void dfs(int depth,int num,int befo,int sign)
{
if(depth==9)
{
if(R==num)
cnt++;
return;
}
dfs(depth+1,num+arr[depth],arr[depth],1);
dfs(depth+1,num-arr[depth],arr[depth],-1);
dfs(depth+1,num+sign*arr[depth]*befo-sign*befo,arr[depth]*befo,sign);
// dfs(depth+1,num/arr[depth]);
}
int main()
{
while(cin>>R)
{
cnt=0;
dfs(1,1,1,1);
cout<<cnt<<endl;
}
return 0;
}