有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
Some parts use both Chinese and English, but some parts use only one language. Feel free to share, but please contact me first and list this blog as your reference.

2014年2月28日 星期五

UVa OJ - 993 Product of digits

The following program is my ACcepted code for UVA-993.
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know.  :D

此乃UVA 993 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )

點這裡看題目 Click here to see this Problem!


//This program is for UVA 993 Product of digits
//題目來源 Problem link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=934


#include<stdio.h>
using namespace std;

void findAns(int n)
{
    int ans[12];
    int count = 0;

    if(n == 0)// I think 0 is not a natural Q
    {
        printf("-1\n");
        return;
    }
 
    if(n == 1)
    {
        printf("1\n");
        return;
    }
 
    for(int i = 9; i >= 2; i--)
    {
        while( (n%i) == 0)// when n has a factor i
        {
            n /= i;
            ans[count ++] = i;
         
            if(n == 1)//when all the factors were divided from n
                break;
        }
    }
 
    if(n !=  1)//the n has a factor above 9
    {
        printf("-1\n");
        return;
    }
 
    for(int i = count - 1; i >= 0; i--)
        printf("%d", ans[i]);
    printf("\n");
 
    return;
}

int main()
{
    int n, m;
 
    while(scanf("%d", &n) != EOF)
    {
        while(n--)
        {
            scanf("%d", &m);
            findAns(m);
        }
    }
    return 0;
}

Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D 

歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )

2 則留言:

  1. 若可以把題目一起貼上來 會更棒喔!!!

    回覆刪除
    回覆
    1. 其實我有放題目網址在上面~ 不過應該不太明顯XD
      我已經加上的題目連結了!
      謝謝你的建議~

      刪除

請留下您的任何想法或建議!
Please leave any thought or comment!