当前位置:   article > 正文

西工大NOJ-2023(持续更新)_noj2023答案

noj2023答案

目录

1-10

Hello World

A+B

数据类型大小及范围

平均值

进制转换

浮点数输出

动态宽度输出

计算地球上两点之间的距离

寒风指数

颜色转换模型

11-20

对称数

幂数模

比率

操作数

组合数

方阵

乘数模

分数的加、减、乘、除法

倍数和

级数和

21-30

俄罗斯农夫乘法

竖式乘法

最大数字

查找数列

方案数

余数和

好数字

倒水

毕达哥拉斯三元组

阶乘倍数

31-40

冰雹序列

可变参数平均

可变参数累加

佩尔数

二进制表示

素数

光线追踪

哈沙德数

运动会

基思数


1-10

Hello World

  1. #include <stdio.h>
  2. int main() {
  3. printf("Hello World\n");
  4. return 0;
  5. }

A+B

  1. #include <stdio.h>
  2. int main() {
  3. int x,y;
  4. scanf("%d%d",&x,&y);
  5. printf("%d",x+y);
  6. return 0;
  7. }

数据类型大小及范围

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <limits.h>
  5. int main() {
  6. int x;
  7. scanf("%d",&x);
  8. switch (x) {
  9. case 1:
  10. printf("%lu,%d,%d",sizeof(char),(-(unsigned char)~0)>>1,(unsigned char)~0>>1);
  11. break;
  12. case 2:
  13. printf("%lu,%u,%u",sizeof(unsigned char),(unsigned char)0,(unsigned char)~0);
  14. break;
  15. case 3:
  16. printf("%lu,%d,%d",sizeof(short),(-(unsigned short)~0)>>1,(unsigned short)~0>>1);
  17. break;
  18. case 4:
  19. printf("%lu,%u,%u",sizeof(unsigned short),(unsigned short)0,(unsigned short)~0);
  20. break;
  21. case 5:
  22. printf("%lu,%d,%d",sizeof(int),1<<(sizeof(int)*8-1),~(1<<(sizeof(int)*8-1)));
  23. break;
  24. case 6:
  25. printf("%lu,%u,%u",sizeof(unsigned int),(unsigned int)0,(unsigned int)~0);
  26. break;
  27. case 7:
  28. printf("%lu,%ld,%ld",sizeof(long),((long)1)<<(sizeof(long)*8-1),~(((long)1)<<(sizeof(long)*8-1)));
  29. break;
  30. case 8:
  31. printf("%lu,%lu,%lu",sizeof(unsigned long),(unsigned long)0,(unsigned long)~0);
  32. break;
  33. case 9:
  34. printf("%lu,%lld,%lld",sizeof(long long),((long long)1)<<(sizeof(long long)*8-1),~(((long long)1)<<(sizeof(long long)*8-1)));
  35. break;
  36. case 10:
  37. printf("%lu,%llu,%llu",sizeof(unsigned long long),(unsigned long long)0,(unsigned long long)~0);
  38. break;
  39. }
  40. return 0;
  41. }

平均值

  1. #include <stdio.h>
  2. int main() {
  3. int x,y,z;
  4. scanf("%d%d",&x,&y);
  5. z=x/2+y/2;
  6. printf("%d",z);
  7. return 0;
  8. }

进制转换

  1. #include <stdio.h>
  2. int main() {
  3. int x;
  4. scanf("%d",&x);
  5. printf("%X,",x);
  6. printf("%o",x);
  7. //printf("%d",x);
  8. return 0;
  9. }

浮点数输出

  1. #include <stdio.h>
  2. int main() {
  3. double x;
  4. scanf("%lf",&x);
  5. printf("%.6lf,",x);
  6. printf("%.2lf,",x);
  7. printf("%.8lf",x);
  8. return 0;
  9. }

动态宽度输出

  1. #include <stdio.h>
  2. int main() {
  3. int x,n,y;
  4. scanf("%d %d",&x,&n);
  5. y=x;
  6. while(y > 10) {
  7. y = y / 10;
  8. n--;
  9. }
  10. n--;
  11. while(n>0){
  12. printf("0");
  13. n--;
  14. }
  15. printf("%d",x);
  16. return 0;
  17. }

计算地球上两点之间的距离

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #define PI 3.1415926
  5. #define EARTH_RADIUS 6371
  6. double radian(double d)
  7. {
  8. return d * PI / 180.0; //角度 = π / 180
  9. }
  10. double get_distance(double lat1, double lng1, double lat2, double lng2)
  11. {
  12. double radLat1 = radian(lat1);
  13. double radLat2 = radian(lat2);
  14. double a = radLat1 - radLat2;
  15. double b = radian(lng1) - radian(lng2);
  16. double dst = 2 * asin((sqrt(pow(sin(a / 2), 2) + cos(radLat1) * cos(radLat2) * pow(sin(b / 2), 2) )));
  17. dst = dst * EARTH_RADIUS;
  18. dst= round(dst * 10000) / 10000;
  19. return dst;
  20. }
  21. int main() {
  22. double lat1,lng1,lat2,lng2;
  23. scanf("%lf %lf\n%lf %lf",&lat1,&lng1,&lat2,&lng2);
  24. double dst = get_distance(lat1, lng1, lat2, lng2);
  25. printf("%0.4fkm\n",dst);
  26. return 0;
  27. }

寒风指数

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. int cold(int wp,int tep){
  5. double c;
  6. int res;
  7. c = 13.12+0.6215*tep-11.37*pow((double)wp,0.16)+0.3965*tep* pow((double)wp,0.16);
  8. res=(int)(c+0.5);
  9. return res;
  10. }
  11. int main() {
  12. int x,y;
  13. scanf("%d %d",&x,&y);
  14. int res;
  15. res= cold(x,y);
  16. printf("%d",res);
  17. return 0;
  18. }

颜色转换模型

  1. #include <stdio.h>
  2. void rgb_to_hsv(int r, int g, int b) {
  3. float R = r / 255.0f;
  4. float G = g / 255.0f;
  5. float B = b / 255.0f;
  6. float max = (R > G) ? ((R > B) ? R : B) : ((G > B) ? G : B);
  7. float min = (R < G) ? ((R < B) ? R : B) : ((G < B) ? G : B);
  8. float diff = max - min;
  9. float H = 0, S = 0, V = max * 100;
  10. if (max != min) {
  11. if (max == R) {
  12. H = 60 * ((G - B) / diff);
  13. } else if (max == G) {
  14. H = 60 * (2 + (B - R) / diff);
  15. } else if (max == B) {
  16. H = 60 * (4 + (R - G) / diff);
  17. }
  18. if (H < 0) {
  19. H += 360;
  20. }
  21. }
  22. if (max != 0) {
  23. S = diff / max * 100;
  24. }
  25. printf("%.4f,%.4f%%,%.4f%%\n", H, S, V);
  26. }
  27. int main() {
  28. int r, g, b;
  29. scanf("%d %d %d", &r, &g, &b);
  30. rgb_to_hsv(r, g, b);
  31. return 0;
  32. }

11-20

对称数

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. int is_false(int pill){
  5. if(pill==2||pill==3||pill==4||pill==5||pill==7)
  6. return 1;
  7. else return 0;
  8. }
  9. int main(){
  10. int num,n,reverse=0,pill,flag=1;
  11. scanf("%d",&num);
  12. n=num;
  13. while(n>0){
  14. pill = n%10;
  15. if(is_false(pill)) {
  16. flag=0;
  17. break;
  18. }
  19. else if(pill==9) pill=6;
  20. else if(pill==6) pill=9;
  21. reverse=reverse*10+pill;
  22. n/=10;
  23. }
  24. if(flag&&(reverse==num))
  25. printf("Yes");
  26. else printf("No");
  27. return 0;
  28. }

幂数模

  1. #include <stdio.h>
  2. typedef unsigned long long ull;
  3. ull power_mod(ull a, ull b, ull m) {
  4. ull result = 1;
  5. while (b > 0) {
  6. if (b & 1) {
  7. result = (result * a) % m;
  8. }
  9. a = (a * a) % m;
  10. b >>= 1;
  11. }
  12. return result;
  13. }
  14. int main() {
  15. ull a, b, m;
  16. scanf("%llu %llu %llu", &a, &b, &m);
  17. printf("%llu\n", power_mod(a, b, m));
  18. return 0;
  19. }

比率

  1. #include <stdio.h>
  2. #include <math.h>
  3. // 定义一个函数,用于计算最大公约数
  4. int gcd(int a, int b) {
  5. if (b == 0)
  6. return a;
  7. return gcd(b, a % b);
  8. }
  9. void convertToRatio(float num) {
  10. double precision = 0.0001; // 定义精度
  11. double decimal_part = num - (int)num; // 获取小数部分
  12. double tmp = decimal_part;
  13. int denominator = 1;
  14. // 通过连续乘法找到一个足够大的整数,使得浮点数可以被表示为两个整数的比率
  15. while (fabs(round(tmp) - tmp) > precision) {
  16. tmp = tmp * 10;
  17. denominator = denominator * 10;
  18. }
  19. int numerator = round(num * denominator); // 计算分子
  20. int divisor = gcd(numerator, denominator); // 计算最大公约数
  21. // 输出结果
  22. printf("%d/%d\n", numerator / divisor, denominator / divisor);
  23. }
  24. int main() {
  25. float num;
  26. scanf("%f",&num);
  27. convertToRatio(num);
  28. return 0;
  29. }

操作数

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. int sum_digital(int n){
  5. if (n<10) return n;
  6. else{
  7. return n%10 + sum_digital(n/10);
  8. }
  9. }
  10. int option(int n){
  11. if (n<=0)
  12. return 0;
  13. else{
  14. n -= sum_digital(n);
  15. return 1 + option(n);
  16. }
  17. }
  18. int main(){
  19. int res,n;
  20. scanf("%d",&n);
  21. if(n==0) res=1;
  22. else res= option(n);
  23. printf("%d",res);
  24. return 0;
  25. }

组合数

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. int combine(int n,int times,int *res){
  5. if (n==0&&times==4)
  6. return 1;
  7. else{
  8. if(times==4||n<0)
  9. return 0;
  10. for(int i=0;i<10;i++){
  11. if (i>n) continue;
  12. *res+=combine(n-i,times+1,res);
  13. }
  14. return 0;
  15. }
  16. }
  17. int main(){
  18. int res,n;
  19. res=0;
  20. scanf("%d",&n);
  21. combine(n,0,&res);
  22. printf("%d",res);
  23. return 0;
  24. }

方阵

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. void output(int n,int direct){//1->+;0->-to1
  5. if(direct){
  6. for(int i=0;i<n;i++){
  7. printf("%d ",i);
  8. }
  9. }
  10. else {
  11. for (int i = n; i > 0; i--) {
  12. printf("%d ", i);
  13. }
  14. }
  15. }
  16. int main(){
  17. int n,j;
  18. scanf("%d",&n);
  19. for(j=0;j<n;j++) {
  20. output(j, 0);
  21. output(n - j, 1);
  22. printf("\n");
  23. }
  24. return 0;
  25. }

乘数模

  1. #include <stdio.h>
  2. int main() {
  3. long long a, b, m;
  4. scanf("%lld %lld %lld", &a, &b, &m); // 从用户输入中读取三个整数
  5. long long result = ((a % m) * (b % m)) % m; // 计算a乘b后对m取模的结果
  6. printf("%lld\n", result); // 输出结果
  7. return 0;
  8. }

分数的加、减、乘、除法

  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. //using namespace std;
  5. void swap(int* a,int* b){
  6. int temp=(*a);
  7. (*a)=(*b);
  8. (*b)=temp;
  9. }
  10. int gcd(int m,int n){
  11. if(fabs(m)< fabs(n))
  12. swap(&m,&n);
  13. if(m%n==0) return n;
  14. else return (gcd(n,m%n));
  15. }
  16. void plus(int son_1,int mother_1,int son_2,int mother_2){
  17. int son,mother,m;
  18. son=son_1*mother_2+son_2*mother_1;
  19. mother=mother_1*mother_2;
  20. m=gcd(mother,son);
  21. son/=m;
  22. mother/=m;
  23. std::cout<<'('<<son_1<<'/'<<mother_1<<')'<<'+'<<'('<<son_2<<'/'<<mother_2<<')'<<'='<<son<<'/'<<mother<<std::endl;
  24. }
  25. void miner(int son_1,int mother_1,int son_2,int mother_2){
  26. int son,mother,m;
  27. son=son_1*mother_2-son_2*mother_1;
  28. mother=mother_1*mother_2;
  29. m=gcd(mother,son);
  30. son/=m;
  31. mother/=m;
  32. std::cout<<'('<<son_1<<'/'<<mother_1<<')'<<'-'<<'('<<son_2<<'/'<<mother_2<<')'<<'='<<son<<'/'<<mother<<std::endl;
  33. }
  34. void mul(int son_1,int mother_1,int son_2,int mother_2){
  35. int son,mother,m;
  36. son=son_1*son_2;
  37. mother=mother_1*mother_2;
  38. m=gcd(mother,son);
  39. son/=m;
  40. mother/=m;
  41. std::cout<<'('<<son_1<<'/'<<mother_1<<')'<<'*'<<'('<<son_2<<'/'<<mother_2<<')'<<'='<<son<<'/'<<mother<<std::endl;
  42. }
  43. void div(int son_1,int mother_1,int son_2,int mother_2){
  44. int son,mother,m;
  45. son=son_1*mother_2;
  46. mother=mother_1*son_2;
  47. m=gcd(mother,son);
  48. son/=m;
  49. mother/=m;
  50. std::cout<<'('<<son_1<<'/'<<mother_1<<')'<<'/'<<'('<<son_2<<'/'<<mother_2<<')'<<'='<<son<<'/'<<mother<<std::endl;
  51. }
  52. int main()
  53. {
  54. int son_1,mother_1,son_2,mother_2;
  55. std::cin>>son_1;
  56. std::cin.get();
  57. std::cin>>mother_1;
  58. std::cin.get();
  59. std::cin>>son_2;
  60. std::cin.get();
  61. std::cin>>mother_2;
  62. plus(son_1,mother_1,son_2,mother_2);
  63. miner(son_1,mother_1,son_2,mother_2);
  64. mul(son_1,mother_1,son_2,mother_2);
  65. div(son_1,mother_1,son_2,mother_2);
  66. //cout<<son_1<<' '<<mother_1<<' '<<son_2<<' '<<mother_2;
  67. return 0;
  68. }

倍数和

  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. using namespace std;
  5. int main()
  6. {
  7. int T,i,j,m;
  8. int *a,*sum;
  9. cin>>T;
  10. a=new int[T]{0};
  11. sum=new int[T]{0};
  12. for(i=0;i<T;i++)
  13. {
  14. m=0;
  15. cin>>a[i];
  16. //scanf("%d",&a[i]);
  17. sum[i]=0;
  18. for(j=0;j<a[i];j++)
  19. {
  20. if(j%3==0||j%5==0)
  21. m=m+j;
  22. sum[i]=m;
  23. }
  24. }
  25. for(i=0;i<T;i++)
  26. cout<<sum[i]<<endl;
  27. //printf("%d\n",sum[i]);
  28. return 0;
  29. }

级数和

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. void output(int int_part,int float_part){
  5. if(float_part%10==0)
  6. float_part/=10;
  7. printf("%d.%d",int_part,float_part);
  8. }
  9. void sum(int n,int i,int *total_int,int *total_float){
  10. if (i<=n){
  11. int int_part=i;
  12. int float_part;
  13. if (i<9) float_part=10*(i+1);
  14. else if(i==99) float_part=10;
  15. else float_part=i+1;
  16. *total_int+=int_part;
  17. *total_float+=float_part;
  18. //check float_part<100
  19. if(*total_float>=100){
  20. *total_float%=100;
  21. (*total_int)++;
  22. }
  23. output(int_part,float_part);
  24. if(i==n) printf("=");
  25. else printf("+");
  26. sum(n,i+1,total_int,total_float);
  27. }
  28. }
  29. int main(){
  30. int total_int,total_float;
  31. short n;
  32. total_float=total_int=0;
  33. scanf("%hd",&n);
  34. // if(n!=99)
  35. sum(n,1,&total_int,&total_float);
  36. /* else{
  37. sum(n-1,1,&total_int,&total_float);
  38. total_int+=99;
  39. total_float+=10;
  40. if(total_float>=100) {
  41. total_float %= 100;
  42. total_int++;
  43. }
  44. }*/
  45. output(total_int,total_float);
  46. return 0;
  47. }

21-30

俄罗斯农夫乘法

  1. #include <iostream>
  2. void div_fuc(int multiplicand,int multiplier, int res){
  3. while (multiplicand != 0) {
  4. std::cout << multiplicand << ' ' << multiplier << std::endl;
  5. if ((multiplicand % 2) != 0) res += multiplier;
  6. multiplicand = multiplicand >> 1;
  7. multiplier = multiplier << 1;
  8. }
  9. std::cout << res << std::endl;
  10. }
  11. int main(){
  12. int multiplicand,multiplier,res=0;
  13. std::cin >> multiplicand >> multiplier;
  14. div_fuc(multiplicand,multiplier,res);
  15. return 0;
  16. }

竖式乘法

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. int count_length(int x){
  5. int lens=0;
  6. while(x!=0){
  7. lens++;
  8. x/=10;
  9. }
  10. return lens;
  11. }
  12. int main(){
  13. int multiplicand,multiplier,res=0,multiplicand_digitalLens=0,multiplier_digitalLens=0,res_digitalLens=0;
  14. std::cin >> multiplicand >> multiplier;
  15. res = multiplicand*multiplier;
  16. multiplicand_digitalLens = count_length(multiplicand);
  17. multiplier_digitalLens = count_length(multiplier);
  18. res_digitalLens = count_length(res);
  19. std::cout << std::setw(res_digitalLens+1) << multiplicand << std::endl;
  20. std::cout << 'x' << std::setw(res_digitalLens) << multiplier << std::endl;
  21. std::cout << std::string(res_digitalLens+1,'-') << std::endl;
  22. int blank=-1;
  23. for(int i=multiplier_digitalLens-1;i>=0;i--){
  24. if (i == 0) std::cout << '+' << multiplicand*(multiplier%10) << std::endl;
  25. else
  26. std::cout << std::setw(res_digitalLens-blank) << multiplicand*(multiplier%10) << std::endl;
  27. multiplier /= 10;
  28. blank++;
  29. }
  30. std::cout << std::string(res_digitalLens+1,'-') << std::endl;
  31. std::cout << std::setw(res_digitalLens+1) << res << std::endl;
  32. return 0;
  33. }

最大数字

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. typedef long long ll;
  5. bool findTrue(ll N,ll num){
  6. if (N == 0) return true;
  7. else if( N%10 > num ) return false;
  8. else return (findTrue(N/10,N%10));
  9. }
  10. int main(){
  11. ll N,i;
  12. std::cin >> N;
  13. if (N<10) std::cout << N << std::endl;
  14. else {
  15. for (i = N; i > 0; i--) {
  16. if(findTrue(i/10,i%10)) break;
  17. }
  18. std::cout << i << std::endl;
  19. }
  20. return 0;
  21. }

查找数列

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. int main(){
  5. int total_count=1,n;
  6. std::cin >> n;
  7. for(; n > 0; ){
  8. n-= total_count;
  9. total_count++;
  10. }
  11. total_count--;
  12. n+=total_count;
  13. std::cout << n;
  14. return 0;
  15. }

方案数

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. typedef long long ll;
  5. int iter(ll N,ll i){
  6. ll j=i,tmp=0;
  7. while(j>0){
  8. tmp+=j;
  9. j--;
  10. if(tmp == N) return 1;
  11. else if (tmp > N) break;
  12. }
  13. return 0;
  14. }
  15. void solution(ll N,ll *res){
  16. for (int i = (N / 2) + 1; i >= 2; i--)
  17. (*res) += iter(N, i);
  18. }
  19. int main(){
  20. ll N,res=0;
  21. std::cin >> N;
  22. if (N<=2) {
  23. if (N==1) std::cout << 1;
  24. if (N==2) std::cout << 1;
  25. }
  26. else{
  27. solution(N,&res);
  28. std::cout << res+1;
  29. }
  30. return 0;
  31. }

余数和

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. int main(){
  5. long n,k,i=1,res=0;
  6. std::cin >> n >> k;
  7. for(;i<=n;i++){
  8. res+=k%i;
  9. }
  10. std::cout << res;
  11. return 0;
  12. }

好数字

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. long goodNumber(long N,long i){
  5. if(i==N) return 1;
  6. else if((i%2)==0) {
  7. return (5*goodNumber(N,i+1))%((int)pow(10,9)+7);
  8. }
  9. else if((i%2)==1){
  10. return (4*goodNumber(N,i+1))%((int)pow(10,9)+7);
  11. }
  12. }
  13. int main(){
  14. long N,i=0;
  15. std::cin >> N;
  16. std::cout << goodNumber(N,i);
  17. return 0;
  18. }

倒水

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. int PourWater(int m_size,int n_size,int goal){
  5. int m_now=0,n_now=0,step1=0,step2=0;
  6. while(m_now!=goal&&n_now!=goal){
  7. if(m_now==0){
  8. m_now=m_size;//fill
  9. }
  10. else if(n_now==n_size){
  11. n_now=0; //pour out
  12. }
  13. else{
  14. int total = n_now + m_now>n_size?n_size-n_now:m_now;
  15. m_now-=total;
  16. n_now+=total;
  17. }
  18. step1++;
  19. }
  20. m_now=0,n_now=0;
  21. while(m_now!=goal&&n_now!=goal){
  22. if(n_now==0){
  23. n_now=n_size;//fill
  24. }
  25. else if(m_now==m_size){
  26. m_now=0; //pour out
  27. }
  28. else{
  29. int total = m_now + n_now>m_size?m_size-m_now:n_now;
  30. n_now-=total;
  31. m_now+=total;
  32. }
  33. step2++;
  34. }
  35. int step = step1>step2?step2:step1;
  36. return step;
  37. }
  38. int main(){
  39. int m,n,d;
  40. std::cin >> m >> n >> d;
  41. std::cout << PourWater(m,n,d);
  42. return 0;
  43. }

毕达哥拉斯三元组

  1. #include <stdio.h>
  2. int main() {
  3. int n;
  4. scanf("%d", &n);
  5. for (int a = 1; a < n; a++) {
  6. for (int b = a; b < n; b++) {
  7. int c = n - a - b;
  8. if (a * a + b * b == c * c) {
  9. printf("%d\n", a * b * c);
  10. return 0;
  11. }
  12. }
  13. }
  14. printf("No solution\n");
  15. return 0;
  16. }

阶乘倍数

题解来自:NWPU-NOJ2023/26-阶乘倍数.cpp at main · Enifedefine/NWPU-NOJ2023 (github.com)

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. long long k;
  5. cin >> k;
  6. long long n = 1;
  7. for (long long i = 2 ; i * i <= k ; i ++){
  8. if (k % i == 0){
  9. long long count = 0;
  10. while (k % i == 0){
  11. k /= i;
  12. count ++;
  13. }
  14. n = max(n, count * i);
  15. }
  16. }
  17. if (k > 1){
  18. n = max(n, k);
  19. }
  20. cout << n << endl;
  21. return 0;
  22. }

31-40

冰雹序列

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. bool mod_2(long n){
  5. if(n%2==0) return true;
  6. else return false;
  7. }
  8. int main(){
  9. long n;
  10. std::cin >> n;
  11. while (n>1){
  12. std::cout << n << ' ';
  13. if(mod_2(n))
  14. n/=2;
  15. else n=3*n+1;
  16. }
  17. std::cout <<n;
  18. return 0;
  19. }

可变参数平均

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <stdarg.h>
  5. double avg(int n,...){
  6. va_list ap;
  7. int i;
  8. double sum=0.0;
  9. va_start(ap,n);
  10. for (i=0;i<n;i++){
  11. sum+= va_arg(ap,int);
  12. }
  13. va_end(ap);
  14. return (sum/n);
  15. }
  16. int main(){
  17. int a,b,c,d,e;
  18. std::cin >>a>>b>>c>>d>>e;
  19. std::cout <<std::fixed<<std::setprecision(4)<< (avg(2,a,b)-avg(3,c,d,e));
  20. return 0;
  21. }

可变参数累加

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <stdarg.h>
  5. int sum(int first,...){
  6. va_list ap;
  7. int tmp,sum=0;
  8. va_start(ap,first);
  9. tmp= first;
  10. sum+=tmp;
  11. while(tmp>0) {
  12. tmp= va_arg(ap,int);
  13. sum += tmp;
  14. }
  15. va_end(ap);
  16. return sum;
  17. }
  18. int main(){
  19. int a,b,c,d,e,f;
  20. std::cin >>a>>b>>c>>d>>e>>f;
  21. std::cout << (sum(a,b,0)-sum(c,d,e,f,0));
  22. return 0;
  23. }

佩尔数

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. int PA(int n){
  5. if(n==0) return 0;
  6. else if(n==1) return 1;
  7. else return 2* PA(n-1)+PA(n-2);
  8. }
  9. int main(){
  10. int n;
  11. std::cin >> n;
  12. std::cout << PA(n);
  13. return 0;
  14. }

二进制表示

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. void print(int n){
  5. int i=0,tmp=n;
  6. while(tmp>0){
  7. tmp>>=1;
  8. i++;
  9. }
  10. i--;
  11. tmp=(int)pow(2,i);
  12. int top=tmp;
  13. for(;tmp>0;tmp>>=1,--i){
  14. if((n&tmp)==tmp){
  15. if( i == 1 ) {
  16. if(tmp!=top)std::cout << '+';
  17. std::cout << "2";
  18. }
  19. else if(i==0){
  20. if(tmp!=top)std::cout << '+';
  21. std::cout << "2(";
  22. std::cout << i;
  23. std::cout << ')';
  24. }
  25. else{
  26. if(tmp!=top)std::cout << '+';
  27. std::cout << "2(";
  28. print(i);
  29. std::cout << ')';
  30. }
  31. }
  32. }
  33. }
  34. int main(){
  35. int n;
  36. std::cin >> n;
  37. print(n);
  38. return 0;
  39. }

素数

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. inline bool is_prime(int num){
  5. if (num <= 3)
  6. {
  7. return num > 1;
  8. }
  9. // 不在6的倍数两侧的一定不是质数
  10. if (num % 6 != 1 && num % 6 != 5)
  11. {
  12. return false;
  13. }
  14. int sqr = (int)sqrt(num);
  15. for (int i = 5; i <= sqr; i += 6)
  16. {
  17. if (num % i == 0 || num % (i + 2) == 0)
  18. {
  19. return false;
  20. }
  21. }
  22. return true;
  23. }
  24. inline int NumOfPrime(int bottom,int top){
  25. int i=bottom,nums_ofPrime=0;
  26. for(;i<=top;i++){
  27. if(is_prime(i)) {
  28. nums_ofPrime++;
  29. }
  30. }
  31. return nums_ofPrime;
  32. }
  33. int main(){
  34. int bottom,top;
  35. std::cin >> bottom >> top;
  36. std::cout << NumOfPrime(bottom,top);
  37. return 0;
  38. }

光线追踪

题解来自:NWPU-NOJ2023/40-光线追踪.cpp at main · Enifedefine/NWPU-NOJ2023 (github.com)

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. long long n , x;
  5. cin >> n >> x;
  6. if (n % 2 == 1){
  7. cout << (n - 1) * 3 << endl;
  8. }
  9. else{
  10. cout << 3 * ((n / 2) + abs(x - (n / 2))) << endl;
  11. }
  12. return 0;
  13. }

哈沙德数

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. int HarshadNumber(int n){
  5. int t=n,s=0;
  6. while(t){
  7. s=s+t%10;
  8. t=t/10;
  9. }
  10. if(s!=0 && n%s==0) return n/s;
  11. return 0;
  12. }
  13. int main(){
  14. int n,i=0;
  15. std::cin >> n;
  16. while(n>=2){
  17. if(HarshadNumber(n)!=0){
  18. n = HarshadNumber(n);
  19. i++;
  20. }
  21. else break;
  22. }
  23. std::cout << i << std::endl;
  24. return 0;
  25. }

运动会

提交上去一直一直超时,所以我想了个笨办法:把40000个结果写入一个txt文件:p

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <fstream>
  5. inline int gcd(int a,int b){
  6. if(a%b==0) return b;
  7. else return gcd(b,a%b);
  8. }
  9. bool IsSeen(int a,int b){
  10. if(gcd(a,b)==1) return true;
  11. else return false;
  12. }
  13. int main() {
  14. int nums=0,x=40000;
  15. std::cin >> x; //x默认是40000
  16. std::ofstream file;
  17. file.open("nums.txt");
  18. file << 1 ;
  19. for(int i=1;i<x;i++) {
  20. for (int j = 1; j < i; j++) {
  21. if (IsSeen(i > j ? i : j, i > j ? j : i)) {
  22. nums++;
  23. //std::cout << i <<' '<< j <<std::endl;
  24. }
  25. }
  26. file << " ," << nums*2 + 3;
  27. std::cout << nums * 2 + 3 << std::endl;
  28. }
  29. return 0;
  30. }
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <fstream>
  5. int main() {
  6. int n;
  7. int nums[40000]= {/*在nums.txt文件中的数字*/}
  8. std::cin >> n;
  9. std::cout << nums[n-1] << std::endl;
  10. return 0;
  11. }

一种很好的办法,简洁地求出欧拉函数:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //找到小于n与n互质的数的个数即可(欧拉函数)
  4. //https://www.luogu.com.cn/problem/solution/P2158
  5. int main()
  6. {
  7. int n,ans,p[50000],i,j;
  8. scanf("%d",&n);
  9. for(i=1;i<=n;++i)
  10. {
  11. p[i]=i;//赋初值(假定所有数都互质)
  12. }
  13. for(i=2;i<=n;++i)
  14. {
  15. if(p[i]==i)//如果i是质数
  16. {
  17. for(j=i;j<=n;j+=i)
  18. {
  19. //此处质数i直接带入欧拉函数进行计算
  20. //phi(j) = j * (1 - 1/p)
  21. p[j]=p[j]*(i-1)/i;
  22. }
  23. }
  24. }
  25. for(i=1;i<n;++i)
  26. {
  27. ans+=p[i];
  28. }
  29. printf("%d\n",(n==1)?0:ans<<1|1);//n=1时候特判
  30. return 0;
  31. }

基思数

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. inline bool IsKeith(int n){
  5. int tmp=n,nums_i=99,lens=0;
  6. int nums[200]={0};
  7. while(tmp>0){
  8. nums[nums_i]=tmp%10;
  9. tmp/=10;
  10. nums_i++;
  11. }
  12. lens=nums_i-99;
  13. while(nums[nums_i-lens-1]!=n){
  14. for(int i=nums_i-1;i>=nums_i-lens;i--){
  15. nums[nums_i-lens-1]+=nums[i];
  16. }
  17. if(nums[nums_i-lens-1]==n) return true;
  18. else if(nums[nums_i-lens-1]>n||(nums_i-lens-1)<0) break;
  19. nums_i--;
  20. }
  21. return false;
  22. }
  23. int main() {
  24. int n;
  25. std::cin >> n;
  26. if(IsKeith(n)) std::cout<<"Yes"<<std::endl;
  27. else std::cout << "No"<<std::endl;
  28. return 0;
  29. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/545353
推荐阅读
相关标签
  

闽ICP备14008679号