iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 8
1
自我挑戰組

資工系大一課程/日常筆記系列 第 8

[Day 8] 連假最後寫程式

  • 分享至 

  • twitterImage
  •  

最後一天來寫寫 UVA10812: Beat the Spread!
題目會輸入兩數,分別是兩組分數相加與相差,試找出兩組分數,找不到則輸出"impossible"
設兩組分數為 x 跟 y
於是我先算出 x+y=add 跟 x-y=dif 的 x 跟 y
x=(add+dif)/2
y=(add-dif)/2

#include <stdio.h>
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		if(a<b||(a+b)%2==1||(a-b)%2==1) printf("impossible\n"); //不可能的組合
		else if(a==b) printf("%d 0\n",a);
		else printf("%d %d\n",((a+b)/2),((a-b)/2));
	}
	return 0;
}

上一篇
[Day 7] 中秋連假第二天寫程式~
下一篇
[Day 9] 第二堂微積分
系列文
資工系大一課程/日常筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言