intmain() { 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); // 输出位数 return0; }
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:
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.
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.
intmain() { 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]); elseif (card[i] > 13 && card[i] <= 26) printf("H%d", card[i] - 13); elseif (card[i] > 26 && card[i] <= 39) printf("C%d", card[i] - 26); elseif (card[i] > 39 && card[i] <= 52) printf("D%d", card[i] - 39); elseprintf("J%d", card[i] - 52); if (i!=54) printf(" "); } return0; }
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:
1
1234567899
Sample Output:
1 2
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
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> int a[11] = { 0 }; voidadda(unsignedlonglong x) { while (x) { a[x % 10] = 1; x /= 10; } return; } intmain() { for (int i = 0; i <= 9; ++i) a[i] = 0; //初始化 unsignedlonglong 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); return0; } 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); elseprintf("No\n%lld", x * 2); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> //#include <stdlib.h> doubleMi(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; } doublechartodbl(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; } doublestrtodbl(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; } //用来判断是不是科学计数法 intjudge(char* a) { for (int i = 0; i < strlen(a); ++i) { if (a[i] == 'E') return0; } return1; } doublenor(char* a) { for (int i = 0; i < strlen(a); ++i) {
} } intmain() { 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); }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> //求最大公约数 longlcd(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; } return0; } longgcd(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; } return0; } voidadd(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); return0; }
voidput(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); } intmain() { 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]); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> //求最大公倍数数 longlcd(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; } return0; } //最小公约数 longgcd(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; } return0; } //化简 longgcdmyself(long* a, long* b) { long mlcd = gcd(*a, *b); //printf("mcld is %ld\n", mlcd); *a = *a / mlcd; *b = *b / mlcd; return; } //相加的函数 voidadd(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; } return0; } //输出 voidput(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); } intmain() { 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]); return0; }