课程结课了,把做的习题都记录一下,告诉自己多少学了点东西,也能给自己一点鼓励。
ps:题目都在cxsjsxmooc.openjudge.cn上能看到,参考答案在差不多结课的时候也会在mooc上放出来。
程序设计与算法(一)C语言程序设计 第一周习题(2019夏季)
001:输出第二个整数
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int a, b, c;
cin >> a >> b >> c;
cout << b;
return 0;
}
002:字符菱形
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
char c;
scanf("%c", &c);
printf(" %c\n", c);
printf(" %c%c%c\n", c, c, c);
printf("%c%c%c%c%c\n", c, c, c, c, c);
printf(" %c%c%c\n", c, c, c);
printf(" %c\n", c);
return 0;
}
003:打印ASCII码
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
char c;
scanf("%c", &c);
printf("%d\n", c);
return 0;
}
004:打印字符
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int c;
scanf("%d", &c);
printf("%c\n", c);
return 0;
}
005:整型数据类型存储空间大小
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int a;
short b;
printf("%lu ", sizeof(a));
printf("%lu", sizeof(b));
return 0;
}
006:浮点型数据类型存储空间大小
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
float a;
double b;
printf("%lu ", sizeof(a));
printf("%lu", sizeof(b));
return 0;
}
程序设计与算法(一)C语言程序设计 第二周习题(2019夏季)
001:对齐输出
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("%8d %8d %8d", a, b, c);
return 0;
}
002:输出保留12位小数的浮点数
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
double a;
scanf("%lf", &a);
printf("%.12lf", a);
return 0;
}
003:空格分隔输出
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
char a;
int b;
float c;
double d;
scanf("%c", &a);
scanf("%d", &b);
scanf("%f", &c);
scanf("%lf", &d);
printf("%c ", a);
printf("%d ", b);
printf("%.6f ", c);
printf("%.6lf", d);
return 0;
}
004:计算球的体积
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
double d;
scanf("%lf", &d);
printf("%.2lf", 4.0/3*3.14*d*d*d);
return 0;
}
005:大象喝水
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int h, r;
float c;
scanf("%d %d", &h, &r);
c = 3.14159*r*r*h;
printf("%d", int(20000/c+1));
return 0;
}
程序设计与算法(一)C语言程序设计 第三周习题(2019夏季)
001:奇偶数判断
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int a;
cin >> a;
if (a % 2){
cout << "odd";
}else{
cout << "even";
}
return 0;
}
002:求一元二次方程的根
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define EPS 1e-6
int main(){
double a, b, c;
scanf("%lf %lf %lf", &a, &b, &c);
double x1, x2, tmp;
if (b*b-4*a*c > -EPS && b*b-4*a*c < EPS){
x1 = x2 = -b/(2*a);
printf("x1=x2=%.5lf", x1);
}else if (b*b-4*a*c >= EPS){
x1 = ((-b)+sqrt(b*b-4*a*c))/(2*a);
x2 = ((-b)-sqrt(b*b-4*a*c))/(2*a);
if (x1 < x2){
tmp = x1;
x1 = x2;
x2 = tmp;
}
printf("x1=%.5lf;x2=%.5lf", x1, x2);
}else{
// 此处x1用来储存实部系数,x2用来储存虚部系数
x1 = -b/(2*a);
x2 = sqrt(4*a*c-b*b)/(2*a);
if (x1 < EPS && x1 > -EPS){
x1 = 0;
}
printf("x1=%.5lf+%.5lfi;x2=%.5lf-%.5lfi", x1, x2, x1, x2);
}
return 0;
}
003:点和正方形的关系
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int x, y;
scanf("%d %d", &x, &y);
if (x <= 1 && x >= -1 && y <= 1 && y >= -1){
printf("yes");
}else{
printf("no");
}
return 0;
}
004:苹果和虫子2
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n, x, y;
cin >> n >> x >> y;
int a;
if (y%x){
a = n-y/x-1;
}else{
a = n-y/x;
}
if (a < 0){
a = 0;
}
cout << a;
return 0;
}
005:简单计算器
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int opd1, opd2;
char opt;
scanf("%d %d %c", &opd1, &opd2, &opt);
int res;
switch (opt){
case '+':
res = opd1+opd2;
printf("%d", res);
break;
case '-':
res = opd1-opd2;
printf("%d", res);
break;
case '*':
res = opd1*opd2;
printf("%d", res);
break;
case '/':
if (!opd2){
printf("Divided by zero!");
}else{
res = opd1/opd2;
printf("%d", res);
}
break;
default:
printf("Invalid operator!");
break;
}
return 0;
}
006:求整数的和与均值
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n, sum;
double ave;
sum = 0;
cin >> n;
int i, tmp;
for (i = 0; i < n; i++){
cin >> tmp;
sum += tmp;
}
ave = 1.0*sum/n;
printf("%d %.5f", sum, ave);
return 0;
}
007:整数序列的元素最大跨度值
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n;
cin >> n;
int max, min;
max = 0;
min = 1000;
int i, tmp;
for (i = 0; i < n; i++){
cin >> tmp;
if (tmp > max){
max = tmp;
}
if (tmp < min){
min = tmp;
}
}
cout << max - min;
return 0;
}
008:奥运奖牌计数
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n;
scanf("%d", &n);
int goldMedal, silverMedal, bronzeMedal;
goldMedal = silverMedal = bronzeMedal = 0;
int i, a, b, c;
for (i = 0; i < n; i++){
scanf("%d %d %d", &a, &b, &c);
goldMedal += a;
silverMedal += b;
bronzeMedal += c;
}
int totalMedal;
totalMedal = goldMedal+silverMedal+bronzeMedal;
printf("%d %d %d %d", goldMedal, silverMedal, bronzeMedal, totalMedal);
return 0;
}
009:乘方计算
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int a, n, res;
res = 1;
scanf("%d %d", &a, &n);
while (n > 0){
res *= a;
n--;
}
printf("%d", res);
return 0;
}
010:鸡尾酒疗法
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n;
cin >> n;
double a[n];
int i, total, effective;
for (i = 0; i < n; i++){
cin >> total >> effective;
a[i] = 1.0*effective/total;
}
for (i = 1; i < n; i++){
if (a[i]-a[0] > 0.05){
cout << "better\n";
}else if (a[i]-a[0] < -0.05){
cout << "worse\n";
}else{
cout << "same\n";
}
}
return 0;
}
程序设计与算法(一)C语言程序设计 第四周习题(2019夏季)
001:角谷猜想
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
long long n;
scanf("%lld", &n);
while (n > 1){
if (n%2){
printf("%lld*3+1=%lld\n", n, 3*n+1);
n = 3*n+1;
}else{
printf("%lld/2=%lld\n", n, n/2);
n /= 2;
}
}
printf("End");
return 0;
}
002:正常血压
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int n;
cin >> n;
int max, tmp, systolicPressure, diastolicPressure;
max = tmp = 0;
int i;
for (i = 0; i < n; i++){
cin >> systolicPressure >> diastolicPressure;
if (systolicPressure <= 140 && systolicPressure >= 90
&& diastolicPressure <= 90 && diastolicPressure >= 60){
tmp++;
if (tmp > max){
max = tmp;
}
}else{
tmp = 0;
}
}
cout << max;
return 0;
}
003:数字反转
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
int sign = 1;
if (n < 0){
sign = -1;
n = -n;
}
int rev = 0;
while (n > 0){
rev = rev*10 + n%10;
n /= 10;
}
rev = sign*rev;
printf("%d", rev);
return 0;
}
004:求特殊自然数
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[])
{
int n10, n7, n9;
int a[3], b[3], tmpA, tmpB, i, j;
for (n10 = 81; n10 < 343; n10++){
tmpA = tmpB = n10;
i = j = 2;
while (tmpA > 0){
a[i] = tmpA%7;
tmpA /= 7;
i--;
}
while (tmpB > 0){
b[j] = tmpB%9;
tmpB /= 9;
j--;
}
if (a[0] == b[2] && a[1] == b[1] && a[2] == b[0]){
break;
}
}
cout << n10 << "\n" << a[2]+a[1]*10+a[0]*100 << "\n" << b[2]+b[1]*10+b[0]*100;
return 0;
}
005:雇佣兵
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXSIZE 10
int main(int argc, char const *argv[])
{
int M, N, X;
cin >> M >> N >> X;
while (N*X >= M){
X -= M/N+1;
N += M/N;
}
printf("%d", N);
return 0;
}
006:数字统计
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[])
{
int a, b;
cin >> a >> b;
int i, tmp, counter;
counter = 0;
for (i = a; i <= b; i++){
int t = i;
while (t>0){
tmp = t%10;
t /= 10;
if (tmp == 2){
counter++;
}
}
}
cout << counter;
return 0;
}
程序设计与算法(一)C语言程序设计 第五周习题(2019夏季)
001:与指定数字相同的数的个数
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
int a[n], i;
for (i = 0; i < n; i++){
scanf("%d", &a[i]);
}
int specifiedNumber;
scanf("%d", &specifiedNumber);
int counter = 0;
for (i = 0; i < n; i++){
if (a[i] == specifiedNumber){
counter++;
}
}
printf("%d", counter);
return 0;
}
002:陶陶摘苹果
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXSIZE 10
int main(int argc, char const *argv[])
{
int height[MAXSIZE];
int i;
int counter = 0;
for (i = 0; i < MAXSIZE; i++){
cin >> height[i];
}
int taotao;
int bench = 30;
cin >> taotao;
for (i = 0; i < 10; i++){
if (height[i] <= taotao + bench){
counter++;
}
}
cout << counter;
return 0;
}
003:年龄与疾病
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXSIZE 10
int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
int a[n], i, counter18, counter35, counter60, counterOther;
counter18 = counter35 = counter60 = counterOther = 0;
for (i = 0; i < n; i++){
scanf("%d", &a[i]);
if (a[i] <=18 ){
counter18++;
}else if (a[i] <= 35){
counter35++;
}else if (a[i] <= 60){
counter60++;
}else{
counterOther++;
}
}
printf("%.2f%%\n", 100.0*counter18/n);
printf("%.2f%%\n", 100.0*counter35/n);
printf("%.2f%%\n", 100.0*counter60/n);
printf("%.2f%%\n", 100.0*counterOther/n);
return 0;
}
004:校门外的树
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[])
{
int L, M;
scanf("%d %d", &L, &M);
int a[L+1], tmpA, tmpB, i;
for (i = 0; i < L+1; a[i++] = 1)
;
for (i = 0; i < M; i++){
scanf("%d %d", &tmpA, &tmpB);
int j;
for (j = tmpA; j <= tmpB; a[j++] = 0)
;
}
int counter = 0;
for (i = 0; i < L+1; i++){
if (a[i] == 1){
counter++;
}
}
printf("%d", counter);
return 0;
}
005:计算鞍点
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXLINE 5
#define MAXROW 5
int main(int argc, char const *argv[])
{
int a[MAXLINE][MAXROW], i, j, lmax, max[MAXLINE];
for (i = 0; i < MAXLINE; i++){
cin >> a[i][0];
lmax = a[i][0];
max[i] = 0;
for (j = 1; j < MAXROW; j++){
cin >> a[i][j];
if (a[i][j] > lmax){
lmax = a[i][j];
max[i] = j;
}
}
}
int found = 1;
for (i = 0; i < MAXLINE; i++){
for (j = 0; j < MAXLINE; j++){
if (a[j][max[i]] < a[i][max[i]]){
found = 0;
}
}
if (found){
break;
}else if (i == MAXLINE-1){
break;
}
else{
found = 1;
}
}
if (found){
cout << i+1 << " " << max[i]+1 << " " << a[i][max[i]];
}else{
cout << "not found";
}
return 0;
}
006:图像模糊处理
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[])
{
int m, n;
scanf("%d %d", &m, &n);
int a[m][n], b[m][n], i, j;
for (i = 0; i < m; i++){
for (j = 0; j < n; j++){
scanf("%d", &a[i][j]);
b[i][j] = a[i][j];
}
}
for (i = 1; i < m-1; i++){
for (j = 1; j < n-1; j++){
b[i][j] = (2*(a[i][j-1]+a[i][j+1]+a[i][j]+a[i-1][j]+a[i+1][j])+5)/10;
}
}
for (i = 0; i < m; i++){
for (j = 0; j < n; j++){
printf("%d ", b[i][j]);
}
printf("\n");
}
return 0;
}
007:矩阵转置
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[])
{
int m, n;
cin >> m >> n;
int a[m][n], i, j;
for (i = 0; i < m; i++){
for (j = 0; j < n; j++){
cin >> a[i][j];
}
}
for (j = 0; j < n; j++){
for (i = 0; i < m; i++){
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
程序设计与算法(一)C语言程序设计 第六周习题(2019夏季)
001:Pell数列
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
int a[1000000];
a[0] = 1;
a[1] = 2;
int i;
for (i = 2; i < 1000000; i++){
a[i] = 2*a[i-1]+a[i-2];
if (a[i] > 32767){
a[i] %= 32767;
}
}
int b[n];
for (i = 0; i < n; i++){
scanf("%d", &b[i]);
}
for (i = 0; i < n; i++){
printf("%d\n", a[b[i]-1]%32767);
}
return 0;
}
002:求最大公约数问题
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[])
{
int a, b, tmp;
cin >> a >> b;
do{
tmp = a%b;
a = b;
b = tmp;
}while (tmp);
cout << a;
return 0;
}
003:编程填空:第i位替换
return 1<<i&m|~(1<<i)&n;
004:编程填空:第i位取反
return 1<<i^n;
005:编程填空:左边i位取反
return ~0<<32-i^n;
程序设计与算法(一)C语言程序设计 第七周习题(2019夏季)
001:统计数字字符个数
#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[])
{
int counter = 0, c;
while ((c = getchar()) != '\n'){
if (c >= '0' && c <= '9'){
counter++;
}
}
cout << counter;
return 0;
}
002:找第一个只出现一次的字符
#include <iostream>
#include <cstdio>
using namespace std;
char a[100000];
int b[26];
int main(int argc, char const *argv[])
{
int i;
for (i = 0; i < 26; i++){
b[i] = 0;
}
cin >> a;
// int c;
// while ((c = getchar()) != '\n'){
// b[c-'a']++;
// }
for (i = 0; a[i]; i++){
b[a[i]-'a']++;
}
for (i = 0; a[i]; i++){
if (b[a[i]-'a'] == 1){
cout << a[i];
break;
}
}
if (a[i] == '\0'){
cout << "no";
}
return 0;
}
003:石头剪子布
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int stoi(char *s);
int main(int argc, char const *argv[])
{
char player1S[10], player2S[10];
int player1I, player2I, N;
scanf("%d", &N);
int i, a[N];
for (i = 0; i < N; i++){
scanf("%s %s", player1S, player2S);
player1I = stoi(player1S);
player2I = stoi(player2S);
int t = player1I - player2I;
if (t == 0){
a[i] = 0;
}else if (t == -1 || t == 2){
a[i] = 1;
}else{
a[i] = -1;
}
}
for (i = 0; i < N; i++){
if (a[i] == -1){
printf("%s\n", "Player2");
}else if (a[i] == 1){
printf("%s\n", "Player1");
}else{
printf("%s\n", "Tie");
}
}
return 0;
}
int stoi(char *s){
int p;
if (!strcmp(s, "Rock"))
p = -1;
else if (!strcmp(s, "Scissors"))
p = 0;
else
p = 1;
return p;
}
004:最长最短单词
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXSIZE 20000
#define MAXWORDSIZE 100
#define IN 1
#define OUT 0
char s[MAXSIZE];
char longest[MAXWORDSIZE];
char shortest[MAXWORDSIZE];
char tmp[MAXWORDSIZE];
int main(int argc, char const *argv[])
{
gets(s);
int state = OUT, max = -1, min = MAXWORDSIZE+1, len;
int i = 0, j = 0;
while (1){
if (s[i] == ' ' || s[i] == ',' || !s[i]){
if (state == IN){
tmp[j] = '\0';
j = 0;
len = strlen(tmp);
if (len > max){
max = len;
strcpy(longest, tmp);
}
if (len < min){
min = len;
strcpy(shortest, tmp);
}
if (!s[i]){
break;
}
}else{
i++;
continue;
}
state = OUT;
i++;
}
tmp[j++] = s[i++];
state = IN;
}
printf("%s\n", longest);
printf("%s\n", shortest);
return 0;
}
005:密码翻译
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXSIZE 80
char s[MAXSIZE];
int main(int argc, char const *argv[])
{
cin.getline(s, MAXSIZE);
int i;
for (i = 0; s[i]; i++){
if (s[i] == 'z'){
s[i] = 'a';
}else if (s[i] == 'Z'){
s[i] = 'A';
}else if (s[i] >= 'a' && s[i] <= 'y' || s[i] >= 'A' && s[i] <= 'Y'){
s[i] += 1;
}
}
cout << s;
return 0;
}
程序设计与算法(一)C语言程序设计 第八、九周习题(2019夏季)
001:指针练习:输出Hello
p = s; *p; p++
002:指针练习:输出Tesla
; *p1 != *p2; p1++
003:指针练习:ForEach
void (*f)(void *)
004:指针练习:Memcpy之一
int i;
for (i = 0; i < n; i++){
dest[i] = src[i];
}
005:指针练习:double
a[1], 6
006:指针练习:Memcpy之二
char *sourceCode = (char*)src;
char *destination = (char*)dest;
char b[size], i;
for (i = 0; i < size; i++){
b[i] = *sourceCode++;
}
char *tmp = b;
for (i = 0; i < size; i++){
*destination++ = *tmp++;
}
007:指针练习:MyMax
void *MyMax(void *anArray, int sizeOfAnItem, int numberOfItem,
int (*comparisonFunction)(void *n1, void*n2)){
char *tmp = (char*)anArray;
int i;
void *max = anArray;
for (i = 0; i < numberOfItem; i++){
if (comparisonFunction(tmp, max) > 0){
max = tmp;
}
tmp = tmp+sizeOfAnItem;
}
return max;
}
008:指针练习:指向指针的指针
int **p = a;
009:指针练习:SwapMemory
char *a1 = (char*)m1;
char *a2 = (char*)m2;
char c;
int i;
for (i = 0; i < size; i++){
c = *a1;
*a1++ = *a2;
*a2++ = c;
}
程序设计与算法(一)C语言程序设计 第十周习题(2019夏季)
001:成绩排序
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct student{
char name[20];
int score;
};
int main(){
int n;
scanf("%d", &n);
int i;
struct student a[20];
for (i = 0; i < n; i++){
scanf("%s %d", &a[i].name, &a[i].score);
}
struct student tmp;
int j, index;
for (i = 0; i < n; i++){
tmp = a[i];
index = i;
for (j = i; j < n; j++){
if (a[j].score > tmp.score){
tmp = a[j];
index = j;
}else if (a[j].score == tmp.score){
if (strcmp(tmp.name, a[j].name) > 0){
tmp = a[j];
index = j;
}
}
}
a[index] = a[i];
a[i] = tmp;
}
for (i = 0; i < n; i++){
printf("%s %d\n", a[i].name, a[i].score);
}
return 0;
}
002:分数线划定
#include <iostream>
#include <cstdio>
using namespace std;
struct candidate{
int ID;
int score;
};
int main(){
int n, m;
scanf("%d %d", &n, &m);
int i;
struct candidate a[n];
for (i = 0; i < n; i++){
scanf("%d %d", &a[i].ID, &a[i].score);
}
struct candidate tmp;
int j, index;
for (i = 0; i < n; i++){
tmp = a[i];
index = i;
for (j = i; j < n; j++){
if (a[j].score > tmp.score){
tmp = a[j];
index = j;
}else if (a[j].score == tmp.score){
if (a[j].ID < tmp.ID){
tmp = a[j];
index = j;
}
}
}
a[index] = a[i];
a[i] = tmp;
}
int t = m*3/2-1;
tmp = a[t];
int boundary = a[t].score;
while (tmp.score == boundary){
t++;
tmp = a[t];
}
printf("%d %d\n", boundary, t);
for (i = 0; i < t; i++){
printf("%d %d\n", a[i].ID, a[i].score);
}
return 0;
}
003:病人排队
#include <iostream>
#include <cstdio>
#define BOUNDARY 60
using namespace std;
struct patient{
char ID[10];
int age;
};
int main(){
int n;
scanf("%d", &n);
int i;
struct patient a[n];
for (i = 0; i < n; i++){
scanf("%s %d", &a[i].ID, &a[i].age);
}
struct patient tmp;
int j, index;
for (i = 0; i < n; i++){
for (j = n-1; j > i; j--){
if (a[j].age >= BOUNDARY){
if (a[j-1].age < a[j].age){
tmp = a[j];
a[j] = a[j-1];
a[j-1] = tmp;
}
}
}
}
for (i = 0; i < n; i++){
printf("%s\n", a[i].ID);
}
return 0;
}
004:mysort
void mysort(void *t, int numberOfElement, unsigned int widthOfElement,
int(*fCompare)(const void *, const void *)){
int i, j, k;
char *p = (char*)t;
char tmp;
for (i = 0; i < numberOfElement; i++){
for (j = numberOfElement-1; j > 0; j--){
char *a, *b;
a = p+(j)*widthOfElement;
b = p+(j-1)*widthOfElement;
if (fCompare(a, b) < 0){
for (k = 0; k < widthOfElement; k++){
tmp = *a;
*a++ = *b;
*b++ = tmp;
}
}
}
}
return;
}
005:从字符串中取数
static char *start;
if (str){
start = str;
}
double f = 0;;
for (; *start < '0' || *start > '9'; start++){
if (*start == '\0'){
return -1;
}
}
for (; *start >= '0' && *start <= '9'; start++){
f = 10*f+(*start-'0');
}
if (*start == '.'){
int tmp = 1;
start++;
for (; *start >= '0' && *start <= '9'; start++){
f = 10*f+(*start-'0');
tmp *= 10;
}
f /= tmp;
}
return f;
程序设计与算法(一)C语言程序设计 第十一周习题(2019夏季)
001:派
#include <iostream>
#include <cstdio>
#define PI 3.141592653589793
#define EPS 0.00001
using namespace std;
int main ()
{
int N, F;
scanf("%d %d", &N, &F);
int i, radius[N];
double volume[N];
double totalVolume = 0;
for (i = 0; i < N; i++){
scanf("%d", &radius[i]);
volume[i] = PI*radius[i]*radius[i];
totalVolume += volume[i];
}
double maxVolume = totalVolume/(F+1);
double left = 0, right = maxVolume, mid;
while (right-left >= EPS){
mid = left+(right-left)/2;
int k = 0;
for (i = 0; i < N; i++){
k += volume[i]/mid;
}
if (k >= F+1){
maxVolume = mid;
left = mid+EPS;
}else{
right = mid-EPS;
}
}
printf("%.3f\n", maxVolume);
return 0;
}
002:月度开销
#include <iostream>
#include <cstdio>
#define PI 3.141592653589793
#define EPS 0.00001
using namespace std;
int main ()
{
int N, M;
scanf("%d %d", &N, &M);
int i, cost[N], sum = 0, min, max = 0;
for (i = 0; i < N; i++){
scanf("%d", &cost[i]);
sum += cost[i];
if (cost[i] > max){
max = cost[i];
}
}
min = sum;
int left = max, right = sum, mid;
while (left <= right){
mid = left+(right-left)/2;
int tmp = 0, numberOfFajomonth = 1;
for (i = 0; i < N; i++){
tmp += cost[i];
if (tmp > mid){
tmp = cost[i];
numberOfFajomonth++;
}else if (tmp == mid){
tmp = 0;
numberOfFajomonth++;
}
}
if (numberOfFajomonth > M){
left = mid+1;
}else{
if (mid < min){
min = mid;
}
right = mid-1;
}
}
printf("%d\n", min);
return 0;
}
003:Aggressive cows
#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAXLENGTH 1000000000
using namespace std;
int main ()
{
int N, C;
scanf("%d %d", &N, &C);
int i, stall[N];
for (i = 0; i < N; i++){
scanf("%d", &stall[i]);
}
sort(stall, stall+N);
int left = 1, right = MAXLENGTH/C, mid;
int max = 1;
while (left <= right){
mid = left+(right-left)/2;
int j, k = 0;
for (j = C; j-1 && k < N; j--){
int t = stall[k]+mid;
k++;
while (k < N && stall[k] < t){
k++;
}
}
if (j > 1){
right = mid-1;
}else{
if (k >= N){
right = mid-1;
}else{
if (mid > max){
max = mid;
}
left = mid+1;
}
}
}
printf("%d\n", max);
return 0;
}
程序设计与算法(一)C语言程序设计 第十二周习题(2019夏季)
001:sort简单题
a+1, a+7, greater<int>()
002:还是sort简单题
struct Rule1{
bool operator()(const int & a1, const int & a2){
if (a1%10 != a2%10){
return a1%10 < a2%10;
}else{
return a1>a2;
}
}
};
struct Rule2{
bool operator()(const Point & a1, const Point & a2){
int t1, t2;
t1 = a1.x*a1.x+a1.y*a1.y;
t2 = a2.x*a2.x+a2.y*a2.y;
if (t1 != t2){
return t2>t1;
}else{
if (a1.x != a2.x){
return a2.x>a1.x;
}else{
return a2.y>a1.y;
}
}
}
};
003:Set
#include <iostream>
#include <set>
#include <cstring>
using namespace std;
struct od{
char a[4];
int b;
};
int main()
{
multiset<int> st;
multiset<int> st_cpy;
int n;
scanf("%d", &n);
int i;
struct od order[n];
for (i = 0; i < n; i++){
scanf("%s %d", order[i].a, &order[i].b);
}
for (i = 0; i < n; i++){
if (strcmp(order[i].a, "add") == 0){
st.insert(order[i].b);
st_cpy.insert(order[i].b);
multiset<int>::iterator j;
int t = 0;
for (j = st.begin(); j != st.end(); j++){
if (*j == order[i].b){
t++;
}
}
printf("%d\n", t);
}
if (strcmp(order[i].a, "del") == 0){
multiset<int>::iterator cs;
int t = 0;
for (cs = st.begin(); cs != st.end(); cs++){
if (*cs == order[i].b){
t++;
}
}
st.erase(order[i].b);
printf("%d\n", t);
}
if (strcmp(order[i].a, "ask") == 0){
int ever;
multiset<int>::iterator k;
k = st_cpy.find(order[i].b);
if (k == st_cpy.end()){
ever = 0;
}else{
ever = 1;
}
printf("%d ", ever);
multiset<int>::iterator j;
int t = 0;
for (j = st.begin(); j != st.end(); j++){
if (*j == order[i].b){
t++;
}
}
printf("%d\n", t);
}
}
return 0;
}
004:热血格斗场
#include <iostream>
#include <set>
#include <cstring>
#include <cstdlib>
using namespace std;
struct member{
int ID;
long long power;
};
struct rule{
bool operator()(const struct member & a1, const struct member a2){
return a1.power < a2.power;
}
};
struct competition{
int freshmanID;
int membershID;
};
struct member minp(struct member a, struct member b, struct member c);
int main()
{
set<struct member, rule> st;
int n;
scanf("%d", &n);
struct member a[n+1], tmpA, tmpB, tmp;
struct competition c[n];
a[0].ID = 1;
a[0].power = 1000000000;
st.insert(a[0]);
int i;
set<struct member, rule>::iterator j;
for (i = 1; i < n+1; i++){
scanf("%d %lld", &a[i].ID, &a[i].power);
j = st.lower_bound(a[i]);
tmpA.power = j->power;
tmpA.ID = j->ID;
tmpB = tmpA;
if (j != st.begin()){
j--;
tmpB.power = j->power;
tmpB.ID = j->ID;
}
tmp = minp(tmpA, tmpB, a[i]);
c[i-1].freshmanID = a[i].ID;
c[i-1].membershID = tmp.ID;
st.insert(a[i]);
}
for (i = 0; i < n; i++){
printf("%d %d\n", c[i].freshmanID, c[i].membershID);
}
return 0;
}
struct member minp(struct member a, struct member b, struct member c){
int t1, t2;
t1 = abs(c.power-a.power);
t2 = abs(c.power-b.power);
if (t1 >= t2){
return b;
}else{
return a;
}
}
程序设计与算法(一)C语言程序设计 第十三周习题(2019夏季)
001:冷血格斗场
#include <iostream>
#include <map>
#include <cstring>
#include <cstdlib>
using namespace std;
struct competition{
int freshmanID;
int memberID;
};
int main()
{
map<long long, int> st;
int n;
scanf("%d", &n);
st[1000000000] = 1;
struct competition c[n];
int i, id;
long long power, diff1, diff2;
map<long long, int>::iterator j, jtmp;
for (i = 1; i < n+1; i++){
scanf("%d %lld", &id, &power);
j = st.lower_bound(power);
if (j == st.end()){
j--;
}
diff1 = abs(j->first-power);
jtmp = j;
diff2 = diff1;
if (j != st.begin()){
j--;
diff2 = abs(j->first-power);
}
if (diff1 > diff2 || diff1 == diff2 && jtmp->second > j->second){
jtmp = j;
}
c[i-1].freshmanID = id;
c[i-1].memberID = jtmp->second;
if (jtmp->first == power && jtmp->second < id){
;
}else{
st[power] = id;
}
}
for (i = 0; i < n; i++){
printf("%d %d\n", c[i].freshmanID, c[i].memberID);
}
return 0;
}
程序设计与算法(一)C语言程序设计 期末考试(2019夏季)
01:分段函数
#include <iostream>
#include <cstdio>
#define EPS 0.000001
using namespace std;
int main(){
double x, y;
scanf("%lf", &x);
if (x>-EPS && x-5 < -EPS){
y = -x+2.5;
}else if (x-5 > -EPS && x-10 < -EPS){
y = 2-1.5*(x-3)*(x-3);
}else if (x-10 > -EPS && x-20 < -EPS){
y = x/2-1.5;
}
printf("%.3f", y);
return 0;
}
02:求分数序列和
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
double sum = 0;
int n, p = 1, q = 2, tmp;
scanf("%d", &n);
for (int i = 0; i < n; i++){
sum += 1.0*q/p;
tmp = q;
q = q+p;
p = tmp;
}
printf("%.4f", sum);
return 0;
}
03:画矩形
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int height, width, isFilled;
char fc;
scanf("%d %d %c %d", &height, &width, &fc, &isFilled);
for (int i = 0; i < height; i++){
if (isFilled){
for (int j = 0; j < width; j++){
printf("%c", fc);
}
}else{
if (i == 0 || i == height-1){
for (int j = 0; j < width; j++){
printf("%c", fc);
}
}else{
for (int j = 0; j < width; j++){
if (j == 0 || j == width-1){
printf("%c", fc);
}else{
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
04:求出e的值
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
double e = 1;
int n;
long long factorial;
scanf("%d", &n);
for (int i = 1; i <= n; i++){
factorial = 1;
for (int j = 1; j <= i; j++){
factorial *= j;
}
e += 1.0/factorial;
}
printf("%.10f", e);
return 0;
}
05:计算多项式的值
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
float x, polynomial = 1, product = 1;
int n;
scanf("%f %d", &x, &n);
for (int i = 1; i <= n; i++){
product *= x;
polynomial += product;
}
printf("%.2f", polynomial);
return 0;
}
06:大整数加法
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char a[210], b[210], c[210];
void reverse(char *s);
void plusHugeInt(char *a, char *b, char *sum);
void printHugeInt(char *s);
int main(){
cin >> a >> b;
reverse(a);
reverse(b);
plusHugeInt(a, b, c);
printHugeInt(c);
return 0;
}
void reverse(char *s){
int len = strlen(s), tmp;
int i = 0, j = len-1;
while (i < j){
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
i++;
j--;
}
return;
}
void plusHugeInt(char *a, char *b, char *c){
int lenA = strlen(a), lenB = strlen(b);
int i, jw = 0, sum;
for (i = 0; i < lenA && i < lenB; i++){
sum = a[i]-'0'+b[i]-'0'+jw;
c[i] = sum%10+'0';
jw = sum/10;
}
while (i < lenA){
sum = a[i]-'0'+jw;
c[i] = sum%10+'0';
jw = sum/10;
i++;
}
while (i < lenB){
sum = b[i]-'0'+jw;
c[i] = sum%10+'0';
jw = sum/10;
i++;
}
if (jw == 1){
c[i++] = '1';
}
c[i] = '\0';
return;
}
void printHugeInt(char *s){
int j = -1, lenS = strlen(s);
for (int i = 0; i < lenS; i++){
if (s[i]-'0' > 0){
j = i;
}
}
if (j == -1 ){
printf("0");
}else{
s[j+1] = '\0';
for (int k = strlen(s)-1; k >= 0; k--){
printf("%c", s[k]);
}
}
return;
}
07:有趣的跳跃
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
int n;
cin >> n;
if (n == 1){
printf("Jolly");
}else{
int a[n], difference[n-1], check[n-1], isJolly = 1;
for (int i = 0; i < n; i++){
cin >> a[i];
}
for (int i = 0; i < n-1; i++){
check[i] = 0;
difference[i] = abs(a[i]-a[i+1]);
}
for (int i = 0; i < n-1; i++){
if (difference[i] == 0 || difference[i] > n-1){
break;
}else{
check[difference[i]-1] = 1;
}
}
for (int i = 0; i < n-1; i++){
if (check[i] == 0){
printf("Not jolly");
isJolly = 0;
break;
}
}
if (isJolly){
printf("Jolly");
}
}
return 0;
}
08:单词倒排
#include <iostream>
#include <cstring>
#define MAXSIZE 100
using namespace std;
char s[MAXSIZE];
char *p[MAXSIZE];
int main(){
for (int i = 0; i < MAXSIZE; i++){
p[i] = NULL;
}
cin.getline(s, MAXSIZE);
char *tmp;
tmp = strtok(s, " ");
int i = 0;
while (tmp){
p[i++] = tmp;
tmp = strtok(NULL, " ");
}
for (int j = i-1; j >= 0; j--){
cout << p[j] << " ";
}
return 0;
}
09:行程长度编码
#include <iostream>
#include <cstring>
#include <cctype>
#define MAXSIZE 1000
using namespace std;
struct charAndNumber{
char c;
int n;
};
char s1[MAXSIZE];
struct charAndNumber s2[MAXSIZE];
int main(){
cin >> s1;
for (int i = 0; i < MAXSIZE; i++){
s2[i].c = '\0';
s2[i].n = 0;
}
s2[0].c = toupper(s1[0]);
s2[0].n++;
int len = strlen(s1), j = 0;
for (int i = 1; i < len; i++){
if (s1[i] == s2[j].c || s1[i]+'A'-'a' == s2[j].c){
s2[j].n++;
}else{
j++;
s2[j].c = toupper(s1[i]);
s2[j].n++;
}
}
for (int i = 0; i <= j; i++){
cout << "(" << s2[i].c << "," << s2[i].n << ")";
}
return 0;
}
10:反反复复
#include <iostream>
#include <cstring>
#define MAXSIZE 200
using namespace std;
char s[MAXSIZE];
int main(){
int line, row;
cin >> row >> s;
line = strlen(s)/row;
for (int i = 0; i < row; i++){
int k = i;
/* 注意题目中并不是把字符串从上到下地一列一列填成的矩阵按每行
从左到右输出,而是一行从左到右,一行从右到左。所以得到的加密后
的字符串,可以把从右到左输出的部分先颠倒,再把一个矩阵一行一行
输出。如果不想做颠倒这一步,就可以像下面这样,一行输出整数第a个,
下一行输出倒数第a个。 */
for (int j = 0; j < line; j++){
printf("%c", s[j*row+k]);
k = row-1-k;
}
}
return 0;
}
11:网线主管
#include <cstdio>
using namespace std;
/* 这题一开始用的double,怎么都不行,有的时候自己电脑上可以,传上去也不行,
实在是无法理解,不知道怎么用double的变量来做。我是只能通通乘100用整数来了 */
int main(){
int N, K;
scanf("%d %d", &N, &K);
int a[N];
/* 注意,单位用厘米时,所有的都乘100,和sum用8字节的int可能就不够了。
我有个测试数据过不了就是因为这个。 */
long long sum = 0;
double tmpA;
for (int i = 0; i < N; i++){
scanf("%lf", &tmpA);
a[i] = tmpA*100;
sum += a[i];
}
/* 枚举+二分法 */
/* 注意,left不能用0,不然下面a[i]/mid的时候,除数可能为0,就出错了,
有个测试数据过不了就是因为一开始left是0。我电脑上的提示是
float exception:8。反正就是出错。况且题目中要求如果小于max小于1就输出0.00,
所以也left也没必要设置成1以下的。 */
int left = 1, right = sum/K, mid, max = 0;
int tmp;
while (left <= right){
mid = left+(right-left)/2;
tmp = 0;
for (int i = 0; i < N; i++){
tmp += a[i]/mid;
}
if (tmp >= K){
max = mid;
left = mid+1;
}else{
right = mid-1;
}
}
if (max < 1){
printf("0.00");
}else{
printf("%.2f", 1.0*max/100);
}
return 0;
}
12:查找最接近的元素
#include <iostream>
using namespace std;
int main(){
int n, m;
cin >> n;
int a[n];
for (int i = 0; i < n; i++){
cin >> a[i];
}
cin >> m;
int test, left, right, mid, l, r;
for (int i = 0; i < m; i++){
cin >> test;
left = 0;
right = n-1;
l = a[0];
r = a[n-1];
while (left <= right){
mid = left+(right-left)/2;
if (a[mid] > test){
r = a[mid];
right = mid-1;
}else{
l = a[mid];
left = mid+1;
}
}
if (l == test){
printf("%d\n", l);
}else{
if (test-l <= r-test){
printf("%d\n", l);
}else{
printf("%d\n", r);
}
}
}
return 0;
}
13:1的个数
#include <iostream>
using namespace std;
int main(){
int N;
cin >> N;
int n = N, cnt = 0;
for (int i = 0; i < 32; i++){
if (n&1 == 1){
cnt++;
}
n >>= 1;
}
printf("%d", cnt);
return 0;
}
14:判断元素是否存在
#include <iostream>
#include <cstdio>
#define MAXSIZE 100000
using namespace std;
int main(){
long long a[MAXSIZE];
int k, x;
int isFind = 0;
scanf("%d,%d", &k, &x);
a[0] = k;
for (int i = 1; i < MAXSIZE; i++){
if (i%2 == 1){
a[i] = 2*a[(i+1)/2-1]+1;
}else{
a[i] = 3*a[(i+1)/2-1]+1;
}
}
for (int i = 0; i < MAXSIZE; i++){
if (a[i] == x){
isFind = 1;
break;
}
}
if (isFind){
printf("YES");
}else{
printf("NO");
}
return 0;
}
原文:https://www.cnblogs.com/Orzst/p/11416125.html