UVa 202 大數除法,uva202大數除法

來源:互聯網
上載者:User

UVa 202 大數除法,uva202大數除法

背景:1_WA:忘了每個答案之間有一個空白行!2_WA:沒看見等號左右兩邊都有空格!!!!!!!!

思路:整數和小數分開來求,整數部分直接用整型除法,小數部分:分子=(分子%分母)*10.並且把每個分子儲存在str[0]中,當出現已經出現過的分子時,小數部分開始迴圈!

學習:

1.巢鴿原理(抽屜原理,狄利克雷原則):

 簡單的描述:如果有n個笼子,n+1隻鴿子居住,則至少有一個笼子有兩隻鴿子。

一般化的描述(用高斯函數來敘述):將n個元素分到m個集合中,至少有一個集合中元素個數大於等於[(n-1)/m]+1 。

對於本題,a%b最多有b-1個情況,根據巢鴿原理,迴圈節數小於等於b-1.

#include<stdio.h>int str[2][10000];  int main(void){  int a,b,intger;  while(scanf("%d %d",&a,&b)!=EOF){  int count=0,start,aa=a;  intger=a/b;  a=a%b;  while(1){  a*=10;  for(int i=0;i < count;i++){  if(a==str[0][i]){  start=i;            goto l1;  }  }  str[0][count]=a;  str[1][count++]=a/b;  a=a%b;  }l1:   printf("%d/%d = %d.",aa,b,intger);      for(int i=0;i < start;i++){      printf("%d",str[1][i]);      }             if(count<=50){      printf("(");      for(int i=start;i < count;i++){      printf("%d",str[1][i]);      }      printf(")\n");      }else{      printf("(");      for(int i=start;i < start+50;i++){      printf("%d",str[1][i]);      }      printf("...)\n");      }      printf("   %d = number of digits in repeating cycle\n\n",count-start);  }  return 0;} 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.