1001. A+B Format (20)[C语言]

1001. A+B Format (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 <stdio.h>
#include <stdlib.h>
#include <math.h>

void print1(int sum){
    if(sum/-1000 >= 1){
             print1(-(sum-sum%1000)/-1000);
             int r = (int)fabs(sum%(-1000));
             if(r!=0) {
                 if(r< 10){
                     printf(",00%d",-(sum%-1000));
                }else if(r < 100){
                     printf(",0%d",-(sum%-1000));
                }else{
                    printf(",%d",-(sum%-1000));
                }
            } 
             else  printf(",000");
    }else{
            printf("%d",sum);
    }
}


void print2(int sum){
    if(sum/1000 >= 1){
             print2((sum-sum%1000)/1000);
             int r = sum%1000 ;
         
             if(r!=0){
                 if(r < 10){
                     printf(",00%d",sum%-1000);
                }else if(r < 100){
                     printf(",0%d",sum%-1000);
                }else{
                    printf(",%d",sum%-1000);
                }
             } 
             else  printf(",000");
    }else{
            printf("%d",sum);
    }
}


int main(){
    int a , b ,sum; 
    scanf("%d %d",&a,&b);
    sum = a+b;
    if(sum < 0)    print1(sum);
    else print2(sum);
    
    return 0 ;
} 


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常抱歉,我之前的回答是基于Python的。以下是等效的C语言实现: ```c #include <stdio.h> unsigned long long pow_mod(unsigned long long base, unsigned long long exponent, unsigned long long modulus) { unsigned long long result = 1; base %= modulus; while (exponent > 0) { if (exponent % 2 == 1) result = (result * base) % modulus; base = (base * base) % modulus; exponent /= 2; } return result; } unsigned long long calculate_expression(unsigned long long n, unsigned long long A, unsigned long long K, unsigned long long a, unsigned long long b, unsigned long long m, unsigned long long P) { // 计算函数f(x)的值 unsigned long long f[n+1]; f[1] = K; for (unsigned long long x = 2; x <= n; x++) { f[x] = (a*f[x-1] + b) % m; } // 计算表达式的结果 unsigned long long result = 0; for (unsigned long long x = 1; x <= n; x++) { result += pow_mod(A, f[x], P); result %= P; } return result; } int main() { unsigned int T; scanf("%u", &T); for (unsigned int case_num = 1; case_num <= T; case_num++) { unsigned long long n, A, K, a, b, m, P; scanf("%llu %llu %llu %llu %llu %llu %llu", &n, &A, &K, &a, &b, &m, &P); unsigned long long ans = calculate_expression(n, A, K, a, b, m, P); printf("Case #%u: %llu\n", case_num, ans); } return 0; } ``` 请注意,此代码假设输入的整数不会超出unsigned long long类型的范围。如果您的输入可能超出该范围,请相应地更改数据类型。 希望这个C语言实现对您有所帮助!如果您还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值