solution1
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n, d, power = 1, x = 0, t, tn, x1, cnt = 0;
scanf("%d%d", &n, &d);
tn = n;
while(tn){
if(cnt < d) x += power * (tn % 10);
tn /= 10;
power *= 10;
cnt++;
if(cnt == d) x1 = tn;
}
printf("%.2f", (x * pow(10, cnt - d)+ x1) / n);
return 0;
}
solution2
数位的处理转换为字符位置的拼接,免于依次取出各位
#include<iostream>
#include<string>
using namespace std;
int main(){
string a, b;
int d;
cin >> a >> d;
b = a.substr(a.size() - d, a.size()) + a.substr(0, a.size() - d);
printf("%.2f", stod(b) / stod(a));
return 0;
}