工作室课题—输入输出格式练习
作者:互联网
Description
按格式读入一个3位的整数、一个实数、一个字符 。
并按格式输出 一个整数占8位左对齐、一个实数占8位右对齐、一个字符 ,并用|隔开。
Input
见题面
Output
见题面
Sample Input 1
123456.789|a
Sample Output 1
123|456.8|a
Hint
HINT:时间限制:1.0s 内存限制:256.0MB
我的AC代码:
思路:先取3个字符想法子弄成数字。再把剩下的部分用字符串一次获取到。对这个字符串进行提取实数的部分,再转换成浮点数。得到三个我要的部分就可以按格式输出了。等我提交了查看题解的时候,才感觉...写得太复杂了。后面放几个简便的题解。
#include <iostream>
#include <sstream>
using namespace std;
int main(){
int a = 0,i = 0;
double b;
string str;
char c,s;
int n = 0;
while(n < 3){
s = getchar();
a = (s - '0') + a*10;
n++;
}
cin >> str;
while(str[i] != '|'){
i++;
}
c = str[i+1];
str[i] = '\0';
stringstream ss;
ss << str.substr(0,i-1);
ss >> b;
printf("%-8d",a);
cout << "|";
printf("%8.1lf",b);
cout << "|" << c;
return 0;
}
简易题解1:
#include <bits/stdc++.h>
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
char a,b,c,d,e;
double n;
int s;
scanf("%c%c%c%lf%c%c",&a,&b,&c,&n,&d,&e);
s = (a - '0') * 100 + (b - '0') * 10 + (c - '0');
printf("%-8d%c%8.1lf%c%c",s,d,n,d,e);
return 0;
}
简易题解2:
#include <stdio.h>
int main()
{
int a;
double b;
char ch;
scanf("%3d%lf|%c",&a,&b,&ch);
printf("%-8d|%8.1lf|%c",a,b,ch);
return 0;
}
标签:%-,int,题解,输入输出,c%,str,格式,include,工作室 来源: https://blog.csdn.net/weixin_51717063/article/details/123593694