OpenJudge百炼-2952-循环数 & poj-1047-Round and Round We Go-C语言-高精度计算

来源:互联网 发布:php判断是否为整数 编辑:程序博客网 时间:2024/06/05 00:25

OpenJudge百炼-2952-循环数,挺好的一道题就这么废了,判断乘二就可以了

描述:
若一个n位的数字串满足下述条件,则称其是循环数(cyclic):将这个数字串视为整数(可能带有前导0),并用任意一个 1 到 n 之间(包含1和n)的整数去乘它时, 会得到一个将原数字串首尾相接后,再在某处断开而得到的新数字串所对应的整数。例如,数字 142857 是循环数,因为: 
142857 *1 = 142857 
142857 *2 = 285714 
142857 *3 = 428571 
142857 *4 = 571428 
142857 *5 = 714285 
142857 *6 = 857142。
请写一个程序判断给定的数是否是循环数。
注意:在此题中,输入数字串允许带前导0,且前导0不能被忽略,例如“01”是两位数字串,而“1”是一位数字串。但将数字串转化为整数做乘法运算或比较运算时,可以忽略前导0。
输入:
一行,一个长度在 2 到 60 位之间的数字串。
输出:
一个整数,若输入的数字串是循环数,输出1,否则输出0。
样例输入:
142857
样例输出:
1

/***************************************************文件名:百炼-2952**Copyright (c) 2015-2025 OrdinaryCrazy**创建人:OrdinaryCrazy**日期:20170910**描述:百炼2952参考答案**版本:1.0**************************************************/#include <stdio.h>#include <string.h>char n[61];int num[60],len,num_loop[60][60],res[61];void multiply(int a)//将num乘a存在res中{    int i;    for(i = 0;i < len;i++)        res[i] = num[i] * a;    for(i = 0;i < len;i++)    {        res[i+1] += res[i] / 10;        res[i] %= 10;    }}int num_compare()//查看num_loop中是否有与res相同的数,如果有返回1,否则返回0{    int i,j;    for(i = 0;i < len;i++)    {        for(j = 0;j < len;j++)            if(num_loop[i][j] != res[j])                break;        if(j == len)            break;    }    if(i < len) return 1;    return 0;}int main(){    int i,j;    scanf("%s",n);    len = strlen(n);    for(i = len-1,j=0;i >= 0;i--,j++)        num[j] = n[i] - '0';    for(i = 0;i < len;i++)        num_loop[0][i] = num[i];    for(i = 1;i < len;i++)    {        int tmp = num_loop[i-1][0];        for(j = 0;j < len-1;j++)            num_loop[i][j] = num_loop[i-1][j+1];        num_loop[i][j] = tmp;    }    multiply(2);    num_compare() ? printf("%d",1) : printf("%d",0);    return 0;}

poj-1047-Round and Round We Go,这才是原题:

Description:
A cyclic number is an integer n digits in length which, when multiplied by any integer from 1 to n, yields a"cycle"of the digits of the original number. That is, if you consider the number after the last digit to "wrap around"back to the first digit, the sequence of digits in both numbers will be the same, though they may start at different positions.For example, the number 142857 is cyclic, as illustrated by the following table: 

142857*1 = 142857 
142857 *2 = 285714 
142857 *3 = 428571 
142857 *4 = 571428 
142857 *5 = 714285 
142857 *6 = 857142 
Input:
Write a program which will determine whether or not numbers are cyclic. The input file is a list of integers from 2 to 60 digits in length. (Note that preceding zeros should not be removed, they are considered part of the number and count in determining n. Thus, "01"is a two-digit number, distinct from "1" which is a one-digit number.)
Output:
For each input integer, write a line in the output indicating whether or not it is cyclic.
Sample Input:
142857
142856
142858
01
0588235294117647
Sample Output:
142857 is cyclic
142856 is not cyclic
142858 is not cyclic
01 is not cyclic
0588235294117647 is cyclic

#include <stdio.h>#include <string.h>char n[61];int num[60],len,num_loop[60][60],res[61];void multiply(int a)//将num乘a存在res中{    int i;    for(i = 0;i < len;i++)        res[i] = num[i] * a;    for(i = 0;i < len;i++)    {        res[i+1] += res[i] / 10;        res[i] %= 10;    }}int num_compare()//查看num_loop中是否有与res相同的数,如果有返回1,否则返回0{    int i,j;    for(i = 0;i < len;i++)    {        for(j = 0;j < len;j++)            if(num_loop[i][j] != res[j])                break;        if(j == len)            break;    }    if(i < len) return 1;    return 0;}int main(){    int i,j;    while(scanf("%s",n) != EOF)    {        len = strlen(n);        for(i = len-1,j=0;i >= 0;i--,j++)            num[j] = n[i] - '0';        for(i = 0;i < len;i++)        num_loop[0][i] = num[i];        for(i = 1;i < len;i++)        {            int tmp = num_loop[i-1][0];            for(j = 0;j < len-1;j++)                num_loop[i][j] = num_loop[i-1][j+1];            num_loop[i][j] = tmp;        }        for(i = 2;i <= len;i++)        {            multiply(i);            if(num_compare() == 0)                break;        }        i == len+1 ? printf("%s is cyclic\n",n) : printf("%s is not cyclic\n",n);    }    return 0;}


原创粉丝点击