// 判断指定数是否为质数 bool is(int n){ if(n ==1)return false; if(n ==2)return true; double d =sqrt(n)+1; for(int i =2; i < d;++i){ if(n % i ==0)return false; } return true; }
intmain(){ int record[1000];// 记录质数 int size =0;// 质数的数量 int m, n; scanf("%d %d",&m,&n); unsignedlonglong sum =0; for(int i = m; i <= n;++i){ if(is(i)){ record[size++]= i; sum += i; } } for(int i =0; i != size;++i){ if(i %5==0&& i !=0)printf("\n"); printf("%d ", record[i]); } printf("\namount=%d sum=%llu", size, sum); return0; }
输入一个整数,在一行中输出相应个数的星号
题干
输入样例
5
结尾无空行
输出样例
*****
结尾无空行
题解
1 2 3 4 5 6
intmain(){ int n; scanf("%d",&n); while(n--)printf("*"); return0; }
// 求区间内的最优解 // 参数: // start - 起始位置(包括) // length - 长度 // 返回: // 跳跃到的下标 intmax(int start,int length){ int end = start + length; // 如果可以直接跳到终点则返回n if(end >= n)return n; int max =-1; int index =-1; // 找到可以跳的最远的点 for(int i = start; i != end;++i){ if(road[i]+ i > max){ max = road[i]+ i; index = i; } } return index; }
intmain(){ scanf("%d",&n); for(int i =0; i != n;++i)scanf("%d",&road[i]); int result =0; for(int i =0; i != n;){ i =max(i +1, road[i]); ++result; } printf("%d", result); return0; }
intmain(){ int weight[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2}; char MS[]={'1','0','X','9','8','7','6','5','4','3','2'}; char id[19]={'\0'}; int t; scanf("%d",&t); int size = t; while(t--){ getchar();//扔掉多余的换行符 int result =0; for(int i =0; i !=17;++i){ int temp =getchar(); id[i]=(char) temp; int k =(temp -'0')* weight[i]; result += k; } char m = MS[result %11]; id[17]=(char)getchar(); if(toupper(id[17])== m)--size; elseprintf("%s\n", id); } if(size ==0)printf("All passed"); return0; }