iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 27
1
自我挑戰組

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

[Day 27] 週五寫一波 code !

  • 分享至 

  • twitterImage
  •  

這次來寫寫 UVA10018: Reverse and Add
題意大概是這樣
把一個數字反轉並相加,直到數字是迴文,然後輸出加了幾次跟該迴文,以題目的 sample 來看看好了

  1. 195(最開始)+591=786
  2. 786+687=1473
  3. 1473+3741=5214
  4. 5214+4125=9339(迴文)

所以輸出 4 9339

#include <stdio.h>
int numlen(int n){ //計算該數字位數
    int l=1,i;  
    for(i=0;i<4;i++){  
        if( (n/10) > 0 ) l++;  
        else break;  
        n /= 10;  
    }  
    return l;  
}
int backtofront(int n){
	int out=0,i;
	while(n/10>0){
		out *= 10;
		out += n % 10;
		n /= 10;
	}
	out *= 10;
	out += n % 10;
	return out;
}
int comparenn(int n){
	int i,bn=backtofront(n);
	for(i=0;i<=(numlen(n)/2);i++){
		if(bn%10!=n%10)	return 0;
		bn /= 10;
		n /= 10;
	} 
	return 1;
}
int main(){
    int n,i;
    scanf("%d",&n);
    for(i=0;i<n;i++){
    	int num,st=1;
    	scanf("%d",&num);
    	num += backtofront(num);
    	while(comparenn(num)==0){
    		num += backtofront(num);
    		st++;
		}
		printf("%d %d\n",st,num);
	}
    return 0;
}

上一篇
[Day 26] 第四堂程式設計
下一篇
[Day 28] 大一的我怎麼申請上 DSC Program 的?
系列文
資工系大一課程/日常筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言