PAT:A1001. A+B Format (20/20)

本文介绍了一个C语言程序,该程序接受两个整数作为输入,并以标准格式输出它们的和,即每三位数字用逗号分隔。讨论了处理负数和不同位数数字的细节。

1、0需要特判;

2、开始提交的代码中没有考虑好个位数的情况;


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

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

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

#include <cstdio>
#include <cstring>

int main()
{
    int a,b,sum,i=0;
    int ass[10];
    scanf("%d %d",&a,&b);
    sum=a+b;
    if(sum<0){
        printf("-");
        sum=-sum;
    }
    do{
        ass[i++]=sum%10;
        sum/=10;
    }while(sum!=0);
    if(ass[i-1]==0){
        printf("0");
        i=-1;
    }else if(i%3==2){
        printf("%d%d,",ass[i-1],ass[i-2]);
        i-=3;
    }else if(i%3==1){
        printf("%d,",ass[i-1]);
        i-=2;
    }else{
        i--;
    }
    for(;i>=0;){
        for(int j=0;j<3;j++){
            printf("%d",ass[i]);
            i--;
        }
        if(i>1)printf(",");
    }
    return 0;
}
正确提交
#include <cstdio>
#include <cstring>

int main()
{
    int a,b,sum,i=0;
    int ass[10];
    scanf("%d %d",&a,&b);
    sum=a+b;
    if(sum<0){
        printf("-");
        sum=-sum;
    }
    do{
        ass[i++]=sum%10;
        sum/=10;
    }while(sum!=0);
    if(ass[i-1]==0){
        printf("0");
        i=-1;
    }else if(i%3==2){
        printf("%d%d",ass[i-1],ass[i-2]);
        if(i>3)printf(",");
        i-=3;
    }else if(i%3==1){
        printf("%d",ass[i-1]);
        if(i>3)printf(",");
        i-=2;
    }else{
        i--;
    }
    for(;i>=0;){
        for(int j=0;j<3;j++){
            printf("%d",ass[i]);
            i--;
        }
        if(i>1)printf(",");
    }
    return 0;
}

/** * 键值型数据库 */ import { promptAction } from '@kit.ArkUI'; import CommonConstants from '../common/constans/CommonConstants'; import { distributedKVStore } from '@kit.ArkData'; import { BusinessError } from '@kit.BasicServicesKit'; let kvManager: distributedKVStore.KVManager | undefined = undefined; let kvStore: distributedKVStore.SingleKVStore | undefined = undefined; let resultData: boolean | string | number | Uint8Array = '' const options: distributedKVStore.Options = { createIfMissing: true, encrypt: false, backup: false, autoSync: false, kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, securityLevel: distributedKVStore.SecurityLevel.S1 }; class KVManager { //获取一个KVManager实例,创建并获取键值数据库 //TODO //2、调用get()方法获取指定键的值(查询数据) // TODO // 3、调用delete()方法删除指定键值的数据(删除数据) // TODO //创建数据库 // TODO //信息提示 showToastMessage(message: Resource) { promptAction.showToast({ message: message, duration: CommonConstants.DURATION }); }; } export default new KVManager()以上为文件代码,以下为辅助文字和代码,2.创建并获取键值数据库 在获取KVManager实例对象后,可以通过getKVStore函数来创建和获取KV-Store对象。getKVStore函数需要传入两个参数:第一个参数为storeId,是KV-Store的唯一标识,需要在应用程序内全局唯一;第二个参数为options, options是一个KVManagerConfig类型的对象,包含5个属性。 createIfMissing:当数据库文件不存在时是否创建数据库,默认为true,即不存在则创建。 encrypt:设置数据库文件是否加密,默认为false(不加密) backup:设置数据库文件是否备份,默认为true(开启加密) kvStoreType:设置要创建数据库的类型,默认为DEVICE_COLLABORATION,表示多设备协同数据库。还支持SINGLE_VERSION,表示单版本数据库类型。 securityLevel:设置数据库的安全级别,支持S1到S4四个级别,S4级别最高。应用程序可以根据数据重要性设置数据安全级别。 autoSync: 设置数据同步 注意:在创建options参数时需要注意,如果修改了options参数属性的值,则将导致之前创建的数据库无法打开或者数据无法读取。 let kvStore: distributedKVStore.SingleKVStore | undefined = undefined; try { const options: distributedKVStore.Options = { createIfMissing: true, encrypt: false, backup: false, autoSync: false, // kvStoreType不填时,默认创建多设备协同数据库 kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION, //多设备协同数据库:kvStoreType: distributedKVStore.KVStoreType.DEVICE_COLLABORATION, securityLevel: distributedKVStore.SecurityLevel.S1 }; kvManager.getKVStore<distributedKVStore.SingleKVStore>('storeId', options, (err, store: distributedKVStore.SingleKVStore) => { if (err) { console.error(`Failed to get KVStore: Code:${err.code},message:${err.message}`); return; } console.info('Succeeded in getting KVStore.'); kvStore = store; // 请确保获取到键值数据库实例后,再进行相关数据操作 }); } catch (e) { let error = e as BusinessError; console.error(`An unexpected error occurred. Code:${error.code},message:${error.message}`); }帮我 完成获取一个KVManager实例,创建并获取键值数据库
05-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值