工作室课题—输入输出格式练习

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值