来自学妹的一道题

sprintf的用法

Posted by Zexin Zhang on December 10, 2017

img sprintf详细用法

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main() {
	int a, b, c;
	while (~scanf("%d%d%d", &a, &b, &c) && a != 0 && b != 0 && c != 0) {
		double ans;
		ans = (double)a / (double)b;
		char a[100];
		sprintf(a, "%llf", ans);
		int cnt = 10000;
		for (int i = 0; cnt != 0; i++, cnt--) {
			if (a[i] == '.') {
				cnt = c + 1;
			}
			if (cnt == 1) {
				if ((a[i + 1] - '0') >= 5)
					printf("%d", a[i] - '0' + 1);
			}
			else
				printf("%c", a[i]);
		}
	}

}