#include<stdio.h> intmain() { int result = 0; scanf("%d", &result); int p = 0,index=1; while (result) { int t = result % 16 * index; p += t; index *= 10; result /= 16; } printf("%d", p); return0; }
7-16 计算符号函数的值
对于任一整数n,符号函数sig**n(n)的定义如下:
记录
请编写程序计算该函数对任一输入整数的值。
输入格式:
输入在一行中给出整数n。
输出格式:
在一行中按照格式“sign(n) = 函数值”输出该整数n对应的函数值。
输入样例1:
1
10
输出样例1:
1
sign(10) = 1
输入样例2:
1
0
输出样例2:
1
sign(0) = 0
输入样例3:
1
-98
输出样例3:
1
sign(-98) = -1
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain() { int p = 0,t=0; scanf("%d", &p); t = p; if (p < 0) p = -1; elseif (p > 0) p = 1; else p = 0; printf("sign(%d) = %d",t,p); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain(void) { int a = 0, b = 0, c = 0; scanf("%d %d %d", &a, &b, &c); if (a == b) printf("C"); elseif (a == c) printf("B"); elseprintf("A"); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> //当x不超过15吨时,y=4x/3;超过后,y=2.5x−17.5 intmain(void) { int x = 0; float y = 0; scanf("%d", &x); if (x <= 15) y = 4 * x / 3.0; else y = x * 2.5 - 17.5; printf("%.2f", y); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> /*Too big Too small Too small */ intmain(void) { int num = 0, frequency = 0; int gres = 0, gresfre = 0, flag = 0; scanf("%d %d", &num, &frequency); while (gresfre<=frequency) { scanf("%d", &gres); gresfre++; if (gres < 0) { printf("Game Over"); return0; } if (gres == num) { flag = 1; break; } elseif (gres < num) printf("Too small\n"); elseprintf("Too big\n"); } if (gresfre > frequency) { printf("Game Over"); return0; } if (gres == 1) printf("Bingo!"); elseif (gresfre <= 3) printf("Lucky You!"); elseif (flag == 1) printf("Good Guess!"); elseprintf("Game Over"); return0; }
7-25 求奇数和
本题要求计算给定的一系列正整数中奇数的和。
输入格式:
输入在一行中给出一系列正整数,其间以空格分隔。当读到零或负整数时,表示输入结束,该数字不要处理。
输出格式:
在一行中输出正整数序列中奇数的和。
输入样例:
1
8 7 4 3 70 5 6 101 -1
输出样例:
1
116
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain(void) { int res = 0, a = 0; scanf("%d", &a); while (a > 0) { if (a % 2 != 0) res +=a; scanf("%d", &a); } printf("%d", res); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain(void) { int M = 0, N = 0, gcd = 0, lcm = 0; //gcd是最大公约数,lcm是最小公倍数 //greatest common measure lowest common multiple scanf("%d %d", &M, &N); if (M > N) { gcd = N; lcm = M; } else { gcd = M; lcm = N; } for (int i = gcd; i >= 1; --i) { if (M % i == 0 && N % i == 0) { gcd = i; break; } } for (int i = lcm; i <= M * N; ++i) { if (i % M == 0 && i % N == 0) { lcm = i; break; } } printf("%d %d", gcd, lcm); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intdigsum(int x) { int sum = 0; while (x) { sum += x % 10; x /= 10; } return sum; } intmain(void) { int x = 0, time = 1, next = 13; //设置成默认的13是为了方便一步到位的情况 scanf("%d", &x); while (digsum(x) * 3 + 1 != x) { next = digsum(x) * 3 + 1; printf("%d:%d\n", time, next); time++; x = digsum(x) * 3 + 1; } printf("%d:%d\n", time, next); return0; }
7-32 求交错序列前N项和
本题要求编写程序,计算交错序列 1-2/3+3/5-4/7+5/9-6/11+… 的前N项之和。
输入格式:
输入在一行中给出一个正整数N。
输出格式:
在一行中输出部分和的值,结果保留三位小数。
输入样例:
1
5
输出样例:
1
0.917
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> floatJudge(int i) { if (i % 2 == 1) return-1.0; elsereturn1.0; } intmain(void) { int n = 0; float sum = 1; scanf("%d", &n); for (float i = 1; i < n; ++i) sum+= Judge(i) * ((1+i) / (2*i+1)); printf("%0.3f", sum); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intisPrime(int num) { if (num == 1) return0; for (int i = 2; i <=num / 2; ++i) { if (num % i == 0) return0; } return1; } intmain(void) { int m = 0, n = 0,times=0,sum=0; scanf("%d %d", &m, &n); for (int i = m; i <=n; ++i) { if (isPrime(i) == 1) { times++; sum += i; } } printf("%d %d", times, sum); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intdit(long t) { int ditnum = 0; while (t) { t /= 10; ditnum++; } return ditnum; } intmi(int t) { int res = 1; for (int i = 0; i < t - 1; ++i) { res *= 10; } return res; } intmain(void) { long t = 0; int dig = 0; scanf("%ld", &t); if (t == 0)printf("0 "); dig = dit(t); while (dig) { printf("%ld ", t / mi(dig)); t = t - (t / mi(dig)) * mi(dig); dig--; } return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain(void) { int y = 0, f = 0, n = 0,flag=0; scanf("%d", &n); for (int y = 0; y < 49; y++) { for (int f = 0; f < 100; f++) { if (98 * f - 199 * y == n) { flag = 1; printf("%d.%d", y, f); } } } if (!flag) printf("No Solution"); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain(void) { float N = 0, H = 0, W = 0; scanf("%f", &N); for (int i = 0; i < N; ++i) { scanf("%f%f", &H, &W); float PerferW = 2 * (H - 100) * 0.9 ; //完美体重 float DecterW = 0; //这个是误差的值 if (W > PerferW&& W - PerferW>=PerferW*0.1) { //真实体重大于实际体重 printf("You are tai pang le!\n"); } elseif (W <= PerferW && PerferW - W >= PerferW * 0.1) { printf("You are tai shou le!\n"); } else { printf("You are wan mei!\n"); } } }
7-41 计算阶乘和
对于给定的正整数N,需要你计算 S=1!+2!+3!+…+N!。
输入格式:
输入在一行中给出一个不超过10的正整数N。
输出格式:
在一行中输出S的值。
输入样例:
1
3
输出样例:
1
9
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intfactorial(int n) { int sum = 1; for (int i = 1; i <= n; ++i) { sum *= i; } return sum; } intmain(void) { int n = 0, sum = 0; scanf("%d", &n); for (int i = 1; i <=n; ++i) { sum += factorial(i); } printf("%d", sum); }
#include<stdio.h> intmain(){ int x,y = 1; scanf("%d",&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
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.
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intjudge(int x) { int sum = 1; for (int i = 2; i <= x / 2; ++i) { if (x % i == 0) sum += i; } if (sum == x) return1; elsereturn0; } intmain() { int begin = 0, end = 0,flag=0; scanf("%d %d", &begin, &end); for (int i = begin; i <= end; ++i) { if (judge(i)) { flag = 1; printf("%d = 1", i); for (int j = 2; j <= i / 2; ++j) { if (i % j == 0) printf(" + %d", j); } printf("\n"); } } if (!flag) printf("None\n"); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> doublefact(int n) { double sum = 1; for (int i = 1; i <= n; ++i) sum *= i; return sum; } intmain() { int n = 0, m = 0; scanf("%d %d", &m, &n); double res = fact(n) / (fact(m) * (fact(n - m)) ); printf("result = %.0lf", res); 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.
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> longMi(int n) { long sum = 1; if(!sum) return sum = 1; for (int i = 1; i <= n; ++i) sum *= 3; return sum; } intmain() { int n = 0; scanf("%d", &n); for (int i = 0; i <= n; ++i) { printf("pow(3,%d) = %ld\n", i, Mi(i)); } }
7-54 求阶乘序列前N项和
本题要求编写程序,计算序列 1!+2!+3!+⋯ 的前N项之和。
输入格式:
输入在一行中给出一个不超过12的正整数N。
输出格式:
在一行中输出整数结果。
输入样例:
1
5
输出样例:
1
153
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intfac(int n) { int sum = 1; for (int i = 1; i <= n; ++i) { sum *= i; } return sum; } intmain() { int n = 0,sum=0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { sum += fac(i); } printf("%d", sum); 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); }
7-57 求e的近似值
自然常数 e 可以用级数 1+1/1!+1/2!+⋯+1/n!+⋯ 来近似计算。本题要求对给定的非负整数 n,求该级数的前 n+1 项和。
输入格式:
输入第一行中给出非负整数 n(≤1000)。
输出格式:
在一行中输出部分和的值,保留小数点后八位。
输入样例:
1
10
输出样例:
1
2.71828180
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> doublefac(double n) { double sum = 1; for (double i = 1; i <= n; ++i) sum *= i; return1.0 / sum; } intmain(void){ double n = 0,sum=1; scanf("%lf", &n); for (double i = 1; i <= n; ++i) { sum += fac(i); } printf("%.8lf", sum); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<string.h> #include<stdio.h> intmain(void) { int h = 0, m = 0, s = 0,ps=0,index=0; scanf("%d:%d:%d", &h, &m, &s); scanf("%d", &ps); if (s + ps < 60) s = s + ps; else { index = (s + ps) / 60; s = (s + ps) % 60; if (m + index < 60) m = m + index; else { index = (m + index) / 60; m = (m + index) % 60; if (h + index < 24) h += index; else { h = h + index - 24; } } } printf("%02d:%02d:%02d", h, m, s); }
7-67 What is a computer?
本题要求编写程序,输出一个短句“What is a computer?”。
输入格式:
本题目没有输入。
输出格式:
在一行中输出短句“What is a computer?”。
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7
#define _CRT_SECURE_NO_WARNINGS #include<string.h> #include<stdio.h> intmain(void) { printf("What is a computer?"); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> intjudge(int a, int b, int c) { if (a + b > c && a + c > b && b + c > a) return1; elsereturn0; } intmain(void) { int a = 0, b = 0, c = 0; double s = 0,area=0,per=0; scanf("%d %d %d", &a, &b, &c); if (!judge(a,b,c)) printf("These sides do not correspond to a valid triangle"); else { per = a + b + c; s = per / 2.0; area = s * (s - a) * (s - b) * (s - c); area = sqrt(area); printf("area = %.2lf; perimeter = %.2lf",area,per); } return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> doublefac(int n) { double sum = 0; return n * n + 1.0 / n; } intmain(void) { int m = 0, n = 0; scanf("%d %d", &m, &n); double sum = 0; for (int i = m; i <= n; ++i) { sum += fac(i); } printf("sum = %.6lf", sum); return0; }
在一行中输出此人在第N天中是“Fishing”(即“打鱼”)还是“Drying”(即“晒网”),并且输出“in day N”。
输入样例1:
1
103
输出样例1:
1
Fishing in day 103
输入样例2:
1
34
输出样例2:
1
Drying in day 34
鸣谢内蒙古师范大学张志平老师补充数据
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> intmain(void) { int n = 0; scanf("%d", &n); if (n % 5 <= 3&&n%5>0)printf("Fishing in day %d", n); elseprintf("Drying in day %d", n); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> intmain(void) { int a = 0, b = 0, c = 0; scanf("%d %d %d", &a, &b, &c); if(a > b) { int t = a; a = b; b = t; } if (a > c) { int t = a; a = c; c = t; } if (b > c) { int t = b; b = c; c = t; } printf("%d->%d->%d", a, b, c); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> doublejudge(double a, double b, double c) { if (a + b > c && a + c > b && b + c > a) return1; elsereturn0; } doubleside(double x1, double y1, double x2, double y2) { double x = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); //printf("x is %lf\n", x); returnsqrt(x); } intmain(void) { double x1=0,y1=0,x2=0,y2=0,x3=0,y3=0; double a = 0, b = 0, c = 0; double s = 0, area = 0, per = 0; scanf("%lf %lf %lf %lf %lf %lf", &x1,&y1,&x2,&y2,&x3,&y3); a = side(x1, y1, x2, y2); b = side(x1, y1, x3, y3); c = side(x2, y2, x3, y3); //printf("a,b,c is %lf %lf %lf\n", a, b, c); if (!judge(a, b, c)) printf("Impossible"); else { per = a + b + c; s = per / 2.0; area = s * (s - a) * (s - b) * (s - c); area = sqrt(area); printf("L = %.2lf, A = %.2lf", per, area); } return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> intmain(void) { double sum = 0,x=0; scanf("%lf", &x); if (x == 10) sum = 1.0 / x; else sum = x; printf("f(%.1lf) = %.1lf", x, sum); return0; }
7-77 求1到100的和
本题要求编写程序,计算表达式 1 + 2 + 3 + … + 100 的值。
输入格式:
本题无输入。
输出格式:
按照以下格式输出:
1
sum= 累加和
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> intmain(void) { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } printf("sum = %d", sum); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain() { char t= ' '; double a = 6.95, b = 7.44, c = 7.93, x = 0, oil = 0,sum=0; scanf("%lf %lf %c", &x,&oil,&t); if (oil == 90) { if (t == 'm') sum = x * a * 0.95; else sum = x * a * 0.97; } elseif (oil == 93) { if (t == 'm') sum = x * b * 0.95; else sum = x * b * 0.97; } else if (t == 'm') sum = x * c * 0.95; else sum = x * c * 0.97; printf("%.2lf", sum); 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; }
7-80 打折
去商场淘打折商品时,计算打折以后的价钱是件颇费脑子的事情。例如原价 ¥988,标明打 7 折,则折扣价应该是 ¥988 x 70% = ¥691.60。本题就请你写个程序替客户计算折扣价。
intmain() { int year = 0,flag=0; scanf("%d", &year); if (year > 2000 && year <= 2100) { for (int i = 2001; i <= year; ++i) { if (i % 400 == 0 || (i % 4 == 00 && i % 100 != 0)) { printf("%d\n", i); flag = 1; } } if (!flag) printf("None"); } else printf("Invalid year!"); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math.h> intmypow(int a, int b) { int sum = 1; for (int i = 1; i <= b; ++i) { sum *= a; } return sum; } intjudge(int n,int mi) { int sum = 0, t = n; while (n) { sum += mypow(n % 10, mi); if (sum > t) return0; n /= 10; } if (t == sum) return1; elsereturn0; } intmain() { int n = 0; scanf("%d", &n); for (int i = mypow(10, n - 1); i < mypow(10, n); ++i) { if (judge(i,n)) printf("%d\n", i); } //if (judge(153)) printf("153\n"); return0; }
intmain() { int n = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= i; ++j) { printf("%d*%d=%-4d", j, i, i * j); } printf("\n"); } return0; }
intmain() { int n = 0,flag=0; scanf("%d", &n); while (n != 0) { if (n % 10 != 0) { printf("%d", n % 10); flag = 1; } else { if (flag) printf("%d", n % 10); }
#define _CRT_SECURE_NO_WARNINGS #include<string.h> #include<stdio.h> voiddang() { printf("Dang"); return; } intmain(void) { int hh = 0, mm = 0; scanf("%d:%d", &hh, &mm); if (hh < 12||(hh==12&&mm==0)) { printf("Only %02d:%02d. Too early to Dang.", hh, mm); } else { int sum = 0; if (mm != 0)sum++; sum += hh - 12; for (int i = 1; i <= sum; ++i) dang(); } return0; }
intmain() { int i, j, k; int n; int count = 0; scanf("%d", &n); for (i = n; i <= n + 3; i++) { for (j = n; j <= n + 3; j++) for (k = n; k <= n + 3; k++) if (i != j && j != k && i != k) { count++; if (count % 6 == 0) printf("%d%d%d\n", i, j, k); else printf("%d%d%d ", i, j, k); } } return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> intmain() { int n = 0, y = 0, x = 0, flag=0; scanf("%d", &n); for (int y = sqrt(n); y >= 1; y--) { x = sqrt(n - y * y); if (x > y) break; if (x * x + y * y == n && x != 0) { printf("%d %d\n", x, y); flag = 1; } } if (!flag) printf("No Solution"); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> intgcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } intmain() { int a = 0, b = 0; scanf("%d/%d", &a, &b); printf("%d/%d", a / gcd(a, b), b / gcd(a, b)); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> intjudge(int n) { for (int i = 2; i <= n / 2; ++i) { if (n % i == 0) return0; } return1; } intmain() { int n = 0,t=10; scanf("%d", &n); n = n % 2 == 0 ? n - 1 : n-2; for (int i =n; i >= 2; --i) { if (judge(i)&&t) { printf("%6d", i); t--; } } return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> doublemonomial(double n) { double num = 1, den = 1; for (int i = 1; i <= n; ++i) num *= i; for (int i = 1; i <= n; ++i) { den *= (i * 2 + 1); } return num*1.0 / den; } intmain() { double n = 0; scanf("%lf", &n); double sum = 1,t=1; while (monomial(t) >= n) { sum += monomial(t); t++; } sum += monomial(t); printf("%.06lf", sum * 2); return0; }
7-117 整数的分类处理
给定 N 个正整数,要求你从中得到下列三种计算结果:
A1 = 能被 3 整除的最大整数
A2 = 存在整数 K 使之可以表示为 3K+1 的整数的个数
A3 = 存在整数 K 使之可以表示为 3K+2 的所有整数的平均值(精确到小数点后 1 位)
输入格式:
输入首先在第一行给出一个正整数 N,随后一行给出 N 个正整数。所有数字都不超过 100,同行数字以空格分隔。
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> intmain() { int n = 0,first=0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { if (i % 2 == 1) { if (first) printf(" "); else first = 1; printf("%d", i); } } return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intmain() { int n = 0; scanf("%d", &n); double sum = 0, min = DBL_MAX,max = DBL_MIN,t=0; for (int i = 1; i <= n; ++i) { scanf("%lf", &t); if (t > max) max = t; if (t < min) min = t; sum += t; } sum = sum - max - min; printf("%.02lf", sum / (n - 2)); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intmain() { int n = 0, t = 0; scanf("%d", &n); while (n!=1) { if (n % 2 == 1) n = 3 * n + 1; else n = n / 2; t++; } printf("%d", t); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intmain() { char c = ' '; scanf("%c", &c); for (int i = 1; i <= 39; ++i) printf(" "); printf("A\n"); for (int i = 1; i <= c - 'B'+1; ++i) { for (int j = 1; j <= 39-i; ++j) { printf(" "); } printf("%c", 'A' + i);
if (i != c - 'B' + 1) { for (int j = 1; j <= 2 * i - 1; ++j) { printf(" "); } printf("%c\n", 'A' + i); } else { for (int j = 1; j <= 2 * i ; ++j) { printf("%c",'A'+i); } } } return0; }
7-124 上三角数字三角形
输入一个正整数n,输出具有n层的上三角数字三角形。
输入格式:
只有一个正整数n,1<=n<=100。
输出格式:
一个上三角数字三角形,每个数字占四个字符位置。
输入样例:
1
5
输出样例:
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intmain() { int n = 0,t=1; scanf("%d", &n); for (int i = 1; i <= n; ++i) { if (i != 1) printf("\n"); for (int j = 1; j <= n - i + 1; ++j) { printf("%4d", t); t++; } } return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intmain() { int n = 0,t=1,p=0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { if (i != 1) printf("\n"); for (int j = 1; j <= n - i + 1; ++j) { if (j == 1) { t = i; p = n; printf("%4d", t); } else { t += p; p--; printf("%4d", t); } } } return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intmain() { int ha = 0, ma = 0, hb = 0, mb = 0,hh=0,mm=0; scanf("%d:%d %d:%d", &ha, &ma, &hb, &mb); hh = (hb * 60 + mb - ma - ha * 60) / 60; mm = (hb * 60 + mb - ma - ha * 60) - hh * 60; printf("%d %d", hh, mm); return0; }
7-127 Sum of the digits
Given a none-negative number, print out the sum of its digits.
Input Format:
A none-negative integer number.
Output Format:
The sum of its all digits.
Sample Input:
1
123
Sample Output:
1
6
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intmain() { int n = 0,sum=0; scanf("%d", &n); while (n) { sum += n % 10; n /= 10; } printf("%d", sum); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intjudge(int n) { int flag = 0; for (int i = 2; i <= n / 2; ++i) { if (n % i == 0) { flag = 1; break; } } return flag; } intmain() { int n = 0,sum=0; scanf("%d", &n); n++; while (judge(n)) { n++; } printf("%d", n); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> longlongpow(long x, long n) { long sum = 1; for (int i = 1; i <= n; ++i) { sum *= x; } return sum; } longlongres(int n) { if (n == 1) return1; else { returnpow(10, n-1)+res(n-1); } } intmain() { longlong sum = 0; int n = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { sum += res(i); //printf("res(i) %lld\n", res(i)); } //printf("sum is %lld\n", sum); printf("%lld", sum * 6); return0; }
7-134 倒顺数字串
输入正整数n,输出它的倒顺数字串。如n=6时,输出
1
12345654321
输入格式:
输入一个正整数n,n的范围是[1,50]。
输出格式:
n的倒顺数字串,每两个数字之间只用一个空格隔开。
输入样例:
1
6
输出样例:
1
1 2 3 4 5 6 5 4 3 2 1
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain() { int n = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { if (i != 1) printf(" "); printf("%d", i); } for (int i = n - 1; i >= 1; --i) printf(" %d", i); return0; }
7-135 数字金字塔
输入正整数n,输出n层数字金字塔。
输入格式:
正整数n,1<=n<=9。
输出格式:
n层的金字塔,其中第1层的“1”在第1行的第39列(列从1开始计数)。
输入样例:
1
5
输出样例:
1 2 3 4 5
1 121 12321 1234321 123454321
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intmain() { int n = 0; scanf("%d", &n); for (int i = 1; i <= 38; ++i) printf(" "); printf("1\n"); for (int i = 2; i <= n; ++i) { for (int j = 1; j <= 39 - i; ++j) printf(" "); for (int j = 1; j <= i; ++j) printf("%d", j); for (int j = 1; j <= i - 1; ++j) printf("%d", i - j); printf("\n"); } return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intmain() { int n = 0,flag=0; scanf("%d", &n); for (int i = 0; i <= n / 3; ++i) { for (int j = n / 2; j >= 0; --j) { int z = n - i - j; if (3 * i + j * 2 + z / 2 == n && z % 2 == 0) { printf("men = %d, women = %d, child = %d\n", i, j, z); flag = 1; } } } if (!flag) printf("None"); return0; }
7-137 Average
You are going to read a serial of none-negative integers, which ends with a negative number that does not count as one of the data. Your program calculates the average of all the none-negative integers, and prints the average which rounds to two decimal places.
When there’s no valid data at all, your program prints out:
1
None
输入格式:
A serial of none-negative integers, ending with a negative number, which is not one of the valide data.
输出格式:
A number rounds to two decimal places, which is the average of the serial.
The printf for this case is:
1
printf("%.2f\n", (double)sum/count);
输入例子
1
1 2 3 4 5 6 -1
输出例子
1
3.50
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intmain() { int sum = 0, count = 0, n = 0; scanf("%d", &n); while (n >= 0) { sum += n; count++; scanf("%d", &n); } if (sum == 0)printf("None"); else printf("%.2f\n", (double)sum / count); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> #include<float.h> intisPrime(int n) { //1就代表是素数,0,就代表不是 int flag = 1; for (int i = 2; i <= n / 2; ++i) { if (n % i == 0) { flag = 0; break; } } return flag; } intmain() { int n = 0; scanf("%d", &n); printf("%d=", n); while (!isPrime(n)) { for (int i = 2; i <= n / 2; ++i) { if (n % i == 0) { printf("%d*",i); n = n / i; break; } } } printf("%d", n); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<string.h> #include<stdio.h> intmain(void) { int t = 0, n = 0,p=0,flag=1; scanf("%d", &t); for (int z = 1; z <= t; ++z) { scanf("%d", &n); flag = 1; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { scanf("%d", &p); if (i >= 2 && j <= i - 1 && p != 0 && flag) flag = 0; } } if (flag) printf("YES\n"); elseprintf("NO\n"); } }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> int Feature[1001] = {0}; intmain() { for (int i = 1; i <= 1000; ++i) Feature[i] = 0; int n = 0, t = 0,temp=0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d",&t); for (int j = 1; j <= t; ++j) { scanf("%d", &temp); Feature[temp]++; } } int max = INT_MIN,index=0; for (int i = 1; i <= 1000; ++i) { if (Feature[i] >= max) { index = i; max = Feature[i]; } } printf("%d %d", index, max); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> intmain() { int a = 0, b = 0, c = 0,temp=0; scanf("%d %d %d", &a, &b, &c); if (a > b) { temp = a; a = b; b = temp; } if (a > c) { temp = a; a = c; c = temp; } if (b > c) { temp = b; b = c; c = temp; } printf("%d->%d->%d", a, b, c); return0; }
对 N 中每一种不同的个位数字,以 D:M 的格式在一行中输出该位数字 D 及其在 N 中出现的次数 M。要求按 D 的升序输出。
输入样例:
1
100311
输出样例:
1 2 3
0:2 1:3 3:1
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> char num[1001] = { ' ' }; int dig[10] = { 0 }; intmain() { for (int i = 0; i < 10; ++i) dig[i] = 0; scanf("%s", num); for (int i = 0; i < strlen(num); ++i) { dig[num[i] - '0']++; } for (int i = 0; i < 10; ++i) { if (dig[i] != 0) printf("%d:%d\n", i, dig[i]); } return0; }
7-152 考试座位号
每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。
输入格式:
输入第一行给出一个正整数 N(≤1000),随后 N 行,每行给出一个考生的信息:准考证号 试机座位号 考试座位号。其中准考证号由 16 位数字组成,座位从 1 到 N 编号。输入保证每个人的准考证号都不同,并且任何时候都不会把两个人分配到同一个座位上。
#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; }
intmain() { int n = 0, res = 1; scanf("%d", &n); for (int i = 1; i <= n; ++i) { res *= 2; } printf("2^%d = %d", n, res); return0; }
7-158 计算阶乘和
对于给定的正整数N,需要你计算 S=1!+2!+3!+…+N!。
输入格式:
输入在一行中给出一个不超过10的正整数N。
输出格式:
在一行中输出S的值。
输入样例:
1
3
输出样例:
1
9
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> intfac(int n) { int res = 1; for (int i = 1; i <= n; ++i) res *= i; return res; } intmain() { int n = 0, sum = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { sum += fac(i); } printf("%d", sum); return0; }
7-159 简单题
这次真的没骗你 —— 这道超级简单的题目没有任何输入。
你只需要在一行中输出事实:This is a simple problem. 就可以了。
输入样例:
1
无
输出样例:
1
This is a simple problem.
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain() { printf("This is a simple problem."); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> int people[1000000] = { 0 }; intmain() { for (int i = 0; i < 1000000; ++i) people[i] = 0; int n = 0, k = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &k); for (int j = 1; j <= k; ++j) { int temp = 0; scanf("%d", &temp); if(k>=2) people[temp] = 1; } } int m = 0,flag=0,judge=0; scanf("%d", &m); for (int i = 1; i <= m; ++i) { int temp = 0; scanf("%d", &temp); if (!people[temp]) { if (flag) printf(" "); else flag = 1; printf("%05d", temp); people[temp] = 1; judge = 1; } } if (!judge) printf("No one is handsome"); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> //1代表就是合法的,0代表没有 intlegal(char* a) { for (int i = 0; i < strlen(a) ; ++i) { if (!isdigit(a[i]) && !isalpha(a[i]) && a[i] != '.') return0; } return1; } //1就代表有数字,0代表没有 intcheckdig(char* a) { for (int i = 0; i < strlen(a) ; ++i) { if (isdigit(a[i])) return1; } return0; } //1就代表有字母,0代表没有 intcheckalpha(char* a) { for (int i = 0; i < strlen(a) ; ++i) { if (isalpha(a[i])) return1; } return0; } intmain() { char str[100]; int n = 0; scanf("%d\n", &n); for (int i = 1; i <= n; ++i) { gets(str); if (strlen(str) < 6) printf("Your password is tai duan le.\n"); else { if (!legal(str)) { printf("Your password is tai luan le.\n"); continue; } if (checkdig(str) && !checkalpha(str)) printf("Your password needs zi mu.\n"); if (!checkdig(str) && checkalpha(str)) printf("Your password needs shu zi.\n"); if (legal(str) && checkdig(str) && checkalpha(str)) printf("Your password is wan mei.\n"); if (legal(str) && !checkdig(str) && !checkalpha(str)) printf("Your password is wan mei.\n"); } //printf("\n"); } return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> intisprime(int n) { if (n < 2) return0; for (int i = 2; i * i <= n; ++i) { if (n % i == 0) return0; } return1; } // p、q、r(p<q<r) pq+r、qr+p、rp+q intmain() { int m = 0, n = 0,sum=0; scanf("%d %d", &m, &n); for (int p = m; p <=n - 2; p++) { if (isprime(p)) { for (int q = p + 1; q <= n - 2; q++) { if (isprime(q)) { for (int r = q + 1; r <= n; ++r) { if (isprime(r) && isprime(p * q + r) && isprime(q * r + p) && isprime(r * p + q)) sum++; } } } } } printf("%d", sum); return0; }
输入在第一行给出一个不超过 10 的正整数 N,随后 N 行,每行给出 3 个正整数 0<x≤y≤z≤1000。
输出格式:
对于每一组输入,如果是一组解,就在一行中输出 Yes,否则输出 No。
输入样例:
1 2 3
2 1 1 1 5 6 7
输出样例:
1 2
Yes No
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> // p、q、r(p<q<r) pq+r、qr+p、rp+q intmain() { int n = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { int x = 0, y = 0, z = 0; scanf("%d %d %d", &x, &y, &z); if (x * x + y * y + z * z == 3 * x * y * z)printf("Yes\n"); elseprintf("No\n"); } return0; }
7-169 矩阵列平移
给定一个 n×n 的整数矩阵。对任一给定的正整数 k<n,我们将矩阵的偶数列的元素整体向下依次平移 1、……、k、1、……、k、…… 个位置,平移空出的位置用整数 x 补。你需要计算出结果矩阵的每一行元素的和。
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #define Max_N 101 int arr[Max_N][Max_N]; intmain() { for (int i = 0; i < Max_N; ++i) { for (int j = 0; j < Max_N; ++j) { arr[i][j] = 0; } } int n = 0, k = 0, x = 0; scanf("%d%d%d", &n, &k, &x); int p_arr[Max_N]; for (int i = 0; i < Max_N; ++i) p_arr[i] = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) scanf("%d", &arr[i][j]); }
//printf("\n\n"); //for (int i = 1; i <= n; ++i) { // for (int j = 1; j <= n; ++j) printf("%d ", arr[i][j]); // printf("\n"); //} int p_k = 1; int t = 1; for (int j = 2; j <= n; j += 2) { for (int i = 1; i <= n; ++i) p_arr[i] = arr[i][j]; //一个一个的赋值 int p = (t-1) % k+1; for (int i = 1; i <= p; ++i) { arr[i][j] = x; } int new_i = 1; for (int i = p + 1; i <= n; ++i) { arr[i][j] = p_arr[new_i]; new_i++; } t++; }
//测试代码 //printf("\n\n"); //for (int i = 1; i <= n; ++i) { // for (int j = 1; j <= n; ++j) printf("%d ", arr[i][j]); // printf("\n"); //} for (int i = 1; i <= n; ++i) { int sum = 0; for (int j = 1; j <= n; ++j) sum += arr[i][j]; if (i != 1) printf(" "); printf("%d", sum); }
return0; }
7-170 真的恭喜你
当别人告诉你自己考了 x 分的时候,你要回答说:“恭喜你考了 x 分!”比如小明告诉你他考了90分,你就用汉语拼音打出来 gong xi ni kao le 90 fen!。
但是如果小明没考好,比如只考了 20 分,你也“恭喜”人家就不对了。这时候你应该安慰他说:“考了 20 分别泄气!”用汉语拼音写出来就是 kao le 20 fen bie xie qi!。
输入格式:
输入在一行里给出一位小朋友的分数。这个分数是一个 0 到 100 之间的整数。
输出格式:
在一行中输出你对这位小朋友说的话。如果人家考到不低于 90 分,就说 gong xi ni kao le X fen!;如果不到 90 分,就说 kao le X fen bie xie qi!。其中 X 是小朋友输入的分数。
输入样例 1:
1
95
输出样例 1:
1
gong xi ni kao le 95 fen!
输入样例 2:
1
89
输出样例 2:
1
kao le 89 fen bie xie qi!
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> intmain() { int n = 0; scanf("%d", &n); if (n >= 90) printf("gong xi ni kao le %d fen!", n); elseprintf("kao le %d fen bie xie qi!", n); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> intmi(int n) { return n * n * n; } intmain() { int n = 0,sum=0,n_copy=0; scanf("%d", &n); n_copy = n; if (n < 100 || n>999) { printf("Invalid Value."); return0; } while (n) { sum += mi(n % 10); n /= 10; } if (sum == n_copy) printf("Yes"); elseprintf("No"); return0; }
7-172 简单求阶乘问题
本题要求编写程序,计算N的阶乘。
输入格式:
输入在一行中给出一个不超过12的正整数N。
输出格式:
在一行中输出阶乘的值。
输入样例:
1
4
输出样例:
1
24
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> intmi(int n) { int sum = 1; for (int i = 1; i <= n; ++i) sum *= i; return sum; } intmain() { int n = 0; scanf("%d", &n); printf("%d", mi(n)); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> intjudge(char a, char b) { if (a >= 'a' && a <= 'z') { if (b == a - 32 || (b == a - 1 && a >= 'b')) return1; elsereturn0; } if (a >= 'A' && a <= 'Z') { if (b == a + 32 || (b == a + 1 && a <= 'Y')) return1; elsereturn0; } } intmain() { int n = 0; //学生的人数 scanf("%d", &n); while (n) { char a[3000000]; int res = 0; scanf("%s", a); for (int i = 0; i < strlen(a) - 1; ++i) { char b = a[i]; char c = a[i + 1]; if (judge(b, c) == 0) { res = 1; break; } } if (res == 0) printf("Y\n"); elseprintf("N\n"); n--; } return0; }
4 2 5 3 A 123456789012345670 1 13:58 B 123456789012345671 0 13:58 C 12345678901234567 0 13:22 D 123456789012345672 0 03:24 C 123456789012345673 0 13:59 4 3 A 123456789012345670 1 13:58 E 123456789012345674 0 13:59 C 123456789012345673 0 13:59 F F 0 14:00 1 3 E 123456789012345674 1 13:58 1 1 A 123456789012345670 0 14:11
输出样例:
1 2 3 4 5 6 7 8
D 123456789012345672 A 123456789012345670 B 123456789012345671 E 123456789012345674 C 123456789012345673 A 123456789012345670 A 123456789012345670 E 123456789012345674
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain(void) { int n = 0, m = 0; scanf("%d%d", &n, &m); printf("%d + %d = %d\n", n, m, n + m); printf("%d - %d = %d\n", n, m, n - m); printf("%d * %d = %d\n", n, m, n * m); printf("%d / %d = %d\n", n, m, n / m); printf("%d %% %d = %d\n", n, m, n % m); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intNarcissistic(int n) { int sum = 0; int t = n; while (n) { int i = n % 10; sum += i * i * i; n /= 10; } if (sum == t) return1; elsereturn0; } intmain(void) { int m = 0, n = 0; scanf("%d%d", &m, &n); if (m >= 100 && n <= 999 && n >= m) { for (int i = m; i <= n; ++i) { if (Narcissistic(i)) printf("%d\n", i); } } else { printf("Invalid Value."); } return0; }
intmain(void) { int m = 0, per = 0, good = 0, bad = 0; scanf("%d", &m); for (int i = 1; i <= m; ++i) { int t = 0; scanf("%d", &t); if (t >= 85) per++; elseif (t >= 60) good++; else bad++; } printf("%d %d %d", per, good, bad); return0; }
7-180 嫑废话上代码
Linux 之父 Linus Torvalds 的名言是:“Talk is cheap. Show me the code.”(嫑废话,上代码)。本题就请你直接在屏幕上输出这句话。
输入格式:
本题没有输入。
输出格式:
在一行中输出 Talk is cheap. Show me the code.。
输入样例:
1
无
输出样例:
1
Talk is cheap. Show me the code.
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
代码如下:
1 2 3 4 5 6
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain(void) { printf("Talk is cheap. Show me the code."); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain(void) { int a = 0, b = 0, c = 0; scanf("%d%d%d", &a, &b, &c); printf("%d", a * b * c); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain(void) { double a = 0, b = 0,c=0; scanf("%lf%lf%lf", &a, &b,&c); if (b == 0) a *= 2.455; else a *= 1.26; printf("%.02lf ", a); if (a > c) printf("T_T"); elseprintf("^_^"); return0; }
7-183 调和平均
N 个正数的算数平均是这些数的和除以 N,它们的调和平均是它们倒数的算数平均的倒数。本题就请你计算给定的一系列正数的调和平均值。
输入格式:
每个输入包含 1 个测试用例。每个测试用例第 1 行给出正整数 N (≤1000);第 2 行给出 N 个正数,都在区间 [0.1,100] 内。
输出格式:
在一行中输出给定数列的调和平均值,输出小数点后2位。
输入样例:
1 2
8 10 15 12.7 0.3 4 13 1 15.6
输出样例:
1
1.61
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain(void) { int n = 0; double m = 0,sum=0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%lf", &m); sum += 1.0 / m; } printf("%.02lf", 1/(sum/n)); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> intmain() { int res = 0; int a = 0, b = 0, c = 0, d = 0; scanf("%d%d%d%d", &a, &b, &c, &d); //printf("%d%d%d%d\n", a, b, c, d); res = abs(d - b) + abs(c - a); //printf("%d\n", res); printf("%d", res); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> #include<string.h> intmain() { int n = 0, k = 0, m = 0; scanf("%d%d%d", &n, &k, &m); printf("%d", n - k * m); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<limits.h> int ary[1000001] = { 0 }; intmain() { int min = INT_MAX, max = INT_MIN; //printf("%d %d", min, max); int n=0; scanf("%d", &n); for (int i = 0; i < 1000001; ++i) { ary[i] = 0; } int c = 0; //for (int i = 0; i < 1000001; ++i) printf("%d", ary[i]); for (int i = 1; i <= n; ++i) { //int c = 0; scanf("%d", &c); if (c > max)max = c; if (c < min)min = c; ary[c]++; //ary[min]++; }
intmain() { int yang = 0, old = 0, a = 0, b = 0; scanf("%d%d%d%d", &yang, &old, &a, &b); if (yang > a && yang > b) printf("%d-N %d-N\nzhang da zai lai ba", a, b); elseif (a >= yang && b >=yang) printf("%d-Y %d-Y\nhuan ying ru guan", a, b); elseif (a >= yang && a < old&&b<yang ) { printf("%d-Y %d-N\n1: huan ying ru guan", a, b); } elseif( b >= yang && b < old&&a<yang){ printf("%d-N %d-Y\n2: huan ying ru guan", a, b); } else { printf("%d-Y %d-Y", a, b); if (a > b) printf("\nqing 1 zhao gu hao 2"); if (b > a) printf("\nqing 2 zhao gu hao 1"); } return0; }
7-203 拯救外星人
你的外星人朋友不认得地球上的加减乘除符号,但是会算阶乘 —— 正整数 N 的阶乘记为 “N!”,是从 1 到 N 的连乘积。所以当他不知道“5+7”等于多少时,如果你告诉他等于“12!”,他就写出了“479001600”这个答案。
本题就请你写程序模仿外星人的行为。
输入格式:
输入在一行中给出两个正整数 A 和 B。
输出格式:
在一行中输出 (A+B) 的阶乘。题目保证 (A+B) 的值小于 12。
输入样例:
1
3 6
输出样例:
1
362880
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
栈限制
8192 KB
代码如下:
1 2 3 4 5 6 7 8 9 10 11
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain() { int a = 0, b = 0,sum=1; scanf("%d%d", &a, &b); for (int i = 1; i <= a + b; ++i) { sum *= i; } printf("%d", sum); return0; }
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> intmain() { int a[999] = {0}; int sum = 0; for (int i = 0; i < 999; ++i) a[i] = 0; int n = 0, k = 0, s = 0; scanf("%d%d%d", &n, &k, &s); for (int i = 1; i <= n; ++i) { int all = 0, pta = 0; scanf("%d%d", &all, &pta); if (all >= 175 && pta >= s) sum++; elseif (all >= 175) a[all]++; } for (int j = 1; j <= k;++j) { for (int i = 175; i <= 400; ++i) { if (a[i]) { sum++; a[i]--; } } } printf("%d", sum); return0; }
7-206 斯德哥尔摩火车上的题
上图是新浪微博上的一则趣闻,是瑞典斯德哥尔摩火车上的一道题,看上去是段伪代码:
1 2 3 4 5 6 7 8
s = '' a = '1112031584' for (i = 1; i < length(a); i++) { if (a[i] % 2 == a[i-1] % 2) { s += max(a[i], a[i-1]) } } goto_url('www.multisoft.se/' + s)
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.