(C语言)A+B Again

Your task is to Calculate A+B. But this time, both A and B represents a time. For instance, if we define A as 12 12 12, indicating twelve hours twelve minutes and twelve seconds.

输入:

There are multiple test cases. The first line of the input is an integer T (1 ≤ T ≤ 200), indicating the number of test cases. Then T test cases follow.
For each case, each line consists of six integers a1​,a2​,a3​ and b1​,b2​,b3​, separated by a space. The first three integers indicate time A, and the last three integers indicate time B.

输出:

For each test case, output one line containing three integers, indicating A+B. Each output consists of three parts, hour, minute and second. And it should be obey the regulations of time, it means that the number of minute or second should not exceed 60. You should output the sum of A and B in one line.

解决代码如下:

#include <stdio.h>
 
int main()
{
    int a,b,a1,a2,a3,b1,b2,b3;
    int s,f,m;
    b=1;
    scanf("%d",&a);
    while(b<=a){
        scanf("%d%d%d%d%d%d",&a1,&a2,&a3,&b1,&b2,&b3);
        m=a3+b3;
        f=b2+a2;
        s=a1+b1;
        if(m>=60){
            m=m-60;
            f=f+1;
        }
        if(f>=60){
            f=f-60;
            s=s+1;
        }
        printf("%d %d %d\n",s,f,m);
        b++;    
    }
    return 0;
}

### C语言中实现两个数字交换的方法 在C语言中,可以通过多种方式来实现两个数字的交换操作。以下是几种常见的方法及其具体实现: #### 方法一:通过临时变量实现 这是最直观的一种方法,利用一个额外的临时变量存储其中一个数值,在完成赋值后再释放该临时变量的空间。 ```c #include<stdio.h> int main(){ int a = 10, b = 20; int temp; temp = a; a = b; b = temp; printf("交换后:a=%d , b=%d\n", a, b); return 0; } ``` 这种方法的优点在于逻辑清晰易懂,缺点是需要额外的内存空间用于保存中间数据[^1]。 #### 方法二:算术运算(加减法) 此方法无需借助第三个变量即可完成两数之间的互换工作。它主要依赖于基本数学原理中的加法规则来进行处理。 ```c #include<stdio.h> int main(){ int a=10,b=20; a=a+b;//假设此时a=30 (即原a+b的结果) b=a-b;//现在b等于原来的a(因为a已经包含了原来ab之和而这里又减去了当前新的b也就是原先的b所以得到的是原始状态下的a) a=a-b;//同理可得此处计算出来的就是最初的b printf("%d,%d\n",a,b); } ``` 需要注意的是如果初始值过大可能会引起溢出问题从而导致错误结果出现因此适用范围有限制条件[^2]。 #### 方法三:按位异或(XOR) 运算符 ^ 这种技术同样不需要任何辅助性的储存单元就能达到目的而且效率非常高因为它只涉及到简单的比特级操作而已没有任何复杂的浮点或者整型转换过程参与其中。 ```c void swap(int *x,int*y){ if(x != y){ //防止自指针情况发生造成不可预期的行为 (*x)^=*y,*y^=*x,*x^=*y;} } //调用示例 int main() { int num1 = 7,num2 =9 ; printf("Before Swapping:\nNum1:%d Num2 :%d ",num1 ,num2 ); swap(&num1,&num2); printf("\nAfter Swapping :\nNum1:%d Num2 :%d ",num1 ,num2 ); } ``` 上述代码片段展示了如何定义并运用函数形式执行XOR-based swapping mechanism.[^3] #### 方法四:基于移位操作的位运算 这是一种特殊情况下使用的技巧,尤其适合那些希望减少CPU指令周期消耗的应用场景下考虑采用这种方式来做同样的事情——不过要注意它的局限性就在于对于特别巨大的数值来说可能并不合适由于存在潜在的数据丢失风险等因素影响最终效果表现不佳等问题存在. ```c #include <stdio.h> int main(void) { unsigned char a,b; scanf("%hhu %hhu",&a,&b); /* Shift operations */ a <<=8 ;/* Move 'a' into higher byte*/ a +=b;/* Add lower order bits of both numbers together.*/ b =(unsigned)(a >>8);/* Extract upper part back to variable b again .*/ a &=0xFF;/* Mask off everything except last eight bitsof combined value stored inside A now */ printf("Swapped values are ::A->%u B ->%u \n",a,b); return 0; } ``` 这段程序说明了当面对较小规模正整型数据类型时可以尝试应用此类算法思路解决问题的同时也要意识到其特定前提约束条件下才能正常发挥作用的特点所在[^4]. ### 总结 综上所述,C编程环境下提供了多样化的途径可供开发者选择用来解决双元素间相互置换的需求.每种方案各有优劣之处需根据实际情况灵活选用最为恰当的那个选项才是明智之举.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值