zoukankan      html  css  js  c++  java
  • A1001 A+B Format [字符串处理]

    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).

    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.

    Sample Input:

    -1000000 9

    Sample Output:

    -999,991

    注意下这个点 当c=0的时候while不会进行循环,要不就也可以
    改成dowhile,不然就要加if,前面一直以为,是前三位打一个
    后面才知道从后卫前三位一个逗号。
    	if (c == 0) z[num++] = 0;
    	while (c != 0)
    	{
    		z[num++] = c % 10;
    		c /= 10;
    	}
    
    #include<iostream>
    using namespace std;
    int main()
    {
    	int a, b, c = 0;
    	int z[20] = { 0 }; int num = 0;
    	int flag = 0;
    	cin >> a >> b;
    	c = a + b;
    	if (c < 0)
    	{
    		c = -c;
    		flag = 1;
    	}
    	if (c == 0) z[num++] = 0;
    	while (c != 0)
    	{
    		z[num++] = c % 10;
    		c /= 10;
    	}
    	if (flag)
    	{
    		cout << '-';
    	}
    	int i = num-1;
    	while (i!=-1)
    	{
    		cout << z[i];
    		if (i%3==0&&i>0)
    		{
    			cout << ',';
    		}
    		i--;
    	}
    	
    
    }
    
  • 相关阅读:
    用户态切换到内核态的3种方式
    vim_action
    import date
    __sizeof__()
    classmethod staticmethod
    Java对对象的引用 不是 引用调用 而是按值引用 Java不存在引用调用
    多线程同步
    Does Hadoop require SSH?
    hdfs namenode出错
    软件项目的一致性语义描述
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13812089.html
Copyright © 2011-2022 走看看