Volume 1. Elementary Problem Solving :: Big Number Uva 424,10106,465,748,10494

刘汝佳 算法入门 第一版 Uva题目集合(三)


Uva 424

#include <stdio.h>
#include <string.h>
const int MAXN=110;

int main(){
	int sum[MAXN];
	char temp[MAXN];
	int len,i;
	int max_len=0;
	memset(sum,0,sizeof(sum));
	while(scanf("%s",temp)){
	   if (temp[0]=='0'&&temp[1]=='\0') break;
	   len=strlen(temp);
	   for (i=0;i<len;i++)  
		   temp[i]-=48;
	   int k=0,s=0,c=0;
	   i=len-1;
	   do{
   	       s=temp[i--]+sum[k]+c;
		   sum[k++]=s%10;
		   c=s/10;	   	
   	   }while(c>0||i>=0);
	   if(k>max_len) max_len=k;	   
	} 
    for (i=max_len-1;i>=0;i--)
    printf("%d",sum[i]);
		 printf("\n");	
	return 0;
}


Uva 10106

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char a[255],b[255];
int x[255],y[505],z[505];
int main(){
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    while(scanf("%s%s",a,b)!=EOF){
        int a_len=strlen(a);
        int b_len=strlen(b);
        int max=0;
        memset(z,0,sizeof(z));
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        for(int i=a_len-1;i>=0;i--){
            x[a_len-i-1]=a[i]-'0';
        }
        for(int i=b_len-1;i>=0;i--){
            y[b_len-i-1]=b[i]-'0';
        }
        for(int i=0;i<a_len;i++){
            int l=i;
            int c=0,s;
            for(int j=0;j<b_len||c>0;j++){
                s=z[l]+y[j]*x[i]+c;
                z[l++]=s%10;
                c=s/10;
            }
            if(max<l) max=l;
        }
        while(!z[--max]&&max);
        for(int i=max;i>=0;i--)
            printf("%d",z[i]);
         putchar('\n');
    }
    return 0;

}


Uva 465

#include <stdio.h>

const int MINT = 0x7fffffff;

int main() {
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif
    char str1[1000], str2[1000], ch;

    while (scanf("%s %c %s", str1, &ch, str2) != EOF) {

        printf("%s %c %s\n", str1, ch, str2);

        double a, b;
        sscanf(str1, "%lf", &a);
        sscanf(str2, "%lf", &b);

        if (a > MINT)
            printf("first number too big\n");
        if (b > MINT)
            printf("second number too big\n");
        if ('+'==ch && a+b>MINT)
            printf("result too big\n");
        if ('*'==ch && a*b>MINT)
            printf("result too big\n");
    }

    return 0;
}


Uva 748

#include <stdio.h>
#include <string.h>
 
const int maxn = 500;
 
void Mul(char *str1, char *str2, char *str3){
    int  i, l, i1, i2, tmp, carry, jj;
    int  len1 = strlen(str1), len2 = strlen(str2);
    char  ch;
    jj = carry = 0;
    for (i1=len1-1; i1 >= 0; --i1){
        l = jj;
        for (i2=len2-1; i2 >= 0; --i2, ++l){
            tmp = (str3[l]-'0')+(str1[i1]-'0')*(str2[i2]-'0')+carry;
            carry = tmp/10;
            str3[l] = tmp%10+'0';
        
        }
         if (carry) {
            str3[l] = carry+'0';
            carry = 0;
            ++l;
        }
        ++jj;
    }
    l--;
    while (str3[l] == '0' && l > 0) --l;
    str3[++l] = '\0';
    for (i=0, --l; i < l; ++i, --l){
        ch = str3[i];
        str3[i] = str3[l];
        str3[l] = ch;
    }
}
 
int main() {
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif

    char num[maxn], res[maxn], t[maxn];
    int n;
    while (1) {
        int p, tmp, i = 0;
        int j;
        while ((tmp = getchar()) != EOF && tmp != ' ')
            if (tmp == '.') {
                p = i;
            } else {
                t[i]= num[i] = tmp;
                i++;
            }
        
        for (j = i - 1; j >= 0; j--) {
            if (num[j] != '0') {
                i = j + 1;
                break;
            }
        }
        t[i] = num[i] = '\0';
        if (scanf("%d", &n) == EOF) {
            break;
        }
        p = (strlen(num) - p) * n;
        getchar();
        memset(res, '0', sizeof(res));
        for (i = 0; i < n - 1; i++) {
            Mul(num, t, res);
            if (i != n - 2) {
                for (j = 0; j < maxn; j++) {
                    t[j] = res[j];
                    res[j] = '0';
                }
            }
        }
        if (p >= strlen(res)) {
            printf(".");
            for (i = 0; i < p - strlen(res); i++) {
                printf("0");
            }
            printf("%s\n", res);
        } else {
            for (i = 0; i < strlen(res) - p; i++) {
                printf("%c", res[i]);
            }
            printf(".");
            printf("%s\n", res + strlen(res) - p);
        }
    }
    return 0;
}


Uva 10494

#include <cstdio> 
#include <cstring> 
const int MAXN=10000000; 
char s[MAXN],ans[MAXN]; 
int main() {
#ifndef ONLINE_JUDGE  
    freopen("input.txt","r",stdin);  
    freopen("output.txt","w",stdout);  
#endif 
    char c; 
    long long b,a; 
    memset(s,0,MAXN); 
    while(scanf("%s %c %lld",s,&c,&b)!=EOF) { 
        memset(ans,0,MAXN); 
        int i; 
        a=0; 
        int j=0; 
        int len=strlen(s); 
        for(i=0;i<len;++i) { 
            a=a*10+s[i]-'0'; 
            ans[j++]=a/b+'0'; 
            a=a%b; 
        } 
        if(c=='%') 
            printf("%lld\n",a); 
        else { 
            i=0; 
            while(ans[i]=='0')++i; 
            if(ans[i]==0)--i; 
            for(;i<=j-1;++i) 
                printf("%c",ans[i]); 
            printf("\n"); 
        } 
    } 
    return 0; 
}  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值