zoukankan      html  css  js  c++  java
  • pat 1001 A+B Format-test

    题目链接:传送门

    题目简述:

    1. 给定两个整数值a,b;

    2.范围-1000000 <= a, b <= 1000000;

    3.按指定格式输出结果

    例:-100000 9

    输出: -99,991

    解题思路:

    1.明确范围

        a+b在正负两百万范围内, 32位系统int类型占4字节精度够

    2.明确要求:

       ① 输入以空格分割, 输入整数

       ②结果如果数字大于4位, 需要每三位用逗号分割

     ③视算法可能有需要补零的情况(我就是踩的这个坑)

     ④正负号提前判定, 便于后面处理

        ⑤函数要以return 0; 结束(这是我提交代码后发现的)

     3.采取措施:

        ①将相加的结果循环对1000取余, 余数存在数组里

     ②输出数组中数字的最高位(最高位不存在需要补零的情况)

     ③ 用printf("%03d"),   实现补零。

    4.潜在问题:

      ①视代码的具体实现方式可能在处理0的时候会出问题;

      ②对应措施:打完代码特别观察一下0的情况,并手测数据即可

    5、提交后仍存在的bug

      无;

    源代码:

    #include<stdio.h>
    int main() 
    {
    
    	int a=0, b=0, sum=0;
    	int format[10] = {0};
    	int i = 0;
    	scanf("%d %d", &a, &b);
    	sum = a+b;
    	if (sum < 0) {
    		printf("-");
    		sum = -sum;
    	}
    
    	while((sum/1000) >0) {
    		format[i] = sum%1000;
    		sum = sum/1000;
    		i++;
    	}
    
    	format[i] = sum;
    	for (printf("%d", format[i]), i--; i>=0;i--) {
    		printf(",%03d", format[i]);
    		
    	}
    	return 0;
    }
    

      

    结果截图:

    本文撰文格式和部分代码参考:http://www.cnblogs.com/andwho/p/5161998.html

    十分感谢!

  • 相关阅读:
    poj 1579(动态规划初探之记忆化搜索)
    hdu 1133(卡特兰数变形)
    CodeForces 625A Guest From the Past
    CodeForces 625D Finals in arithmetic
    CDOJ 1268 Open the lightings
    HDU 4008 Parent and son
    HDU 4044 GeoDefense
    HDU 4169 UVALive 5741 Wealthy Family
    HDU 3452 Bonsai
    HDU 3586 Information Disturbing
  • 原文地址:https://www.cnblogs.com/tpwBlog/p/8893774.html
Copyright © 2011-2022 走看看