https://ac.nowcoder.com/acm/contest/25080
if从句搞定
#include <stdio.h> int main(){ long long int n,a,cnt5,cnt1; scanf("%d %d",&n,&a); cnt5 = a / 5; if(cnt5 > n){ cnt5 = n; cnt1 = a-n*5; }else{ cnt1 = a-cnt5*5; } printf("%d %d",cnt5,cnt1); return 0; }
#include <stdio.h> int main(){ int n; scanf("%d",&n); if(n%4==0){ printf("bob"); }else{ printf("kiki"); } return 0; }
https://ac.nowcoder.com/acm/contest/25080求余通过比较找最大值
#include <bits/stdc++.h> using namespace std; int main(){ int n,r,l; int ans=0; cin >> n >> l >> r; for(int i=l;i<=r;i++){ ans=max(ans,i%n); } cout << ans; return 0; }
位运算详细戳这https://blog.csdn.net/deaidai/article/details/78167367?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163819810116780357218526%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163819810116780357218526&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-78167367.pc_search_result_cache&utm_term=%E4%BD%8D%E8%BF%90%E7%AE%97&spm=1018.2226.3001.4187
注意题目是 求二的正整数次幂之和;
奇数直接输出-1,不符合要求;
剩下的数由二进制形式找到第一个“1”所在的位置,前面补零
#include <bits/stdc++.h> using namespace std; int main(){ int x,i; cin >> x; if(x%2!=0){ cout<<-1<<endl; }else{ for(i=31;i>=1;i--){ if(x>>i&1){ cout<<(1<<i)<<" "; //输出2的i次方; } } } return 0; } //x&1代表最低位的值(最右位的值) //假设:x=10==(1010)2 //那么 x&1 == 0
#include<stdio.h> int main(void){ int n ; int i ; scanf("%d" , &n); if (n % 2 == 1) printf("-1"); if(n % 2 == 0){ for(i = 2 ; n > 0 ; i = i * 2){ if(i > n){ i = i / 2; n = n - i; printf("%d " , i); i = 2; } } } return 0 ; }