題目1125:大整數的因子 C++/Java

來源:互聯網
上載者:User

標籤:online judge   九度   編程   c++   java   

題目描述:

已知正整數k滿足2<=k<=9,現給出長度最大為30位的十進位非負整數c,求所有能整除c的k.

輸入:

若干個非負整數c,c的位元<=30
每行一個c,當c=-1時中止
(不要對-1進行計算!)

輸出:

每一個c的結果佔一行
1) 若存在滿足 c%k == 0 的k,輸出所有這樣的k,中間用空格隔開,最後一個k後面沒有空格。
2) 若沒有這樣的k則輸出"none"

範例輸入:
307213-1
範例輸出:
2 3 5 62 3 4 6 8 9none
提示:

注意整數溢出問題
不要對-1進行計算



C++代碼:

#include<iostream>#include<string>#include<vector>using namespace std; int main(){    string s;    while(cin>>s&&s!="-1")    {        vector<int> ivec;        for(int n=2;n<=9;++n)        {            int index=0;            int temp;            for(int i=0;i<s.size();++i)            {                temp=index*10+(s[i]-'0');                index=temp%n;            }            if(index==0)                ivec.push_back(n);        }        if(ivec.empty())            cout<<"none"<<endl;        else        {            cout<<ivec[0];            for(int i=1;i<ivec.size();++i)                cout<<" "<<ivec[i];            cout<<endl;        }    }    return 0;}/**************************************************************    Problem: 1125    User: Carvin    Language: C++    Result: Accepted    Time:150 ms    Memory:1520 kb****************************************************************/

Java代碼:

import java.util.Scanner;public class Main{public static void main(String[] args){String str;int i,j,k;//int array[]=new int[100];Scanner cin=new Scanner(System.in);while(cin.hasNext()){str=cin.nextLine();boolean flag=false;char num[]=str.toCharArray();int len=num.length;j=0;if('-'==(num[0]))break;//System.exit(1);for(k=2;k<=9;k++){int temp;int index=0;for(i=0;i<len;i++){temp=index*10+(num[i]-'0');index=temp%k;}if(index==0){//array[j++]=k;if(k==2)System.out.print(k);elseSystem.out.print(" "+k);flag=true;}}if(!flag)System.out.print("none");System.out.println();}}}/**************************************************************    Problem: 1125    User: Carvin    Language: Java    Result: Accepted    Time:150 ms    Memory:1520 kb****************************************************************/



題目1125:大整數的因子 C++/Java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.