大一C語言程式設計練習題四
- A - 無劍無我
- B - 最小公倍數
- C - 密碼
- D - Vote
- E - 進制轉換
A - 無劍無我
Description
北宋末年,奸臣當道,宦官掌權,外侮日亟,遼軍再犯,時下戰火連連,烽煙四起,哀鴻遍野,民不聊生,又有眾多能人異士群起而反,天下志士云集回應,景糧影從,
值此危急存亡之秋,在一個與世隔絕的地方—MCA山上一位江湖人稱<英雄哪里出來>的人正在為抗擊遼賊研究劍法,終于于一雷電交加之夜精確計算出了蕩劍回鋒的劍氣傷害公式,
定義 f(x, y, m, n) = sqrt(xx + yy + mm + nn - 2mx - 2ny);
hint : sqrt表示開方,即sqrt(4) = 2; sqrt(16) = 4;
(其中x,y為位置變數,m,n為屬性常量)
劍氣傷害 = f(x, y, a, b) + f(x, y, c, d);
劍氣威力巨大無比,實難控制,現在他想知道劍氣傷害的最小傷害值,
Input
首先輸入一個t,表示有t組資料,跟著t行:
輸入四個實數a,b,c,d均小于等于100
Output
輸出劍氣的最小傷害值M,保留小數點后一位
(可以使用.1lf)
Sample Input
2
0 0 3 4
4 0 0 3
Sample Output
5.0
5.0
代碼如下(示例):
#include <stdio.h>
#include <math.h>
void main()
{
float x,y,m,n;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%f%f%f%f",&x,&y,&m,&n);
printf("%.1lf\n",sqrt(x*x + y*y + m*m + n*n - 2*m*x - 2*n*y));
}
}
B - 最小公倍數
Description
給定兩個正整數,計算這兩個數的最小公倍數,
Input
輸入包含多組測驗資料,每組只有一行,包括兩個不大于1000的正整數.
Output
對于每個測驗用例,給出這兩個數的最小公倍數,每個實體輸出一行,
Sample Input
10 14
Sample Output
70
代碼如下(示例):
#include<stdio.h>
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
int n;
for (; b != 0;)
n = a % b, a = b, b = n;
return a;
}
int lcm(int a,int b,int x)
{
int y;
y = a * b / x;
return y;
}
int main()
{
int a, b, t;
while (scanf_s("%d %d", &a, &b) != EOF)
{
int x, y;
if (a < b) swap(a, b);
x = gcd(a, b);
y = lcm(a, b, x);
printf("%d\n", y);
}
return 0;
}
C - 密碼
Description
網上流傳一句話:“常在網上飄啊,哪能不挨刀啊~”,其實要想能安安心心地上網其實也不難,學點安全知識就可以,
首先,我們就要設定一個安全的密碼,那什么樣的密碼才叫安全的呢?一般來說一個比較安全的密碼至少應該滿足下面兩個條件:
(1).密碼長度大于等于8,且不要超過16,
(2).密碼中的字符應該來自下面“字符類別”中四組中的至少三組,
這四個字符類別分別為:
1.大寫字母:A,B,C…Z;
2.小寫字母:a,b,c…z;
3.數字:0,1,2…9;
4.特殊符號:~,!,@,#,$,%,^;
給你一個密碼,你的任務就是判斷它是不是一個安全的密碼,
Input
輸入資料第一行包含一個數M,接下有M行,每行一個密碼(長度最大可能為50),密碼僅包括上面的四類字符,
Output
對于每個測驗實體,判斷這個密碼是不是一個安全的密碼,是的話輸出YES,否則輸出NO,
Sample Input
3
a1b2c3d4
Linle@ACM
~@^@!%
Sample Output
NO
YES
NO
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void isspecial(char ch[],int k)
{
int i, flag=0;
int n1, n2, n3, n4, N;
n1 = n2 = n3 = n4 = 0;
if (k >= 8 && k <= 16)
{
for (i = 0; i < k; i++)
{
if (isdigit(ch[i]))
{
flag++;
n1 = 1;
continue;
}
else if (islower(ch[i]))
{
flag++;
n2 = 1;
continue;
}
else if (isupper(ch[i]))
{
flag++;
n3 = 1;
continue;
}
else if (ch[i] == '!' || ch[i] == '@' || ch[i] == '#' || ch[i] == '$' || ch[i] == '%' || ch[i] == '^' || ch[i] == '~')
{
flag++;
n4 = 1;
continue;
}
else printf("NO\n");
}
N = n1 + n2 + n3 + n4;
if (flag == k && N > 2)printf("YES\n");
else printf("NO\n");
}
else printf("NO\n");
}
int main()
{
char ch[100];
int n, k;
scanf_s("%d", &n);
getchar();
while (n--)
{
gets_s(ch);
k = strlen(ch);
isspecial(ch,k);
memset(ch, 0, sizeof(ch));
}
}
D - Vote
Description
美國大選是按各州的投票結果來確定最終的結果的,如果得到超過一半的州的支持就可以當選,而每個州的投票結果又是由該州選民投票產生的,如果某個州超過一半的選民支持希拉里,則她將贏得該州的支持,現在給出每個州的選民人數,請問希拉里至少需要贏得多少選民的支持才能當選?
Input
多組輸入資料
每組資料的第一行包括一個整數N(1<=N<=101),表示美國的州數,N=0表示輸入結束
接下來一行包括N個正整數,分別表示每個州的選民數,每個州的選民數不超過100
Output
對于每組資料輸出一行,表示希拉里至少需要贏得支持的選民數
Sample Input
3
5 7 5
0
Sample Output
6
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
while (scanf_s("%d", &n) && n)
{
int i;
int sum = 0;
int a[101];
for (i = 0; i < n; i++)
scanf_s("%d", &a[i]);
sort(a, a + n);
for (i = 0; i < n / 2 + 1; i++)
sum += a[i]/2+1;
cout << sum << '\n';
}
return 0;
}
E - 進制轉換
Description
輸入一個十進制數N,將它轉換成R進制數輸出,
Input
輸入資料包含多個測驗實體,每個測驗實體包含兩個整數N(32位整數)和R(2<=R<=16, R<>10),
Output
為每個測驗實體輸出轉換后的數,每個輸出占一行,如果R大于10,則對應的數字規則參考16進制(比如,10用A表示,等等),
Sample Input
7 2
23 12
-4 3
Sample Output
111
1B
-11
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n, R;
char a[1000];
char b[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
while (scanf_s("%d%d", &n, &R)!=EOF)
{
memset(a, 0, sizeof(a));
int flag = 0;
int i = 0;
if (n < 0) { n = -n; flag = 1; }
while (n)
{
int t;
t = n % R;
a[i] = b[t];
n /= R;
i++;
}
if (flag == 1)printf("-");
while (i--)
cout << a[i];
cout << '\n';
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/234382.html
標籤:其他
下一篇:作業系統第一章 概述
