C,C++语法基础 | 循环语句 | 03
循环语句 | 03
for
while
do...while
习题三
偶数
#include<iostream>
using namespace std;
int main(){
for(int i=1;i<=100;i++)
if(!(i&1))cout << i << endl;
return 0;
}
注意了,位运算符的优先级很低很低.
奇数
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
for(int i=1;i<=n;i++)
if(i&1)cout << i << endl;
return 0;
}
正数
#include<iostream>
using namespace std;
int main(){
int cnt=0;
for(int i=0;i<6;i++){
double x;
cin >> x;
if(x>0)cnt++;
}
printf("%d positive numbers\n",cnt);
return 0;
}
连续奇数的和 1
#include<iostream>
using namespace std;
int main(){
int sum=0;
int a,b;
cin >> a >> b;
if(a>b)swap(a,b);
for(int i=a+1;i<b;i++){
if(i&1)sum+=i;
}
cout << sum << endl;
return 0;
}
最大数和它的位置
#include<iostream>
using namespace std;
int main(){
int m=-0x7ffffff,idx=-1; // 最大数和位置
for(int i=0;i<100;i++){
int x;
cin >> x;
if(x>m)m=x,idx=i+1;
}
cout << m << endl << idx << endl;
return 0;
}
递增序列
#include<iostream>
using namespace std;
int main(){
int n;
while(cin>>n,n!=0){
for(int i=1;i<=n;i++){
cout << i << " ";
}
cout << endl;
}
return 0;
}
连续整数相加
#include<iostream>
using namespace std;
int main(){
int a,b;
while(cin>>a && a<=0);
while(cin>>b && b<=0);
int sum=0;
for(int i=0,x=a;i<b;i++,x++){
sum += x;
}
cout << sum << endl;
return 0;
}
约数
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
for(int i=1;i<=n;i++){
if(n%i==0)cout << i << endl;
}
return 0;
}
PUM
#include<iostream>
using namespace std;
int main(){
int r,c;
cin >> r >> c;
int x = 1;
for(int i=0;i<r;i++){
for(int j=0;j<c-1;j++){
cout << x++ << " " ;
}
cout << "PUM" << endl,x++;
}
return 0;
}
余数
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
for(int i=1;i<10000;i++)
if(i%n==2)cout << i << endl;
return 0;
}
六个奇数
#include<iostream>
using namespace std;
int main(){
int x;
cin >> x;
for(int i=x,cnt=6;cnt;i++){
if(i&1)cnt--,cout << i << endl;
}
return 0;
}
乘法表
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
for(int i=1;i<=10;i++){
printf("%d x %d = %d\n",i,n,i*n);
}
return 0;
}
实验
#include<iostream>
#include<unordered_map>
using namespace std;
int main(){
int n;
cin >> n;
unordered_map<string,int> M;
while(n--){
string s;
int x;
cin >> x >> s;
if(M[s])M[s]+=x;
else M[s] = x;
}
int total = M["C"] + M["F"] + M["R"];
printf("Total: %d animals\n",total);
printf("Total coneys: %d\n",M["C"]);
printf("Total rats: %d\n",M["R"]);
printf("Total frogs: %d\n",M["F"]);
printf("Percentage of coneys: %.2lf %%\n",(M["C"]*100)/(double)total);
printf("Percentage of rats: %.2lf %%\n",(M["R"]*100)/(double)total);
printf("Percentage of frogs: %.2lf %%\n",(M["F"]*100)/(double)total);
return 0;
}
区间 2
#include<iostream>
using namespace std;
int main(){
int in=0,out=0;
int n;
cin >> n;
while(n--){
int x;
cin >> x;
if(x>=10 && x<=20)in++;
else out++;
}
printf("%d in\n%d out\n",in,out);
return 0;
}
连续奇数的和 2
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
while(n--){
int x,y;
cin >> x >> y;
if(x>y)swap(x,y);
int sum=0;
for(int i=x+1;i<y;i++)if(i&1)sum+=i;
cout << sum << endl;
}
return 0;
}
简单斐波那契
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
int a=0,b=1;
for(int i=0;i<n;i++){
cout << a << " ";
int c = a + b;
a=b,b=c;
}
return 0;
}
数字序列和它的和
#include<iostream>
using namespace std;
int main(){
int a,b;
while(cin>>a>>b,a>0&&b>0){
if(a>b)swap(a,b);
int sum=0;
for(int i=a;i<=b;i++)sum+=i,cout << i << " ";
printf("Sum=%d\n",sum);
}
return 0;
}
完全数
c++
一秒运行不超过10**9
,需要进行优化.
这里需要注意一个求约数的优化,因为约数都是成对出现的.
#include<iostream>
using namespace std;
bool is_perfect(int x){
int sum=0;
for(int i=1;i<=x/i;i++){
if(x%i==0){
if(i<x)sum+=i;
if(i!=x/i && x/i < x)sum += x/i;
}
}
return x==sum;
}
int main(){
int n;
cin >> n;
while(n--){
int x;
cin >> x;
if(is_perfect(x))printf("%d is perfect\n",x);
else printf("%d is not perfect\n",x);
}
return 0;
}
质数
#include<iostream>
using namespace std;
bool is_prime(int x){
for(int i=2;i<=x/i;i++){
if(x%i==0)return false;
}
return true;
}
int main(){
int n;
cin >> n;
while(n--){
int x;
cin >> x;
if(is_prime(x))printf("%d is prime\n",x);
else printf("%d is not prime\n",x);
}
return 0;
}
菱形
曼哈顿距离 (x1,y1),(x2,y2)
, 其距离为|x1-x2| + |y1 - y2|
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n;
cin >> n;
int c_x = n/2;
int c_y = n/2;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int d = abs(i-c_x) + abs(j-c_y);
if(d<=n/2)cout << "*";
else cout << " ";
}
cout << endl;
}
return 0;
}