2017.03.18
5個(gè)數(shù)求最值
描述
設(shè)計(jì)一個(gè)從5個(gè)整數(shù)中取最小數(shù)和最大數(shù)的程序
輸入
輸入只有一組測(cè)試數(shù)據(jù),為五個(gè)不大于1萬(wàn)的正整數(shù)
輸出
輸出兩個(gè)數(shù),第一個(gè)為這五個(gè)數(shù)中的最小值,第二個(gè)為這五個(gè)數(shù)中的最大值,兩個(gè)數(shù)字以空格格開。
樣例輸入
1 2 3 4 5
樣例輸出
1 5
代碼實(shí)現(xiàn)
#include <iostream>
using namespace std;
int main(){
int num[5];
int min,max;
for(int i=0;i<5;i++){
cin>>num[i];
}
min = max = num[0];
for(int j=0;j<5;j++){
if(min>num[j]){
min = num[j];
}
if(max < num[j]){
max = num[j];
}
}
cout<<min<<" "<<max;
return 0;
}
水仙花數(shù)
描述
請(qǐng)判斷一個(gè)數(shù)是不是水仙花數(shù)。
其中水仙花數(shù)定義各個(gè)位數(shù)立方和等于它本身的三位數(shù)。
輸入
有多組測(cè)試數(shù)據(jù),每組測(cè)試數(shù)據(jù)以包含一個(gè)整數(shù)n(100<=n<1000)
輸入0表示程序輸入結(jié)束。
輸出
如果n是水仙花數(shù)就輸出Yes
否則輸出No
樣例輸入
153
154
0
樣例輸出
Yes
No
代碼實(shí)現(xiàn)
#include <iostream>
#include<stdio.h>
#include<vector>
using namespace std;
int main(){
vector<string> v;
int temp;
while(true){
cin>>temp;
if(temp == 0){
break;
}else{
int sum = 0,num = temp;
for(;num>0;num/=10){
sum += (num%10) * (num%10)*(num%10);
}
if(temp == sum){
v.push_back("Yes");
}else{
v.push_back("No");
}
}
}
for(int i=0;i<v.size();i++){
cout<<v[i]<<endl;
}
return 0;
}
三個(gè)數(shù)從小到大排序
描述
現(xiàn)在要寫一個(gè)程序,實(shí)現(xiàn)給三個(gè)數(shù)排序的功能
輸入
輸入三個(gè)正整數(shù)
輸出
給輸入的三個(gè)正整數(shù)排序
樣例輸入
20 7 33
樣例輸出
7 20 33
代碼實(shí)現(xiàn)
#include <iostream>
using namespace std;
int main(){
int num[3];
for(int i=0;i<3;i++)
cin>>num[i];
for(int i=0;i<3;i++){
int min = num[i];
for(int j=i+1;j<3;j++){
if(num[i]>num[j]){
int temp = num[j];
num[j] = num[i];
num[i] = temp;
}
}
}
for(int i=0;i<3;i++){
cout<<num[i]<<" ";
}
return 0;
}
2017.03.19
素?cái)?shù)求和問題
描述
現(xiàn)在給你N個(gè)數(shù)(0<N<1000),現(xiàn)在要求你寫出一個(gè)程序,找出這N個(gè)數(shù)中的所有素?cái)?shù),并求和。
輸入
第一行給出整數(shù)M(0<M<10)代表多少組測(cè)試數(shù)據(jù)
每組測(cè)試數(shù)據(jù)第一行給你N,代表該組測(cè)試數(shù)據(jù)的數(shù)量。
接下來的N個(gè)數(shù)為要測(cè)試的數(shù)據(jù),每個(gè)數(shù)小于1000
輸出
每組測(cè)試數(shù)據(jù)結(jié)果占一行,輸出給出的測(cè)試數(shù)據(jù)的所有素?cái)?shù)和
樣例輸入
3
5
1 2 3 4 5
8
11 12 13 14 15 16 17 18
10
21 22 23 24 25 26 27 28 29 30
樣例輸出
10
41
52
代碼實(shí)現(xiàn)
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
int getSum(int *num,int size){
int result = 0;
for(int i=0;i<size;i++){
int number = num[i];
bool istrue = true;
if(number == 1){
continue;
}
if(number == 2 || number == 3){
result += number;
continue;
}
for(int j= 2;j<=sqrt(number);j++){
if(number % j == 0){
istrue = false;
break;
}
}
if(istrue){
result += number;
}
}
return result;
}
int main(){
int n;
cin>>n;
int *result = new int[n];
for(int i=0;i<n;i++){
int group;
cin>>group;
int *num = new int[group];
for(int j=0;j<group;j++){
cin>>num[j];
}
result[i] = getSum(num,group);
delete []num;
}
for(int i=0;i<n;i++)
cout<<result[i]<<endl;
delete []result;
return -1;
}