问题:
Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
计算a+b并以标准格式输出总和——也就是说,数字必须用逗号分成三组(除非少于四位)。
问题描述
Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where −106≤a,b≤106. The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
输入规范:
每个输入文件包含一个测试用例。每种情况都包含一对整数a和b,其中−106≤a、 b个≤106.数字之间用空格隔开。
输出规格:
对于每个测试用例,您应该在一行中输出a和b的总和。总和必须以标准格式书写。
样例输入:
Sample Input:样例输入
-1000000 9
样例输出:
Sample Output:
-999,991
代码展示:c++
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
using namespace std;
int standard_output(int num) {
string s = to_string(abs(num));
char ans[10] = {0};
int length = s.length(), k = 0;
for (int i = 0; i < length; i++) {
ans[k++] = s[i];
if (i == length - 4 || i == length - 7) {
ans[k++] = ',';
}
}
if (num < 0) {
cout << "-";
}
// for (int i = 0; i <= k - 1; i++) {
// cout << ans[i];
// }
cout << ans << endl;
}
int main() {
int a, b;
cin >> a >> b;
standard_output(a + b);
}
代码展示:Java
我第一次在pta上写题目是用Java,因为我是Java初学者,然后不懂提交题目的要求啥的,直接提交这个代码,但是并不能通关,但是在ideaIC-2022.2.2上测试过关
import java.util.Scanner;
public class ABFormat {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
standard_ouput(a + b);
}
private static void standard_ouput(int num) {
String s = String.valueOf(Math.abs(num));
char[] ans = new char[10];
for (int i = 0; i < ans.length; i++) {
ans[i] = 0;
}
int pos = 0, length = s.length();
for (int i = 0; i < length; i++) {
ans[pos++] = s.charAt(i);//str.charAt(i)的意思是第i个字符在字符串str中所占的位置;
if (i == length - 4 || i == length - 7) {
ans[pos++] = ',';
}
}
if (num < 0) System.out.print("-");
for (int i = 0; i < pos; i++) System.out.print(ans[i]);
}
,就会出现这样的错误
解决方法:
但是把public删除后,还是不能过关,出现如下显示:
Compiler did not create the expected binary的意思是编译器未创建所需的二进制文件,但是我还是不知道咋该,突然我就发现一个博主所说的
就懂了我出现的错误,我就把ABFormat改成Main,就过关啦~
正确Java代码展示:
第一种方法:
import java.util.Scanner;
class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
standard_ouput(a + b);
}
private static void standard_ouput(int num) {
String s = String.valueOf(Math.abs(num));
char[] ans = new char[10];
for (int i = 0; i < ans.length; i++) {
ans[i] = 0;
}
int pos = 0, length = s.length();
for (int i = 0; i < length; i++) {
ans[pos++] = s.charAt(i);//str.charAt(i)的意思是第i个字符在字符串str中所占的位置;
if (i == length - 4 || i == length - 7) {
ans[pos++] = ',';
}
}
if (num < 0) System.out.print("-");
for (int i = 0; i < pos; i++) System.out.print(ans[i]);
}
}
第二种方法:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
ADD num=new ADD();
num.standard_ouput(a+b);
}
}
class ADD{
void standard_ouput(int num) {
String s = String.valueOf(Math.abs(num));
char[] ans = new char[10];
for (int i = 0; i < ans.length; i++) {
ans[i] = 0;
}
int pos = 0, length = s.length();
for (int i = 0; i < length; i++) {
ans[pos++] = s.charAt(i);//str.charAt(i)的意思是第i个字符在字符串str中所占的位置;
if (i == length - 4 || i == length - 7) {
ans[pos++] = ',';
}
}
if (num < 0) System.out.print("-");
for (int i = 0; i < pos; i++) System.out.print(ans[i]);
}
}
祝你们好运~