C语言-翁恺-PTA-41-80课后练习题-02


title: C语言-翁恺-PTA-41-80课后练习题-02
tags:

  • PTA
  • C
    description: ’ ’
    mathjax: true
    date: 2024-04-02 11:50:25
    categories:
  • PTA
  • C

第二个40题,还是

7-42 整除光棍

这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1、11、111、1111等。传说任何一个光棍都能被一个不以5结尾的奇数整除。比如,111111就可以被13整除。 现在,你的程序要读入一个整数x,这个整数一定是奇数并且不以5结尾。然后,经过计算,输出两个数字:第一个数字s,表示x乘以s是一个光棍,第二个数字n是这个光棍的位数。这样的解当然不是唯一的,题目要求你输出最小的解。

提示:一个显然的办法是逐渐增加光棍的位数,直到可以整除x为止。但难点在于,s可能是个非常大的数 —— 比如,程序输入31,那么就输出3584229390681和15,因为31乘以3584229390681的结果是111111111111111,一共15个1。

输入格式:

输入在一行中给出一个不以5结尾的正奇数x(<1000)。

输出格式:

在一行中输出相应的最小的sn,其间以1个空格分隔。

输入样例:

31

输出样例:

3584229390681 15

代码长度限制

错误原因

7-42这题也是一点思路都没有,这种是我最讨厌的题,没有思路,就不太会写。

这题数学题,我确实是笨蛋,一点点思路都没有

好吧,但是实际做起来,比我想象中的简单,也只要通过遍历即可,

11,111,1111,这样加上去,代码如下:

别人的代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    int x, y = 1;
    scanf("%d", &x);    //读取X
    int s = 1;         //用来表示光棍数
    int n = 0;         //记录位数
    while (s < x) {
        s = s * 10 + 1;  // 先找出大于x的最小光棍数
        n++;
    }
    do {
        y = s % x;  // 储存余数
        printf("%d", s / x);
        s = y * 10 + 1;  // 更新光棍数
        n++;
    } while (y != 0);  // 如果余数为0,说明找到了光棍数字,直接结束循环
    printf(" %d", n);  // 输出位数
    return 0;
}

7-43 Shuffling Machine

看这个B题目,和看天书一样,我真的服我自己,英语学的别太烂

下面是原来的题目,我会通过翻译软件翻译一下

Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid “inside jobs” where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

S1, S2, ..., S13, 
H1, H2, ..., H13, 
C1, C2, ..., C13, 
D1, D2, ..., D13, 
J1, J2

where “S” stands for “Spade”, “H” for “Heart”, “C” for “Club”, “D” for “Diamond”, and “J” for “Joker”. A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer K (≤20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.

Sample Input:

2
36 52 37 38 3 39 40 53 54 41 11 12 13 42 43 44 2 4 23 24 25 26 27 6 7 8 48 49 50 51 9 10 14 15 16 5 17 18 19 1 20 21 22 28 29 30 31 32 33 34 35 45 46 47

Sample Output:

S7 C11 C10 C12 S1 H7 H8 H9 D8 D9 S11 S12 S13 D10 D11 D12 S3 S4 S6 S10 H1 H2 C13 D2 D3 D4 H6 H3 D13 J1 J2 C1 C2 C3 C4 D1 S5 H5 H11 H12 C6 C7 C8 C9 S2 S8 S9 H10 D5 D6 D7 H4 H13 C5

代码长度限制

16 KB

时间限制

400 ms

内存限制

错误原因

不会看题,题目看不懂

Shuffling Machine

洗牌机

Shuffling is a procedure used to randomize a deck of playing cards.洗牌是将一副扑克牌随机化的过程。

Because standard shuffling techniques are seen as weak,

因为标准的洗牌技术被认为很薄弱、

and in order to avoid “inside jobs” where employees collaborate with gamblers by performing inadequate shuffles

以及为了避免员工通过不适当的洗牌与赌徒合作的 “内部工作”

many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

许多赌场都使用自动洗牌机。您的任务就是模拟洗牌机。

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

该机器按照给定的随机顺序洗一副 54 张牌,并重复给定的次数。假设一副牌的初始状态按以下顺序排列:

where “S” stands for “Spade”, “H” for “Heart”, “C” for “Club”, “D” for “Diamond”, and “J” for “Joker”. A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

其中 "S "代表 “黑桃”,"H "代表 “红心”,"C "代表 “梅花”,"D "代表 “方块”,"J "代表 “小丑”。给定的顺序是[1, 54]中不同整数的排列。例如,假设我们只有 5 张牌: S3、H5、C1、D13 和 J2。给定一个洗牌顺序{4,2,5,3,1},结果将是 J2、H5、D13、S3、C1。如果我们再重复一次洗牌,结果将是 C1、H5、S3、J2、D13。

这里很恶心的这个B翻译软件,有一句话,没有翻译出来

If the number at the i-th position is j, it means to move the card from position i to position j.

如果第 i 个位置的数字是 j,则表示将牌从第 i 个位置移动到第 j 个位置。这一句话,可以不翻译的是吧,B翻译软件

做反而是好做的

代码如下:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {
    int card[55] = { 0 }, pcard[55] = { 0 }, random[55] = { 0 },k=0;
    for (int i = 0; i < 55; ++i)   card[i] = i;
    scanf("%d", &k);
    for (int i = 1; i <= 54; ++i)
        scanf("%d", &random[i]);
    while (k) {
        for (int i = 1; i <= 54; ++i) {
            pcard[random[i]] = card[i];
        }
        for (int i = 1; i <= 54; ++i)
            card[i] = pcard[i];
        k--;
    }
    for (int i = 1; i <= 54; ++i) {
        if (card[i] >= 1 && card[i] <= 13) printf("S%d", card[i]);
        else if (card[i] > 13 && card[i] <= 26)   printf("H%d", card[i] - 13);
        else if (card[i] > 26 && card[i] <= 39)  printf("C%d", card[i] - 26);
        else if (card[i] > 39 && card[i] <= 52)  printf("D%d", card[i] - 39);
        else printf("J%d", card[i] - 52);
        if (i!=54)    printf(" ");
    }
    return 0;
}

7-49 Have Fun with Numbers

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

Input Specification:

Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

Output Specification:

For each test case, first print in a line “Yes” if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or “No” if not. Then in the next line, print the doubled number.

Sample Input:

1234567899

Sample Output:

Yes
2469135798

这题也是,题目看不太懂,但是比43题要好的多的多得多

逐句翻译

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication

请注意,数字 123456789 是一个 9 位数,正好由 1 到 9 的数字组成,没有重复的数字

Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation

将它加倍,我们将得到 246913578,这恰好是另一个 9 位数,完全由 1 到 9 的数字组成,只是排列方式不同而已

Check to see the result if we double it again!

现在,您需要检查是否有更多数字具有此属性

也就是说,将一个有 k 位数字的给定数字加倍,你要知道得到的数字是否只由原始数字中的数字排列而成。

错误原因

一直在解决那个运算大小的问题,这个我一直不清楚

错误代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int a[11] = { 0 };
void adda(unsigned long long x) {
    while (x) {
        a[x % 10] = 1;
        x /= 10;
    }
    return;
}
int main() {
    for (int i = 0; i <= 9; ++i)  a[i] = 0;    //初始化
    unsigned long long x = 0, flag = 1, t = 0;
    scanf("%lld", &x);
    adda(x);
    t = x * 2;
    for (int i = 0; i <= 9; ++i) {
        if (a[i] == 0) flag = 0;
    }
    if (flag) {
        printf("Yes\n%lld", t);
        return 0;
    }
    flag = 1;
    while (t) {
        if (a[t % 10] == 0) {
            flag = 0;
            break;
        }
        else {
            a[t % 10]++;
        }
        t /= 10;
    }
    for (int i = 0; i <= 9; ++i) {
        if (a[i] == 1) flag = 0;
    }
    if (flag)    printf("Yes\n%lld", x * 2);
    else printf("No\n%lld", x * 2);
    return 0;
}

正确代码如下:

思路如下:

分析:使用char数组存储这个数,没个数的数位乘以2 + 进位,同时设立book来标记数位出现的情况。只有最后book的每个元素都是0的时候才说明这两个数字是相等的一个排列结果~

代码地址

正确代码

https://blog.csdn.net/liuchuo/article/details/52155928?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171204890216800215051717%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=171204890216800215051717&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1~rank_v31_ecpm-1-52155928-null-null.nonecase&utm_term=Have%20Fun%20with%20Numbers&spm=1018.2226.3001.4450

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int book[10];
int main() {
    char num[22];
    scanf("%s", num);
    int flag = 0, len = strlen(num);
    for (int i = len - 1; i >= 0; i--) {
        int temp = num[i] - '0';
        book[temp]++;
        temp = temp * 2 + flag;
        flag = 0;
        if (temp >= 10) {
            temp = temp - 10;
            flag = 1;
        }
        num[i] = (temp + '0');
        book[temp]--;
    }
    int flag1 = 0;
    for (int i = 0; i < 10; i++) {
        if (book[i] != 0)
            flag1 = 1;
    }
    printf("%s", (flag == 1 || flag1 == 1) ? "No\n" : "Yes\n");
    if (flag == 1) printf("1");
    printf("%s", num);
    return 0;
}

7-50 输出华氏-摄氏温度转换表

题目如下:

输入2个正整数lowerupperlowerupper≤100),请输出一张取值范围为[lowerupper]、且每次增加2华氏度的华氏-摄氏温度转换表。

温度转换的计算公式:C=5×(F−32)/9,其中:C表示摄氏温度,F表示华氏温度。

输入格式:

在一行中输入2个整数,分别表示lowerupper的值,中间用空格分开。

输出格式:

第一行输出:“fahr celsius”

接着每行输出一个华氏温度fahr(整型)与一个摄氏温度celsius(占据6个字符宽度,靠右对齐,保留1位小数)。

若输入的范围不合法,则输出"Invalid."。

输入样例1:

32 35

输出样例1:

fahr celsius
32   0.0
34   1.1

输入样例2:

40 30

输出样例2:

Invalid.

错误原因

温度是可以有负的,铁铁,我确实是傻逼了

正确代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
	int lower = 0, upper = 0;
	scanf("%d %d", &lower, &upper);
	if ( lower>upper) {
		printf("Invalid.\n");
		return 0;
	}
	printf("fahr celsius\n");
	for (int i = lower; i <= upper; i += 2) {
		printf("%d%6.1f\n", i, 5 * (i - 32) / 9.0);
	}
}

7-56 求给定精度的简单交错序列部分和

本题要求编写程序,计算序列部分和 1 - 1/4 + 1/7 - 1/10 + … 直到最后一项的绝对值不大于给定精度eps。

输入格式:

输入在一行中给出一个正实数eps。

输出格式:

在一行中按照“sum = S”的格式输出部分和的值S,精确到小数点后六位。题目保证计算结果不超过双精度范围。

输入样例1:

4E-2

输出样例1:

sum = 0.854457

输入样例2:

0.02

输出样例2:

sum = 0.826310

代码长度限制

16 KB

时间限制

400 ms

内存限制

这题我是写出来了,但是我写的非常的糟糕,非常的糟糕

代码真的是又臭又长,也是真的抽象

我自己写的代码,正确但是臭

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <stdlib.h>
double Mi(int d) {
	double sum = 1;
	for (int i = 1; i < d; ++i) {
		sum *= 10;
	}
	if (d < 0) {
		for (int i = 1; i < -d; ++i) {
			sum *= 0.1;
		}
	}
	return sum;
}
double chartodbl(char* a) {
	int dit = strlen(a),sum=0;
	for (int i = 0; i < strlen(a); i++) {
		sum += (a[i] - '0') * Mi(dit);
		dit--;
	}
	return sum;
}
double strtodbl(char *a) {
	int dit = 0,flag=0;	//0表示在E之前的数字
	char num[50] = { ' ' }, mi[50] = {' '};
	int index = 0,num2=0;
	double num1 = 0;
	for (int i = 0; i < strlen(a); i++) {
		if (!flag) {
			if (a[i] > '0' && a[i] < '9')
				num[i] = a[i];
		}
		if (a[i] == 'E') {
			flag = 1;
			num1 = chartodbl(num);
		}
		if (flag) {
			if (a[i] > '0' && a[i] < '9') {
				mi[index] = a[i];
				index++;
			}
		}
	}
	num2 = chartodbl(mi);
	for (int i = 1; i <=num2; ++i) {
		num1 *= 0.1;
	}
	return num1;
}
//用来判断是不是科学计数法
int judge(char* a) {
	for (int i = 0; i < strlen(a); ++i) {
		if (a[i] == 'E')	return 0;
	}
	return 1;
}
double nor(char* a) {
	for (int i = 0; i < strlen(a); ++i) {

	}
}
int main() {
	double sum=0, n = 0, t = 1,i=1;
	char str[100] = { ' ' };
	scanf("%s", str);
	if (!judge(str)) {
		n = strtodbl(str);
	}
	else
		n = atof(str);
	if (n >= 1)	sum = 1;
	//printf("%lf\n", n);
	while (t - n >0) {
		t = 1.0 / (3 * i - 2);
		if ((int)i % 2 == 0)	t *= -1;
		//printf("i is %lf   t is %lf\n", i, t);
		sum += t;
		i++;
		if (t < 0)	t = -t;
	}
	//printf("t is %.8lf\n", t);
	//t = 1.0 / (3 * i - 2);
	//if ((int)i % 2 == 0)	t *= -1;
	//sum += t;
	printf("sum = %.6lf", sum);
}

找找柳神的代码,怎么样。

我是傻逼,这个是可以读取科学计数法的,我真的是日了狗了

我是傻逼,scanf是可以读取double类型下的科学计数法的

还有就是float和double,能用double,就用double,因为这两个float可能会损失精度

别人的代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int main(void)
{
    double eps;
    double flag, denominator, item, sum;
    scanf_s("%lf", &eps);
    flag = 1;
    denominator = 1;
    /*
        item的初值比eps大
        可以保证进入循环程序
        可以满足eps比1大的情况下,sum能加到第一项
    */
    item = eps + 1;
    sum = 0;
    while (fabs(item) > eps)
    {
        /*不满足while判断表达式后的第一项,此时的item为下一项*/
        item = flag / denominator;
        sum = sum + item;
        flag = -flag;
        denominator = denominator + 3;
    }

    printf("sum = %.6f\n", sum);

    return 0;
}

7-59 打印菱形图案

本题要求编写程序,打印一个高度为n的、由“*”组成的正菱形图案。

输入格式:

输入在一行中给出一个正的奇数n

输出格式:

输出由n行星号“*”组成的菱形,如样例所示。每个星号后跟一个空格。

输入样例:

7

输出样例:

      * 
    * * * 
  * * * * * 
* * * * * * * 
  * * * * * 
    * * * 
      * 

代码长度限制

16 KB

时间限制

400 ms

内存限制

错误原因

也没有什么错误原因,就是不会写,我也是有点低能的

我真是傻逼了,不管怎么样,先输出5个空格在说呗,为什么要管呢

然后在输出一个*

正确代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
    int n;
    scanf("%d", &n);
    int i;
    int j;
    for (i = 1; i <= n / 2 + 1; i++)
    {
        for (j = 1; j <= n + 1 - 2 * i; j++)        //  不管怎么样,先输出5个空格
            printf(" ");
        for (j = 1; j <= 2 * i - 1; j++)            // 然后在输出对应的*
            printf("* ");

        printf("\n");
    }
    for (i = 1; i <= n / 2; i++)
    {
        for (j = 1; j <= 2 * i; j++)
            printf(" ");
        for (j = 2 * (n / 2) - 1 - 2 * (i - 1); j >= 1; j--)
            printf("* ");
        printf("\n");
    }
    return 0;
}

7-79 N个数求和

本题的要求很简单,就是求N个数字的和。麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和也必须是有理数的形式。

输入格式:

输入第一行给出一个正整数N(≤100)。随后一行按格式a1/b1 a2/b2 ...给出N个有理数。题目保证所有分子和分母都在长整型范围内。另外,负数的符号一定出现在分子前面。

输出格式:

输出上述数字和的最简形式 —— 即将结果写成整数部分 分数部分,其中分数部分写成分子/分母,要求分子小于分母,且它们没有公因子。如果结果的整数部分为0,则只输出分数部分。

输入样例1:

5
2/5 4/15 1/30 -2/60 8/3

输出样例1:

3 1/3

输入样例2:

2
4/3 2/3

输出样例2:

2

输入样例3:

3
1/3 -1/6 1/8

输出样例3:

7/24

代码长度限制

16 KB

时间限制

400 ms

内存限制

错误原因,已经超时了

错误代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//求最大公约数
long lcd(long a, long b) {
    long m = 0;
    if (a > b) m = a;
    else m = b;
    for (long i = m; i <= a * b; ++i) {
        if (i % a == 0 && i % b == 0)   return i;
    }
    return 0;
}
long gcd(long a, long b) {
    long m = 0;
    if (b < 0) b = -b;
    if (a < b) m = a;
    else m = b;
    for (int i = m; i >= 1; i--) {
        if (a % i == 0 && b % i == 0)  return i;
    }
    return 0;
}
void add(long *n,long *a, long *b, long *a1, long *b1) {
    long blcd = lcd(*b, *b1);
    long alcd = (blcd / *b) * (*a) + (blcd / *b1) * (*a1)+(*n)*blcd;
    long tgcd = gcd(blcd, alcd);
    *a = alcd / tgcd;
    *b = blcd / tgcd;
    *n = *a / (*b);
    *a = *a - (*n) * (*b);
    return 0;
}

void put(long num, long a, long b) {
    if (a != 0) {
        if (num != 0)
            printf("%ld %ld/%ld", num, a, b);
        else
            printf("%ld/%ld", a, b);
    }
    else
        printf("%ld", num);
}
int main() {
    int n = 0;
    scanf("%d", &n);
    long a[101] = { 0 }, b[101] = { 0 },num=0;
    scanf("%ld/%ld", &a[1], &b[1]);
    for (int i = 2; i <= n; ++i) {
        scanf("%ld/%ld", &a[i],&b[i]);
        add(&num,&a[1], &b[1], &a[i], &b[i]);
    }
    put(num,a[1], b[1]);
    return 0;
}

自己写的正确代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//求最大公倍数数
long lcd(long a, long b) {
    long m = 0;
    if (a > b) m = a;
    else m = b;
    for (long i = m; i <= a * b; ++i) {
        if (i % a == 0 && i % b == 0)   return i;
    }
    return 0;
}
//最小公约数
long gcd(long a, long b) {
    long m = 0;
    if (a < 0)  a = -a;
    if (a < b) m = a;
    else m = b;
    for (long i = m; i >= 1; i--) {
        if (a % i == 0 && b % i == 0)  return i;
    }
    return 0;
}
//化简
long gcdmyself(long* a, long* b) {
    long mlcd = gcd(*a, *b);
    //printf("mcld is %ld\n", mlcd);
    *a = *a / mlcd;
    *b = *b / mlcd;
    return;
}
//相加的函数
void add(long* n, long* a, long* b, long* a1, long* b1) {
    long blcd = lcd(*b, *b1);
    long alcd = (blcd / *b) * (*a) + (blcd / *b1) * (*a1) + (*n) * blcd;
    long tgcd = gcd(alcd,blcd);
    if (alcd != 0) {
        *a = alcd / tgcd;
        *b = blcd / tgcd;
        *n = *a / (*b);
        *a = *a - (*n) * (*b);
    }
    else {
        *n = 0;
        *a = 0;
        *b = 1;
    }
    return 0;
}
//输出
void put(long num, long a, long b) {
    if (a == 0 && num == 0) {
        printf("0");
        return ;
    }
    if (a != 0) {
        if (num != 0)
            printf("%ld %ld/%ld", num, a, b);
        else
            printf("%ld/%ld", a, b);
    }
    else
        printf("%ld", num);
}
int main() {
    int n = 0;
    scanf("%d", &n);
    long a[101] = { 0 }, b[101] = { 0 }, num = 0;
    scanf("%ld/%ld", &a[1], &b[1]);
    gcdmyself(&a[1], &b[1]);
    for (int i = 2; i <= n; ++i) {
        scanf("%ld/%ld", &a[i], &b[i]);
        gcdmyself(&a[i], &b[i]);    
        add(&num, &a[1], &b[1], &a[i], &b[i]);
    }
    put(num, a[1], b[1]);
    return 0;
}

柳神的好代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

long long gcd(long long a, long long b) {
    return b == 0 ? a : gcd(b, a % b);
}
int main() {
    long long n, a, b, suma = 0, sumb = 1, gcdvalue;
    scanf("%lld", &n);
    for (int i = 0; i < n; i++) {
        scanf("%lld/%lld", &a, &b);
        gcdvalue = (suma == 0 || sumb == 0) ? 1 : gcd(abs(suma), abs(sumb));
        sumb = sumb / gcdvalue;
        suma = suma / gcdvalue;
        gcdvalue = (a == 0 || b == 0) ? 1 : gcd(abs(a), abs(b));
        a = a / gcdvalue;
        b = b / gcdvalue;
        suma = a * sumb + suma * b;
        sumb = b * sumb;
    }
    long long integer = suma / sumb;
    suma = suma - (sumb * integer);
    gcdvalue = (suma == 0 || sumb == 0) ? 1 : gcd(abs(suma), abs(sumb));
    suma = suma / gcdvalue;
    sumb = sumb / gcdvalue;
    if (integer != 0) {
        printf("%lld", integer);
        if (suma != 0)
            printf(" ");
    }
    if (suma != 0) {
        printf("%lld/%lld", suma, sumb);
    }
    if (integer == 0 && suma == 0)
        printf("0");
    return 0;
}
  • 12
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值