C语言编程题

  • 格式:docx
  • 大小:47.19 KB
  • 文档页数:41

1.短信计费用手机发短信,一般一条短信资费为0.1 元,但限定每条短信的内容在70 个字以内(包括70 个字)。

如果你所发送的一条短信超过了70 个字,则大多数手机会按照每70 个字一条短信的限制把它分割成多条短信发送。

假设已经知道你当月所发送的每条短信的字数,试统计一下你当月短信的总资费。

#include <set>#include <map>#include <vector>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define ll long long#define inf 2147483647 using namespace std;int n;int main(){cin >> n;double ans = 0;for(int i = 1; i <= n; i++){int x;cin >> x;ans = ans + ((x - 1) / 70 + 1) * 0.1;}printf("%.1lf\n", ans);return 0;}2.集体照医学部口腔 3 班 n 位同学约定拍集体照, n 大于 1 且不超过 100 。

摄影师要求同学按照身高站成两排,保证第二排的人身高都要大于等于第一排的人,且第二排的人数和第一排的人数相等或者比第一排多一个人。

输入 n 位同学的身高,请问第二排中身高最矮的人的身高是多少?#include <set>#include <map>#include <vector>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define ll long long#define inf 2147483647using namespace std;int n;int a[105];int main(){while(scanf("%d", &n)){if(n == 0)return 0;for(int i = 1; i <= n; i++)cin >> a[i];sort(a + 1, a + n + 1);cout << a[n / 2 + 1] << endl;}return 0;}3.1020 跳格问题有一种游戏,在纸上画有很多小方格,第一个方格为起点(S) ,最后一个方格为终点。

有一个棋子,初始位置在起点上,棋子每次可移动一次,棋子在起点时,可向前移动一个格子到第二个方格内;棋子在其他方格内时,可根据方格内的数字Ni 进行移动。

如果 Ni 大于零,就向前移动 Ni 个格子;如果 Ni 小于零,就向后移动 -Ni 个格子;如果 Ni 等于零,则此次原地不动一次,在下一步移动时可向前移动一步到下一个格子。

显然,如果仅按此方案,会出现棋子永远移动不到终点的情形。

为防止这种情况发生,我们规定,当棋子再次来到它曾经到过的方格时,它需要原地不动一次,在下一步移动时可向前移动一步到下一个格子。

按此方案,棋子总能够走到终点 (F) 。

如果给定一个方格图,试求棋子要走多少步才能从起点走到终点。

(注:当然还可能会出现向前移动Ni 个格子就跑过终点了,则把棋子放到终点上。

如果 Ni 太小,使得棋子向后移动跑过了起点,则把棋子放到起点上。

)(如图所示,其中S代表起点, F 代表终点)(只有离开后再次来到一个方格时,才算来到它曾经到过的方格,包括起点 S )#include <set>#include <map>#include <vector>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define ll long long#define inf 2147483647using namespace std;int n, ans;int a[105];bool vis[105];void dfs(int x){if(x < 1)x = 1;if(x > n + 2)x = n + 2;if(vis[x]){if(a[x])ans += 2;else ans += 1;dfs(x + 1);}else{vis[x] = 1;if(x == n + 2)return;ans++;dfs(x + a[x]); }}int main(){scanf("%d", &n);a[1] = 1;for(int i = 2; i <= n + 1; i++)cin >> a[i];dfs(1);cout << ans << endl;return 0;}4.配对碱基链脱氧核糖核酸( DNA )由两条互补的碱基链以双螺旋的方式结合而成。

而构成DNA 的碱基共有 4 种,分别为腺瞟呤( A )、鸟嘌呤( G )、胸腺嘧啶(T )和胞嘧啶( C )。

我们知道,在两条互补碱基链的对应位置上,腺瞟呤总是和胸腺嘧啶配对,鸟嘌呤总是和胞嘧啶配对。

你的任务就是根据一条单链上的碱基序列,给出对应的互补链上的碱基序列。

#include <set>#include <map>#include <vector>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define ll long long#define inf 2147483647using namespace std;int n;string x;int main(){scanf("%d", &n);for(int i = 1; i <= n; i++){cin >> x;for(int j = 0; j < x.length(); j++) {if(x[j] == 'T')x[j] = 'A';else if(x[j] == 'A')x[j] = 'T'; else if(x[j] == 'C')x[j] = 'G';elsex[j] = 'C';}cout << x << endl;}return 0;}5.打鱼还是晒网中国有句俗语叫“三天打鱼两天晒网”。

某人从 1990 年 1 月 1 日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。

注意要区分闰年和不是闰年的两种情况#include <set>#include <map>#include <vector>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define ll long long#define inf 2147483647using namespace std;int y, m, d;int mm[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int tot;int main(){cin >> y >> m >> d;for(int i = 1990; i < y; i++)if((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)tot += 366;else tot += 365;for(int i = 1; i < m; i++)if(((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) && i == 2) tot += 29;elsetot += mm[i - 1];tot += (d - 1);if(tot % 5 <= 2)puts("fishing"); elseputs("sleeping");return 0;}6.优先队列给定一个初始数字集合,对其做如下两种操作:1.添加一个新的数字2.将集合中当前最小的数字取出(输出并删除),如果最小的数字有多个,只取出其中一个。

#include <set>#include <map>#include <queue>#include <vector>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define ll long long#define inf 2147483647using namespace std;int n, m;struct data{int a[200005];int cnt;bool empty(){return cnt == 0;}void push(int x){a[++cnt] = x;int p = cnt;while(p > 1 && a[p] < a[p / 2]){swap(a[p], a[p / 2]);p = p / 2;}}int top(){return a[1];}void down(int x){int t = x << 1;if(t > cnt)return;if(t + 1 <= cnt && a[t + 1] < a[t])t++;if(a[t] < a[x]){swap(a[t], a[x]);down(t);}}void pop(){a[1] = a[cnt];cnt--;down(1);}}q;int main(){cin >> n;for(int i = 1; i <= n; i++){int x;cin >> x;q.push(x); }cin >> m;char opt[15];for(int i = 1; i <= m; i++){scanf("%s", opt);if(opt[0] == 'E'){if(q.empty())puts("NULL");else{cout << q.top() << endl;q.pop();}}else{int x;cin >> x;q.push(x);} }return 0;}7.护林员盖房子在一片保护林中,护林员想要盖一座房子来居住,但他不能砍伐任何树木。